Self Test 5

February 3, 2009

1.      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?
A.    OF(11) is: 45
B.    OF(11) is: 56
C.    OF(11) is: 89
D.    OF(11) is: 111
E.    Compilation fails.
F.    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.)
A.    Only one compilation will succeed.
B.    Exactly two compilations will succeed.
C.    Exactly three compilations will succeed.
D.    All four compilations will succeed.
E.    No compiler warnings will be produced.
F.    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.)
A.    String doFileStuff() { return “b”; }
B.    String doFileStuff() throws IOException ( return “b”; }
C.    String doFileStuff(int. x) throws IOException ( return “b”; }
D.    String doFileStuff() throws FileNotFoundException { return “b”; }
E.    String doFileStuff() throws NumberFormatException { return “b”; }
F.    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.)
A.    Line 6 is executed exactly 0 times.
B.    Line 6 is executed exactly 1 time.
C.    Line 6 is executed exactly 2 times.
D.    The finally block is executed exactly 0 times.
E.    The finally block is executed exactly 1 time.
F.    The finally block is executed exactly 2 times.
G.    Both invocations produce the same exceptions.
H.    Each invocation produces a different exception.

5.      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?
A.    int y = x;
B.    int y = 10;
C.    int y = 11;
D.    int y = 12;
E.    int y = 13;
F.    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?
A.    –
B.    -c
C.    -c2
D.    -2c
E.    -c22b
F.    -2c2b
G.    -2c2bc
H.    Compilation fails.

7.      Given:
try { int x = Integer.parselnt(“two”); }
Which could be used to create an appropriate catch block? (Choose all that apply.)
A.    ClassCastException
B.    IllegalStateException
C.    NumberFormatException
D.    IllegalArgumentException
E.    ExceptionInInitializerError
F.    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.)
A.    Just C1 is sufficient.
B.    Just C2 is sufficient.
C.    Just C3 is sufficient.
D.    Both C1 and C2 are required.
E.    Both C1 and C3 are required.
F.    Both C2 and C3 are required.
G.    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?
A.    -c
B.    -X
C.    -cm
D.    -cmp
E.    -cmXp
F.    Compilation fails.
G.    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?
A.    8
B.    8 7
C.    8 7 6
D.    Compilation fails.
E.    An exception is thrown at runtime.

11.      Which are true? (Choose all that apply.)
A.    It is appropriate to use assertions to validate arguments to methods marked public.
B.    It is appropriate to catch and handle assertion errors.
C.    It is NOT appropriate to use assertions to validate command-line arguments.
D.    It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.
E.    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.)
A.    for(int y : x) {
B.    for(x : Int y) {
C.    int y = 0; for(y : x) {
D.    for(int y=0, z=0; z<x.length; z++) { y = x[z];
E.    for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
F.    int y = 0; for(int z=0; z<x.length; z++) { y = x[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?
A.    abc
B.    abcde
C.    Compilation fails due only to an error on line 7.
D.    Compilation fails due only to an error on line 8.
E.    Compilation fails due only to an error on line 10.
F.    Compilation fails due only to an error on line 11.
G.    Compilation fails due to errors on multiple lines.

14.      Given:
class Emu {
static String s = “-“;
public static void main(String[] args) {
try {
throw new Exception();
} catch (Exception e) {
try {
try { throw new Exception();
} catch (Exception ex) { s += “ic “; }
throw new Exception(); }
catch (Exception x) { s += “mc “; }
finally { s += “mf “; }
} finally { s += “of “; }
System.out.println(s);
} }
What is the result?
A.    -ic of
B.    -mf of
C.    -mc mf
D.    -ic mf of
E.    -ic mc mf of
F.    -ic mc of mf
G.    Compilation fails.

15.      Given:
class Mineral { }
class Gem extends Mineral { }
class Miner {
static int x = 7;
static String s = null;
public static void getWeight(Mineral m) {
int y = 0 / x;
System.out.print(s + ” “);
}
public static void main(String[] args) {
Mineral[] ma = {new Mineral(), new Gem()};

for(Object o : ma)
getWeight((Mineral) o);
}
}
And the command-line invocation:
java Miner.java
What is the result?
A.    null
B.    null null
C.    A ClassCastException is thrown.
D.    A NullPointerException is thrown.
E.    A NoClassDefFoundError is thrown.
F.    An ArithmeticException is thrown.
G.    An IllegalArgumentException is thrown.
H.    An ArrayIndexOutOfBoundsException is thrown.

16.      Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)
A.    ClassCastException
B.    IllegalStateException
C.    NumberFormatException
D.    IllegalArgumentException
E.    ExceptionInInitializerError

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: