Friday, October 26, 2007

Java interview questions Part22


Given the following code:

public class OrtegorumFunction {
public int computeDiscontinuous(int x) {
int r = 1;
r += X;
if ((x > 4) && (x < 10)) {
r += 2 * x;
} else (x <= 4) {
r += 3 * x;
} else {
r += 4 * x;
}
r += 5 * x;
return r;
}

public static void main(String [] args) {
OrtegorumFunction o = new OrtegorumFunction();
System.out.println("OF(11) is: " + o.computeDiscontinuous (11));
}
}

What is the result?

OF(11) is: 45

OF(11) is: 56

OF(11) is: 89

OF(11) is: 111

Compilation fails.

An exception is thrown at runtime.


2.
Given two files:

1. class One {
2. public static void main(String[] args) {
3. int assert = 0;
4. }
5. }

1. class Two {
2. public static void main(String[] args) {

3. assert(false);
4. }
5. }

And the four command-line invocations:

javac -source 1.3 One.java
javac -source 1.4 One.java
javac -source 1.3 Two.java
javac -source 1.4 Two.java

What is the result? (Choose all that apply.)

Only one compilation will succeed.

Exactly two compilations will succeed.

Exactly three compilations will succeed.

All four compilations will succeed.

No compiler warnings will be produced.

At least one compiler warning will be produced.


3.
Given:

import java.io.*;
class Master {
String doFileStuff() throws FileNotFoundException { return "a"; }
}
class Slave extends Master {
public static void main(String[] args) {
String s = null;
try { s = new Slave().doFileStuff();
} catch ( Exception x) {
s = "b"; }
System.out.println(s);
}
// insert code here
}

Which, inserted independently at // insert code here, will compile, and produce the output b? (Choose all that apply.)

String doFileStuff() { return "b"; }

String doFileStuff() throws IOException ( return "b"; }

String doFileStuff(int. x) throws IOException ( return "b"; }

String doFileStuff() throws FileNotFoundException { return "b"; }

String doFileStuff() throws NumberFormatException { return "b"; }

String doFileStuff() throws NumberFormatException, FileNotFoundException { return "b"; }


4.
Given:

class Input {
public static void main(String[] args) {
String s = "- " ;
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}
public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
} }

And the command-line invocations:

java Input
java Input 0

Which are true? (Choose all that apply.)

Line 6 is executed exactly 0 times.

Line 6 is executed exactly 1 time.

Line 6 is executed exactly 2 times.

The finally block is executed exactly 0 times.

The finally block is executed exactly 1 time.

The finally block is executed exactly 2 times.

Both invocations produce the same exceptions.

Each invocation produces a different exception.

0 comments:

Advertisement

 

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