repeated questions in scjp material


1 2 3 4 5 6 7 --> Main page


19. Given:
class Mixer {
Mixer() { }
Mixer(Mixer m) { m1 = m; }
Mixer m1;
public static void main(String[] args) {
Mixer m2 = new Mixer();
Mixer m3 = new Mixer(m2); m3.go();
Mixer m4 = m3.m1; m4.go();
Mixer m5 = m2.m1; m5.go();
}
void go() { System.out.print("hi "); }
}
What is the result?
A. hi
B. hi hi
C. hi hi hi
D. Compilation fails
E. hi, followed by an exception
F. hi hi, followed by an exception
Answer:
-> F is correct. The m2 object’s m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.
-> A, B, C, D, and E are incorrect based on the above. (Objective 7.3)
20. Given:
1. class Zippy {
2. String[] x;
3. int[] a [] = {{1,2}, {1}};
4. Object c = new long[4];
5. Object[] d = x;
6. }
What is the result?
A. Compilation succeeds.
B. Compilation fails due only to an error on line 3.
C. Compilation fails due only to an error on line 4.
D. Compilation fails due only to an error on line 5.
E. Compilation fails due to errors on lines 3 and 5.
F. Compilation fails due to errors on lines 3, 4, and 5.
Answer:
-> A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays can be cast.
->B, C, D, E, and F are incorrect because this code compiles. (Objective 1.3)
21. Given:
class Fizz {
int x = 5;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;
} }
What is the result?
A. true true B. false true C. true false
D. false false E. Compilation fails. F. An exception is thrown at runtime.
Answer:
->A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object, but final doesn’t keep the object’s state from changing.
->B, C, D, E, and F are incorrect based on the above. (Objective 7.3)
22. Given:
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);
} }
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)
23. Given:
1. class Bigger {
2. public static void main(String[] args) {
3. // insert code here
4. }
5. }
6. class Better {
7. enum Faster {Higher, Longer};
8. }
Which, inserted independently at line 3, will compile? (Choose all that apply.)
A. Faster f = Faster.Higher;
B. Faster f = Better.Faster.Higher;
C. Better.Faster f = Better.Faster.Higher;
D. Bigger.Faster f = Bigger.Faster.Higher;
E. Better.Faster f2; f2 = Better.Faster.Longer;
F. Better b; b.Faster = f3; f3 = Better.Faster.Longer;
Answer:
-> C and E are correct syntax for accessing an enum from another class.
->A, B, D, and F are incorrect syntax.

1 2 3 4 5 6 7 --> Main page

0 Response to "repeated questions in scjp material"

Post a Comment