What is displayed on the console when running the following program?
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 three times followed by End of the block. |
| The program displays Welcome to Java three times. |
| The program displays Welcome to Java two times followed by End of the block. |
| The program displays Welcome to Java two times. |
Save
Question 2 (5 points)
Under what conditions must a throws clause be added to a method signature
Question 2 options:
| Whenever any unchecked exception that can be thrown by the method is not caught |
| Whenever any checked exception that can be thrown by the method is not caught |
| Whenever any checked exception can be thrown by the method |
| Whenever any 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 RuntimeException |
| The program has a compilation error |
| The program displays NumberFormatException followed by After the method call |
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 superclasses |
| Exceptions of type RuntimeException and any of its subclasses |
| Any exception |
| Only exceptions of type RuntimeException |
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:
| No exception |
| StringIndexOutOfBoundsException |
| ArithmeticException |
| ClassCastException |
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 |
| Nothing is wrong |
| You cannot have a try block without a catch block or a finally block |
| You cannot have a try block without a catch 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 parseInt could throw NumberFormatException and it is never caught |
| 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 |
Save
Question 8 (5 points)
The difference between throw and throws is correctly explained by which of the following statements?
Question 8 options:
| Either one can be used to throw an exception |
| The two reserved words are interchangeable |
| throw is a reserved word, but throws is not |
| 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:
| Only the RuntimeException will be caught |
| The change will have no effect, the program will work the same as before |
| The program will no longer compile |
| 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 program always terminates and displays an error message |
| The exception is ignored |
| The exception is propagated to the method that called the current method |
| The exception is rethrown |
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 subclass can be assigned to a reference to its superclass, but type casting is required |
| An object of a superclass can be assigned to a reference to one of its subclasses without casting |
Save
Question 12 (5 points)
Which of the following statements about final classes is correct?
Question 12 options:
| A final class cannot have any instances |
| A final class cannot be extended |
| A final class cannot have a constructor |
| A final class must have all abstract methods |
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:
| It is possible to create an instance of an abstract class |
| An abstract class cannot have any constructors |
| 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:
| Derived object5 = new Spec(); |
| Derived object2 = new Base(); |
| Spec object3 = new Derived(); |
| Spec object4 = new Base(); |
Save
Question 16 (5 points)
Which of the following can contain a constructor?
Question 16 options:
| Any abstract class or interface |
| Any nonabstract class or interface |
| Any class or interface |
| Any nonabstract or abstract class |
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:
| Nothing |
| b is an instance of C |
| 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:
| public class abstract A { abstract void unfinished(); } |
| class 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:
| 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 |
| A data field can be declared abstract |
Save
Question 20 (5 points)
What is true about using the reserved word super?
Question 20 options:
| It can only be used in a constructor |
| It can only be used in subclasses |
| It can only be used in superclasses |
| It can only be used in a class that contains an extends clause |