Friday, October 26, 2007

Java interview questions Part26


Given:

class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}
}

What is the result?

null

life

universe

everything

Compilation fails.

An exception is thrown at runtime.


2.
Given:

1. class Example {
2. public static void main(String[] args) {
3. Short s = 15;
4. Boolean b;
5. // insert code here
6. }
7. }

Which, inserted independently at line 5, will compile? (Choose all that apply.)

b = (Number instanceof s);

b = (s instanceof Short);

b = s.instanceof(Short);

b = (s instanceof Number);

b = s.instanceof(Object);

b = (s instanceof String);


3.
Given:

1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }

And the following five code fragments:

F1. if (f1 == f2)
F2. if (f1 == f2[2][1])
F3. if (x == f2[0][0])
F4. if (f1 == f2 [1,1] )
F5. if (f3 == f2 [2] )

What is true?

One of them will compile, only one will be true.

Two of them will compile, only one will be true.

Two of them will compile, two will be true.

Three of them will compile, only one will be true.

Three of them will compile, exactly two will be true.

Three of them will compile, exactly three will be true.


4.
Given:

class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1] .equals("test")) {
System.out.println ("test case");
} else {
System.out.println("production " + args[0]) ;
}
}
}


And the command-line invocation:

java Fork live2

What is the result?

test case

production

test case live2

Compilation fails.

An exception is thrown at runtime.


5.
Given:

0 comments:

Advertisement

 

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