Question 1 (5 points) What is displayed on the console when running the followin
ID: 3760986 • Letter: Q
Question
Question 1 (5 points)
What is displayed on the console when running the following program?
public class Quiz2B
{
public static void main(String[] args)
{
try
{
System.out.println("Welcome to Java");
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
catch (RuntimeException ex)
{
System.out.println("Welcome to Java");
}
finally
{
System.out.println("End of the block");
}
}
}
Question 1 options:
The program displays Welcome to Java two times followed by End of the block.
The program displays Welcome to Java two times.
The program displays Welcome to Java three times followed by End of the block.
The program displays Welcome to Java three times.
Save
Question 2 (5 points)
Under what conditions must a throws clause be added to a method signature
Question 2 options:
Whenever any checked exception that can be thrown by the method is not caught
Whenever any unchecked exception that can be thrown by the method is not caught
Whenever any exception can be thrown by the method
Whenever any checked exception can be thrown by the method
Save
Question 3 (5 points)
What is displayed on the console when running the following program?
public class Quiz2C
{
public static void main(String[] args)
{
try
{
method();
System.out.println("After the method call");
}
catch (NumberFormatException ex)
{
System.out.println("NumberFormatException");
}
catch (RuntimeException ex)
{
System.out.println("RuntimeException");
}
}
private static void method()
{
String s = "8.3";
Integer.parseInt(s);
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
}
Question 3 options:
The program displays NumberFormatException
The program displays NumberFormatException followed by After the method call
The program displays NumberFormatException followed by RuntimeException
The program has a compilation error
Save
Question 4 (5 points)
What exceptions will be caught by a catch block that catches exceptions of the class RuntimeException?
Question 4 options:
Exceptions of type RuntimeException and any of its subclasses
Only exceptions of type RuntimeException
Any exception
Exceptions of type RuntimeException and any of its superclasses
Save
Question 5 (5 points)
What exception type does the following program throw?
public class Quiz2D
{
public static void main(String[] args)
{
Object object = new Object();
String string = (String) object;
}
}
Question 5 options:
ClassCastException
ArithmeticException
No exception
StringIndexOutOfBoundsException
Save
Question 6 (5 points)
What is wrong in the following program?
public class Quiz2E
public static void main(String[] args)
{
try
{
System.out.println("Hello world");
}
}
}
Question 6 options:
A method call that does not declare exceptions cannot be placed inside a try block
You cannot have a try block without a catch block
Nothing is wrong
You cannot have a try block without a catch block or a finally block
Save
Question 7 (5 points)
Select the correct statement regarding the program show below:
public class Quiz2G
{
public static void main(String[] args)
{
if (Integer.parseInt(args[0]) == 0)
throw new Exception("Invalid Command Line Argument");
}
}
Question 7 options:
The program will not compile because a throws clause is needed
The program will compile without any errors
The program will not compile because Exception is an unchecked exception
The program will not compile because parseInt could throw NumberFormatException and it is never caught
Save
Question 8 (5 points)
The difference between throw and throws is correctly explained by which of the following statements?
Question 8 options:
The two reserved words are interchangeable
throw is a reserved word, but throws is not
Either one can be used to throw an exception
throws is used in a method signature, throw is used to throw an exception
Save
Question 9 (5 points)
What will occur if the catch block in program show below are reversed?
class Quiz2H
{
public static void main(String[] args)
{
try
{
aMethod();
System.out.println("After the call");
}
catch (ArithmeticException exception)
{
System.out.println("Arithmetic Exception");
}
catch (RuntimeException exception)
{
System.out.println("Runtime Exception");
}
System.out.println("After the try-catch statement");
}
private static void aMethod() throws RuntimeException
{
int x = 1/0;
}
}
Question 9 options:
The change will have no effect, the program will work the same as before
The program will no longer compile
Only the RuntimeException will be caught
Both exceptions will be caught
Save
Question 10 (5 points)
What occurs when an exception is not caught in the current method?
Question 10 options:
The exception is propagated to the method that called the current method
The exception is rethrown
The program always terminates and displays an error message
The exception is ignored
Save
Question 11 (5 points)
Which of the following statements about type compatibility is true?
Question 11 options:
An object of a subclass can be assigned to a reference to its superclass without a type cast
Assigning an object of a superclass to a reference to one of its subclasses always causes a compilation error whether type casting is used or not
An object of a superclass can be assigned to a reference to one of its subclasses without casting
An object of a subclass can be assigned to a reference to its superclass, but type casting is required
Save
Question 12 (5 points)
Which of the following statements about final classes is correct?
Question 12 options:
A final class must have all abstract methods
A final class cannot have any instances
A final class cannot be extended
A final class cannot have a constructor
Save
Question 13 (5 points)
Which of the following expressions evaluates to false?
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C1 c2 = new C2();
C1 c3 = new C3();
C1 c4 = new C4();
Question 13 options:
c1 instanceof C1
c2 instanceof C1
c4 instanceof C2
c3 instanceof C1
Save
Question 14 (5 points)
Select the correct statement about interfaces and abstract classes among those shown below.
Question 14 options:
An abstract class cannot have any constructors
It is possible to create an instance of an abstract class
An abstract class can have ordinary methods but an interface cannot
No class can implement more than one interface
Save
Question 15 (5 points)
Which of the following declarations are valid?
class Base
{
...
}
interface Spec
{
...
}
class Derived extends Base implements Spec
{
}
Question 15 options:
Spec object4 = new Base();
Derived object2 = new Base();
Spec object3 = new Derived();
Derived object5 = new Spec();
Save
Question 16 (5 points)
Which of the following can contain a constructor?
Question 16 options:
Any nonabstract class or interface
Any class or interface
Any nonabstract or abstract class
Any abstract class or interface
Save
Question 17 (5 points)
What is the output the program shown below.
interface A
{
}
class D extends C
{
}
class C
{
}
class B extends D implements A
{
}
public class Quiz2F
{
public static void main(String[] args)
{
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
Question 17 options:
b is an instance of C
Nothing
b is an instance of A
b is an instance of A followed by b is an instance of C
Save
Question 18 (5 points)
Which of the following class definitions defines a legal abstract class?
Question 18 options:
class A { abstract void unfinished() { } }
public class abstract A { abstract void unfinished(); }
class A { abstract void unfinished(); }
abstract class A { abstract void unfinished(); }
Save
Question 19 (5 points)
Which of the following statements regarding abstract methods is not true?
Question 19 options:
A data field can be declared abstract
It is possible to declare an abstract class that contains no abstract methods
A class that contains abstract methods must be abstract
Abstract classes can have constructors
Save
Question 20 (5 points)
What is true about using the reserved word super?
Question 20 options:
It can only be used in a class that contains an extends clause
It can only be used in a constructor
It can only be used in superclasses
It can only be used in subclasses
The program displays Welcome to Java two times followed by End of the block.
The program displays Welcome to Java two times.
The program displays Welcome to Java three times followed by End of the block.
The program displays Welcome to Java three times.
Explanation / Answer
Question 1
Answer: The program displays Welcome to Java two times followed by End of the block.
Explanation: As per the given code, there are no exceptions in the try-block so the catch block will not execute. The final-block will execute if it may contains exceptions or may not contains the exceptions.
______________________________________________________________________
Question 2
Answer: Whenever any checked exception that can be thrown by the method is not caught.
Explanation: The exception is thrown using the keyword throw
_____________________________________________________________________
Question 3
Answer: The program displays NumberFormatException
Explanation: A string variable ‘s’ contains the value 8.3. This value can be converted to the float variable but in the method named method(), the lines of code trying to convert in the integer datatype.
_____________________________________________________________________
Question 4
Answer: Only exceptions of type RuntimeException
Explanation: In the catch block the written for the type RuntimeException. If the run time exception occurs, the related catch block will executes.
_____________________________________________________________________
Question 5
Answer: ClassCastException
Explanation: An object cannot be cast to the string.
_____________________________________________________________________
Question 6
Answer: You cannot have a try block without a catch block or a finally block
Explanation: As per the syntax the try block must followed by catch or final block.
_____________________________________________________________________
Question 7
Answer: The program will not compile because a throws clause is needed.
Explanation: The program will not compile because Exception is an unchecked exception.
_____________________________________________________________________
Question 8
Answer: throws is used in a method signature, throw is used to throw an exception
Explanation: The throws keyword is used to throw an exception it is written in a method signature.
_____________________________________________________________________
Question 9
Answer: Both the exceptions will be caught
Explanation: In a method named ‘aMethod()’ the arithmetic operation is done in between 1 and 0. If the denomination is zero then it raises the Athematic Exception and it is a runtime exception also. So both the exceptions will be caught.
_____________________________________________________________________
Question 10
Answer: The exception is propagated to the method that called the current method
Explanation: if the exception is not coughed then the exception will passed from the current method to the invoking method.
_____________________________________________________________________
Question 11
Answer: An object of a subclass can be assigned to a reference to its superclass without a type casting.
_____________________________________________________________________
Question 12
Answer: A final class cannot be extended
Explanation: A final class does not have abstract methods. Only an abstract class can have abstract methods.
_____________________________________________________________________
Question 13
Answer: c4 instance of C2
Explanation: From the given options c4 instance of C2 is false.
The class C4 extends C1 which states that all the properties of C1 can be used by C4. So c4 is not an instance of C2.
_____________________________________________________________________
Question 14
Answer: An abstract class can have ordinary methods but an interface cannot."
Explanation: An abstract class can have abstract methods and ordinary methods.
An interface can have ordinary methods.
_____________________________________________________________________
Question 15
Answer: Spec object3 = new Derived();"
_____________________________________________________________________
Question 16
Answer: Any non-abstract or abstract class
Explanation: An abstract class can have a constructor. The object for an abstract class is created by the anonymous subclass. The constructor of the abstract class is automatically invoked. Interface cannot have constructor.
_____________________________________________________________________
Question 17
Answer: b is an instance of A followed by b is an instance of C
_____________________________________________________________________
Question 18
Answer: abstract class A { abstract void unfinished(); }
Explanation:
Abstract is a special type of class that cannot be instantiated. Abstract methods are the methods declared without implementation.
_____________________________________________________________________
Question 19
Answer: A data field can be declared abstract
Explanation: Data fields are declared directly without using any abstract keywords.
_____________________________________________________________________
Question 20
Answer: It can only be used in subclasses
Explanation: The keyword ‘super’ is used in sub class to call the overridden method of super class or to call the constructor of super class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.