Monday 18 March 2013

SCJP - 32 -Given Example



What is the result?
A. 343 340 340
B. 343 340 342
C. 343 341 342
D. 343 341 340
E. 343 341 343
F. Compilation fails.
G. An exception is thrown at runtime.

Answer:

D is correct. There are three different long variables named tooth. Remember that you
can apply the final modifier to local variables, but in this case the 2 versions of tooth
marked final are not changed. The only tooth whose value changes is the one not
marked final. This program demonstrates a bad practice known as shadowing.

A, B, C, E, F, and G are incorrect based on the above. (Objective 7.3)
==
class Knowing {
static final long tooth = 343L;
static long doIt(long tooth) {
System.out.print(++tooth + " ");

return ++tooth;
}
public static void main(String[] args) {
System.out.print(tooth + " ");
final long tooth = 340L;
new Knowing().doIt(tooth);
System.out.println(tooth);
} }


No comments:

Post a Comment