Friday, October 26, 2007

Java interview questions Part23


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?

x = 42

x = 43

x = 44

Compilation fails.

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?

0

1

2

Compilation fails.

It is not possible to know.

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?

many

a few

Compilation fails.

The output is not predictable.

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?

2

4

An exception is thrown at runtime.

Compilation fails due to an error on line 4.

Compilation fails due to an error on line 5.

Compilation fails due to an error on line 6.

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?

1 1

2 1

3 1

4 1

2 3

3 3

4 3

0 comments:

Advertisement

 

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