Wednesday, July 9, 2008

SCJA & SCJP 6 Mock Exam Questions and Tests

By the way, this is a continuation of an earlier post on getting SCJP certified in Java 6. These mock tests questions and answers are related to interfaces.

So, as we can see, Java interfaces cannot define instance variables, or even static variables for that matter. There is no "varying" when it comes to properties defined within a Java interface. Instead, Java interfaces can only contain public constant attributes, or as we say in Java parlance, "pulbic static final" properties.

However, the JVM doesn't really enforce this from a syntax point of view, you could use the syntax for defining an instance variable in a Java interface, such as int i = 10; and it would compile. Even though int i = 10; would imply a non-static, variable with default access mitigation, the reality is, the compiler defaults it to a public, static and final constant when the code is both compiled and run.

You'd figure that the compiler would enforce a certain syntax with variable, and for that matter, method declarations, when the code is compiled, but it doesn't. Regardless though, despite how interface variables are defined, they are always public static and final.

Note that leaving off the public, static or final keywords on a property in a Java interface will not cause a compiler problem, adding incorrect keywords before an interface constant, such as abstract, native, private, protected or volatile, will indeed cause compiler problems.

Of course, Java interfaces aren't so much about defining constants, as much as they are about defining a common behavior between different classes that may or may not share an inheritance hierarchy. And when we talk about behaviour, we're talking about methods, and really, that's the whole point of an interface, to declare abstract methods that implementing classes will code and implement.

So, if I guess a good question would be to how define an abstract method,seeing that that's all that a Java interface can contian.

Which of the following properly defines an abstract method?

a) public static abstract int getCertified();

b) public abstract int getCertified();

c) public final abstract int getCertified();

d) final abstract in getCertified();

e) public int abstract getCertified() {};

..more to come...

No comments: