Page: 4
3. Operators
17. Given:
1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];
5. Object o1 = a;
6. int[][] a2 = (int[][]) o1;
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. } }
What is the result?
A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4.
E. Compilation fails due to an error on line 5.
F. Compilation fails due to an error on line 6.
G. Compilation fails due to an error on line 7.
Answer:
->C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int[][]not an int[]. If line 7 was removed, the output would be 4.
->A, B, D, E, F, and G are incorrect based on the above. (Objective 1.3)
18. Given:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }
What is the result?
A. 1 1
B. 2 1
C. 3 1
D. 4 1
E. 2 3
F. 3 3
G. 4 3
Answer:
-> G is correct. Two rules apply to the first invocation of doX(). You can’t widen and then box in one step, and var-args are always chosen last. Therefore you can’t widen shorts to either ints or longs, and then box them to Integers or Longs. But you can box shorts to Shorts and then widen them to Numbers, and this takes priority over using a var-args method. The
second invocation uses a simple box from int to Integer.
-> A, B, C, D, E, and F are incorrect based on the above. (Objective 3.1)
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)
Page: 4
1
2
3
4
5