Friday, February 4, 2011

Self Test: Chapters 15 to 21

Self Test Questions:

Question 1
Given:
class GCTest {
Short story = 200;
GCTest go(GCTest cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
GCTest c1 = new GCTest();
GCTest c2 = new GCTest();
GCTest c3 = c1.go(c2);
c1 = null;
// do Stuff
} }

When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime

Question 2
Given:
class Army {
String invade(short guns) { return "a few"; }
String invade(short... guns) { return "many"; }
}
class Defend {
public static void main(String [] args) {
System.out.println(new Army().invade(7));
}
}

What is the result?
A. many
B. a few
C. Compilation fails
D. The output is not predictable
E. An exception is thrown at runtime

Question 3
Given:
1. class ArrayTest {
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

Question 4
Given:
class Driver {
Driver() { }
Driver(Driver m) { m1 = m; }
Driver m1;
public static void main(String[] args) {
Driver m2 = new Driver();
Driver m3 = new Driver(m2); m3.go();
Driver m4 = m3.m1; m4.go();
Driver 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

Question 5
Given:
class Kill {
int x = 5;
public static void main(String[] args) {
final Kill f1 = new Kill();
Kill f2 = new Kill();
Kill f3 = KillSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Kill KillSwitch(Kill x, Kill y) {
final Kill 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

Question 6
Given:
class Car {
{ System.out.print("b1 "); }
public Car() { System.out.print("b2 "); }
}
class Ferrari extends Car {
static { System.out.print("r1 "); }
public Ferrari() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class F50 extends Ferrari {
public static void main(String[] args) {
System.out.print("pre ");
new F50();
System.out.println("F50 ");
}
}

What is the result?
A. pre b1 b2 r3 r2 F50
B. pre b2 b1 r2 r3 F50
C. pre b2 b1 r2 r3 F50 r1 r4
D. r1 r4 pre b1 b2 r3 r2 F50
E. r1 r4 pre b2 b1 r2 r3 F50
F. pre r1 r4 b1 b2 r3 r2 F50
G. pre r1 r4 b2 b1 r2 r3 F50
H. The order of output cannot be predicted
I. Compilation fails

Question 7
Given:
3. public class Oops {
4. static int Oops = 7;
5. public static void main(String[] args) {
6. new Oops().go(Oops);
7. System.out.print(" " + Oops);
8. }
9. void go(int Oops) {
10. Oops++;
11. for(int Oops = 3; Oops < 6; Oops++)

12. ;

13. System.out.print(" " + Oops);

14. }

15. }


What is the result?

A. 5 7

B. 5 8

C. 8 7

D. 8 8

E. Compilation fails

F. An exception is thrown at runtime


Question 8
Given:
3. public class Test {
4. static String s = "";
5. public static void main(String[] args) {
6. int x = 4; Boolean y = true; short[] sa = {1,2,3};
7. doSomething(x, y);
8. doSomething(x);
9. doSomething(sa, sa);
10. System.out.println(s);
11. }
12. static void doSomething(Object o) { s += "1"; }
13. static void doSomething(Object... o) { s += "2"; }
14. static void doSomething(Integer... i) { s += "3"; }
15. static void doSomething(Long L) { s += "4"; }
16. }

What is the result?
A. 212
B. 232
C. 234
D. 312
E. 332
F. 334
G. Compilation fails

Question 9
Given:
3. class TestGarbageCollection {
4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
5. }
6. public class Eggs {
7. public static void main(String[] args) {
8. TestGarbageCollection [] da = new TestGarbageCollection[3];
9. da[0] = new TestGarbageCollection();
10. TestGarbageCollection d = new TestGarbageCollection();
11. da[1] = d;
12. d = null;
13. da[1] = null;
14. // do stuff
15. }
16. }

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?
A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One object is eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC


Question 10
Given:
3. public class Test {
4. int x = 3;
5. public static void main(String[] args) {
6. new Test().go1();
7. }
8. void go1() {
9. int x;
10. go2(++x);
11. }
12. void go2(int y) {
13. int x = ++y;
14. System.out.println(x);
15. }
16. }

What is the result?
A. 2
B. 3
C. 4
D. 5
E. Compilation fails
F. An exception is thrown at runtime

Self Test - Answers:

Question 1:

C is correct. Only one GCTest object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

A, B, D, E, and F are incorrect based on the above

Question 2:

C is correct, compilation fails. The var-args declaration is fine, but invade takes a short, so the argument 7 needs to be cast to a short. With the cast, the answer is B, ‘a few’.

A, B, D, and E are incorrect based on the above.

Question 3:

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

Question 4:

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

Question 5:

A is correct. The references f1, z, and f3 all refer to the same instance of Kill. 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

Question 6:

D is correct. Static init blocks are executed at class loading time, instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type occur in a class, they run in order, from the top down.

A, B, C, E, F, G, H, and I are incorrect based on the above.

Question 7:

E is correct. The parameter declared on line 9 is valid (although ugly), but the variable name Oops cannot be declared again on line 11 in the same scope as the declaration on line 9.

A, B, C, D, and F are incorrect based on the above

Question 8:

A is correct. It’s legal to autobox and then widen. The first call to doSomething() boxes the int to an Integer then passes two objects. The second call cannot widen and then box (making the Long method unusable), so it boxes the int to an Integer. As always, a var-args method will be chosen only if no non-var-arg method is possible. The third call is passing two objects—they are of type ‘short array.’

B, C, D, E, F, and G are incorrect based on the above

Question 9:

C and F are correct. da refers to an object of type “TestGarbageCollection array,” and each TestGarbageCollection object that is created comes with its own “int array” object. When line 14 is reached, only the second TestGarbageCollection object (and its “int array” object) are not reachable

A, B, D, E, and G are incorrect based on the above.

Question 10:

E is correct. In go1() the local variable x is not initialized

A, B, C, D, and F are incorrect based on the above.

Previous Chapter: Quick Review - Chapters 15 to 21

Next Chapter: Chapter 22: Assignment Operator

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers