Friday, July 4, 2008

SCJP Mock Exams on Interface Related Certification Objectives

Just for reference, here's the SCJP 6.0 Objectives List

  • Develop code that declares classes (including abstract and all forms of nested classes), interfaces, and enums, and includes the appropriate use of package and import statements (including static imports).
  • Develop code that declares an interface. Develop code that implements or extends one or more interfaces. Develop code that declares an abstract class. Develop code that extends an abstract class.
I figured I'd put together a few mock exam questions on the two key sun certification exam objectives that have to do with Interfaces.

The key to interfaces is that they are simply a listing of public methods and constants that all implementing classes will either implement in the case of the methods, or have access to in the case of the constants. That's really it. However, the JVM will jump to many conclusions about your code, so sometimes, code that you really wouldn't expect to compile, will. This tends to catch SCJP exam takers off guard a bit.


Which of the following properly declares a public, top level interface, named JavaCertified?

a) public interface class JavaCertified {}
b) public class JavaCertified {}
c) public interface JavaCertified {}
d) public class JavaCertified extends Interface {}

Option c) is the correct answer to this entry level question on properly declaring interfaces.

Option a) is incorrect and simply would not compile, as a Java type cannot be both a class and an interface at the same time. Option c) would indeed compile, but it would create a Java class type, not a Java interface, and an interface is what's needed. Option d) is nonsensical gibberish and would not compile to create an interface named JavaCertified.

Option d) properly declares an interface named JavaCertified, properly using the public access modifier and the interface keyword.


Which of the three keywords are valid when preceeding the keyword int in the following code?

public interface JavaCertified {
int ARRESTING = 1010;

a) public
b) private
c) protected
d) transient
f) static
h) abstract
i) final

Options a) f) and i) are correct.

You are not allowed to declare instance level variables in an interface, so all variables that are declared within an interface are constants. The final keyword in Java denotes a variable whose value cannot change once initialized, and the static ensures that only one memory location is used to store that constant variable. So, variables defined within an interface are static and final. Furthermore, all implementing classes must have access to any constant properties defined within an interface, and as such, propertied defined within an interface should be decorated with the public keyword

So, as far as properties go, Java Interfaces can only contain constants. And we know how constants are properly declared in Java, right? If we do, the following is a very simple question:

Which of the following properly declares, following both syntax and convention, a constant property of type double, to the value of 10.10?

a) public static double cfrb = 10.10;
b) public static final double CFRB = 10.10;
c) public final static double cfrb = 10.10;
d) public const double CFRB = 10.10;

Well, we all know that constants must absolutely use the final keyword, and they should also use the static keyword as well, to ensure that every instance doesn't maintain a private copy of the constant value. Both options b) and c) will syntactically create a constant value, but c) does not conform to the naming convention, which demands that constants are named with upper case letters, so to be true to the question, c) is incorrect.

Option a) is incorrect, as it does not use the final keyword. Option d) is incorrect, because it also does not use the final keyword. Curiously, const is indeed a reserved word in Java, although it doesn't actually have a meaningful implementation.

So, as we know, option b) here is correct, as it is the only line of code that properly declared and initializes a constant property, while at the same time, being true to Java naming conventions.

So, we know two things for sure:

  1. We know how to declare and define constants using the static and final keywords
  2. We know that Java interfaces cannot contain any variables, although they can define constants.
So, powered with that knowledge, how would you answer the following question.

Which of the following lines of code would compile if placed on the commented line

public interface JavaCertified {
/* insert code here */

a) public int id;
b) public static final int id = 10;
c) public final static int id = 10;
d) public static final int = 10;
e) public static id = 10;
f) public final id = 10;
g) public static final id;

Well, the answer might surprise you.

Obviously g) will not compile, as the declaration lacks a datatype definition. Option d) will not compile due to the fact that it lacks a variable name. Now, in light of the fact that interfaces can only declare constants, or static final properties, you would think that only b) and c) would properly compile. Well, they do...But, options a) e) and f) also compile just fine, making a) b) c) e) and f) will also compile. Tricky, eh?

***Java Keywords***
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
not used
added in 1.2
added in 1.4
added in 5.0

No comments: