Sunday, July 20, 2008

More SJCP 6.0 Mock Exams for JVM 1.6 Sample Test and Sample Quiz Answers

Give the following class and interface definitions:

interface Family {}

abstract class Ancestor implements Family {}
class Parent extends Ancestor {}
class Child extends Parent {}

Which lines of code would be valid after the declaration and initialization of Ancestor a?

public class SCJPTestClass {

public static void main(String args[]) {
Ancestor a = new Parent();

}
}

a) Child c = (Child) a;
b) Parent p = (Parent) a;
c) Family f = (Family) a;
d) Ancestor an = (Ancestor) a;

Options b) c) and d) are correct.

Option a) will not compile, as the object that Ancestor a refers to was created as a Parent object, and while a Parent can be cast into anything above it on the inheritance hierarchy, it cannot be cast into anything below it on the inheritance hierarchy, and the Child class is below the Parent class on the hierarchy chain. Option a) will generate a ClassCastException at runtime, although the code will compile just fine.

Option b) casts the ancestor reference a back into a Parent, which is fine, as the object was originally creates as a Parent instance.

Furthermore, a Parent shares an is-a relationship with the Family class through the fact that it inherits from Ancestor, and Ancestor implements the Family interface. So option c) is completely valid as well.

Option d) simply casts the ancestor reference into an ancestor, which is completely valid, although perhaps a bit redundant and unnecessary.




###############

Given the following code snippet, when will the contents of the finally block be executed?

public class SCJP6Certified {
public static void main(String args[]) {

String input = (new java.util.Scanner( System.in )).nextLine();

try{ Long haul = new Long(input); }
catch (Exception e) { e.printStackTrace(); }
finally{ System.out.println("finally"); }

}
}

a) Any time the try block throws a RuntimeException
b) Any time the try block runs runs without throwing an Exception
c) Any time the try block throws a checked Exception
d) Any time the try block throws an Error

Options b) c) and d) are correct.

The finally block will always execute whenever a try block is entered, that is, barring someone hasn't pulled the plug out of the computer while the code is executing.

The finally block is often used for cleaning up resources that may need special attention upon their destruction. It's a great place for doing such things, because the finally block is always executed so long as the corresponding try block is entered, regardless of whether the try block throws an exception, or runs to fruition.


Given the following code snippet, when will the contents of the catch block be executed?

public class SCJP6Certified {
public static void main(String args[]) {

String input = (new java.util.Scanner( System.in )).nextLine();

try{ Long haul = new Long(input); }
catch (Error e) { e.printStackTrace(); }
finally{ System.out.println("finally"); }

}
}

a) Any time the try block throws a NumberFormatException
b) Any time the try block runs runs without throwing an Exception
c) Any time the try block throws a checked Exception
d) Any time an abnormal condition generates an Error

Option d) is correct.

Since this try catch finally block only looks for an Error object, normal checked Exception instances and RuntimeException instances will be ignored by the catch block, and will cause termination of the program after the finally block is executed.

"An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. " Java 6 API JavaDoc java.lang.Error

The following are commonly seen subclasses of java.lang.Error:
AnnotationFormatError, AssertionError, AWTError, CoderMalfunctionError, FactoryConfigurationError, FactoryConfigurationError, IOError, LinkageError, ServiceConfigurationError, ThreadDeath, TransformerFactoryConfigurationError, VirtualMachineError

No comments: