Friday, October 26, 2007

Java interview questions Part22


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?

hi

hi hi

hi hi hi

Compilation fails

hi, followed by an exception

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?

Compilation succeeds.

Compilation fails due only to an error on line 3.

Compilation fails due only to an error on line 4.

Compilation fails due only to an error on line 5.

Compilation fails due to errors on lines 3 and 5.

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?

true true

false true

true false

false false

Compilation fails.

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?

343 340 340

343 340 342

343 341 342

343 341 340

343 341 343

Compilation fails.

An exception is thrown at runtime.


10.
Which is true? (Choose all that apply.)

The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

When a stack variable goes out of scope it is eligible for GC.

Some reference variables live on the stack, and some live on the heap.

Only objects that have no reference variables referring to them can be eligible for GC.

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

Line 4

Line 5

Line 6

Line 7

Line 8

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?

After line 8 runs.

After line 9 runs.

After line 10 runs.

After line 11 runs.

Compilation fails.

Never in this program.

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

Faster f = Faster.Higher;

Faster f = Better.Faster.Higher;

Better.Faster f = Better.Faster.Higher;

Bigger.Faster f = Bigger.Faster.Higher;

Better. Faster f2; f2 = Better.Faster.Longer;

Better b; b.Faster = f3; f3 = Better.Faster.Longer;

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com