Friday, October 26, 2007

Java interview questions Part27


Given:

1. class Crivitch {
2. public static void main(String [] args) {
3. int x = 0;
4. // insert code here
5. do { } while (x++ < y);
6. System.out.println(x);
7. }
8. }


Which, inserted at line 4, produces the output 12?

int y = x;

int y = 10;

int y = 11;

int y = 12;

int y = 13;

None of the above will allow compilation to succeed.


6.
Given:

class Plane {
static String s = "-";
public static void main(String[] args) {
new Plane().s1() ;
System.out.println(s);
}
void sl() {
try { s2();
catch (Exception e) { s += "c"; }
}
void s2() throws Exception {
s3(); s += "2";
s3(); s += "2b";
}
void s3() throws Exception {
throw new Exception();
}
}

What is the result?

-

-c

-c2

-2c

-c22b

-2c2b

-2c2bc

Compilation fails.


7.
Given:

try { int x = Integer.parselnt("two"); }

Which could be used to create an appropriate catch block? (Choose all that apply.)

ClassCastException

IllegalStateException

NumberFormatException

IllegalArgumentException

ExceptionInInitializerError

ArrayIndexOutOfBoundsException


8.
Given:

1. class Ping extends Utils {
2. public static void main(String [] args) {
3. Utils u = new Ping();
4. System.out.print(u.getInt(args[0]));
5. }
6. int getInt(String arg) {
7. return Integer.parseInt(arg);
8. }
9. }
10. class Utils {
11. int getInt(String x) throws Exception { return 7; }
12. }

And the following three possible changes:

C1. Declare that main() throws an Exception.

C2. Declare that Ping.getInt() throws an Exception.

C3. Wrap the invocation of getInt() in a try / catch block.

Which change(s) allow the code to compile? (Choose all that apply.)

Just C1 is sufficient.

Just C2 is sufficient.

Just C3 is sufficient.

Both C1 and C2 are required.

Both C1 and C3 are required.

Both C2 and C3 are required.

All three changes are required.


9.
Given:

class Swill {
public static void main(String[] args) {
String s = "-";
switch(TimeZone.CST) {
case EST: s += "e";
case CST: s += "c";
case MST: s += "m";
default: s += "X";
case PST: s += "p";
}
System.out.println(s);
}
}
enum TimeZone {EST, CST, MST, PST }

What is the result?

-c

-X

-cm

-cmp

-cmXp

Compilation fails.

An exception is thrown at runtime.


10.
Given:

class Circus {
public static void main(String[] args) {
int x = 9;
int y = 6;
for(int z = 0; z < 6; z++, y--) {
if(x > 2) x--;
label:
if(x > 5) {
System.out.print(x + " "};
--X;
continue label;

}
X--;
}
}
}

What is the result?

8

8 7

8 7 6

Compilation fails.

An exception is thrown at runtime.


11.
Which are true? (Choose all that apply.)

It is appropriate to use assertions to validate arguments to methods marked public.

It is appropriate to catch and handle assertion errors.

It is NOT appropriate to use assertions to validate command-line arguments.

It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.

It is NOT appropriate for assertions to change a program's state.


12.
Given:

1. class Loopy {
2. public static void main(String[] args) {
3. int[] x = {7,6,5,4,3,2,1};
4. // insert code here
5. System.out.print(y + " ");
6. }
7. }
8. }

Which, inserted independently at line 4, compiles? (Choose all that apply.)

for(int y : x) {

for(x : Int y) {

int y = 0; for(y : x) {

for(int y=0, z=0; z
for(int y=0, int z=0, int z=0; z
int y = 0; for(int z=0; z

13.
Given:

1. class Ring {
2. final static int x2 = 7;
3. final static Integer x4 = 8;
4. public static void main(String[] args) {
5. Integer x1 = 5;
6. String s = "a";
7. if(xl < 9) s += "b";
8. switch(x1) {
9. case 5: s += "c";
10. case x2: s += "d";
11. case x4: s += "e";
12. }
13. System.out.println(s);
14. }
15. }

What is the result?

abc

abcde

Compilation fails due only to an error on line 7.

Compilation fails due only to an error on line 8.

Compilation fails due only to an error on line 10.

Compilation fails due only to an error on line 11.

Compilation fails due to errors on multiple lines.

0 comments:

Advertisement

 

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