Self Test 3

February 3, 2009

1.      Given:
class Scoop {
static int thrower() throws Exception { return 42; }
public static void main(String [] args) {
try {
int x = thrower();
} catch (Exception e) {
X++;
} finally {
System.out.printIn(“x = ” + ++x);
} } }
What is the result?
A.    x = 42
B.    x = 43
C.    x = 44
D.    Compilation fails.
E.    The code runs with no output.

2.      Given:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard 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.

3.      Given:
class Alien {
String invade(short ships) { return “a few”; }
String invade(short… ships) { return “many”; }
}
class Defender {
public static void main(String [] args) {
System.out.println(new Alien().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.

4.      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.

5.      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

6.      Given:
class Mixer {
Mixer() { }
Mixer(Mixer m) { ml = 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

7.      Given:
1. class Zippy {
2.   String[] x;
3.   int[] a [] = {{1,2}, {l}};
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.

8.      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.

9.      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.

10.      Which is true? (Choose all that apply.)
A.    The invocation of an object’s finalize() method is always the last thing that happens before an object is garbage collected (GCed).
B.    When a stack variable goes out of scope it is eligible for GC.
C.    Some reference variables live on the stack, and some live on the heap.
D.    Only objects that have no reference variables referring to them can be eligible for GC.
E.    It’s possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.

11.      Given:
1. class Convert {
2.   public static void main(String[] args) {
3.     Long xL = new Long(456L);
4.     long x1 = Long.valueOf(“123”);
5.     Long x2 = Long.valueOf(“123”);
6.     long x3 = xL.longValue();
7.     Long x4 = xL.longValue();
8.     Long x5 = Long.parseLong(“456”);
9.     long x6 = Long.parseLong(“123”);
10.  }
11. }

Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)
A.    Line 4
B.    Line 5
C.    Line 6
D.    Line 7
E.    Line 8
F.    Line 9

12.      Given:
1. class Eco {
2.   public static void main(String[] args)    {
3.     Eco e1 = new Eco();
4.     Eco e2 = new Eco();
5.     Eco e3 = new Eco();
6.     e3.e = e2;
7.     e1.e = e3;
8.     e2 = null;
9.     e3 = null;
10.    e2.e = el;
11.    e1   =  null;
12.  }
13.  Eco e;
14. }
At what point is only a single object eligible for GC?
A.    After line 8 runs.
B.    After line 9 runs.
C.    After line 10 runs.
D.    After line 11 runs.
E.    Compilation fails.
F.    Never in this program.
G.    An exception is thrown at runtime.

13.      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;

14.      Given:
class Bird {
{ System.out.print(“bl “); }
public Bird() { System.out.print(“b2 “); }
}
class Raptor extends Bird {
static { System.out.print(“r1 “); }
public Raptor() { System.out.print(“r2 “); }
{ System.out.print(“r3 “); }
static { System.out.print(“r4 “); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print(“pre “);
new Hawk();
System.out.println(“hawk “);
}
}
What is the result?
A.    pre b1 b2 r3 r2 hawk
B.    pre b2 b1 r2 r3 hawk
C.    pre b2 b1 r2 r3 hawk r1 r4
D.    r1 r4 pre b1 b2 r3 r2 hawk
E.    r1 r4 pre b2 b1 r2 r3 hawk
F.    pre r1 r4 b1 b2 r3 r2 hawk
G.    pre r1 r4 b2 b1 r2 r3 hawk
H.    The order of output cannot be predicted.
I.    Compilation fails.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: