The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.
Today, you can find my book on the retail web site of the company that prints it and distributes it.
class A {
public static void main (String[] args) {
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + ",");
System.out.print(error instanceof Throwable);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {C() {}} // 3
Which of the following statements are true?
| a. | class A extends Object. |
| b. | Compile-time error at 1. |
| c. | Compile-time error at 2. |
| d. | Compile-time error at 3. |
class JMM102 {
public static void main(String args[]) {
for (int i = 0; i<5 ;i++) {
switch(i) {
case 0: System.out.print("v ");break;
case 1: System.out.print("w ");
case 2: System.out.print("x ");break;
case 3: System.out.print("y ");
case 4: System.out.print("z ");break;
default: System.out.print("d ");
}}}}
What is the result of attempting to compile and run the program?
| a. | Prints: v w x y z |
| b. | Prints: v w x y z d |
| c. | Prints: v w x x y z z |
| d. | Prints: v w w x y y z d |
| e. | Prints: d d d d d d |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
class A {
public static void main (String[] args) {
Object error = new Error();
Object runtimeException = new RuntimeException();
System.out.print((error instanceof Exception) + ",");
System.out.print(runtimeException instanceof Exception);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class JMM103 {
public static void main(String args[]) {
for (int i = 0; i < 5 ;i++) {
switch(i) {
0: System.out.print("v ");break;
1: System.out.print("w ");
2: System.out.print("x ");break;
3: System.out.print("y ");
4: System.out.print("z ");break;
}}}}
What is the result of attempting to compile and run the program?
| a. | Prints: v w x y z |
| b. | Prints: v w x x y z z |
| c. | Prints: v w w x y y z |
| d. | Run-time error. |
| e. | Compile-time error. |
| f. | None of the above. |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 1;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,1,0,0 |
| b. | Prints: 0,0,1,1,0,1 |
| c. | Prints: 0,1,1,1,0,1 |
| d. | Prints: 1,0,1,1,0,1 |
| e. | Prints: 1,1,1,1,0,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 2;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,1,0,0,1 |
| b. | Prints: 0,1,0,0,0,0 |
| c. | Prints: 0,1,1,0,0,1 |
| d. | Prints: 0,1,0,0,0,1 |
| e. | Prints: 1,1,1,0,0,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,1,1,0,0,1 |
| b. | Prints: 0,1,1,0,0,1 |
| c. | Prints: 0,1,0,0,0,0 |
| d. | Prints: 0,1,0,0,0,1 |
| e. | Prints: 0,0,1,0,0,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 4;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
case 4: throw new Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally{c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,0,0,1 |
| b. | Prints: 0,0,0,0,1,0 |
| c. | Prints: 0,0,1,0,0,1 |
| d. | Prints: 0,0,1,0,1,1 |
| e. | Prints: 0,1,1,1,1,1 |
| f. | Prints: 1,1,1,1,1,1 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 5;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
case 4: throw new Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,0,0,0,0,0 |
| b. | Prints: 1,0,1,0,0,1 |
| c. | Prints: 0,0,1,0,0,1 |
| d. | Prints: 1,1,1,1,1,1 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}
Which statements are true?
| a. | With assertions enabled it prints 210210-1 followed by an AssertionError message. |
| b. | With assertions enabled it prints 210210 followed by an AssertionError message. |
| c. | With assertions enabled it prints only 210210. |
| d. | With assertions enabled it prints nothing. |
| e. | With assertions disabled it prints 210210-1 |
| f. | With assertions disabled it prints only 210210 |
| g. | Assertions should not be used within the default case of a switch statement. |
class B {
private static int x1;
public void setX(int x) {x1 = x;}
public int getX() {return x1;}
public static void main(String[] args) {
int i1 = 0, j1 = 0;
do {
System.out.print(j1++);
assert (i1 = j1 + x1) < 6;
} while (i1 < 3);
}}
Assuming that the setX method is never invoked, which statements are true?
| a. | With assertions enabled it prints 012345 followed by an AssertionError message. |
| b. | With assertions enabled it prints 012. |
| c. | With assertions disabled it prints: 012345 |
| d. | With assertions disabled it prints: 012 |
| e. | With assertions disabled it attempts to print an infinite sequence of numbers. |
| f. | With assertions disabled the variable i1 is incremented with each pass through the loop. |
| g. | As a rule, the boolean expression of an assert statement should not be used to perform actions that are required for normal operation of the program. |
Which statements are true?
| a. | Assertions should not be used to validate arguments passed to public methods. |
| b. | Assertions should not be used to validate arguments passed to nonpublic methods. |
| c. | Assertions should not be used within the default case of a switch statement. |
| d. | Assertions should not be used to perform processing that is required for the normal operation of the application. |
Which statements are true?
| a. | Assertions should never be placed at any point in the code that should not be reached under normal circumstances. |
| b. | The compiler will generate an error if an assert statement is "unreachable" as defined by the Java Language Specification. |
| c. | A catch clause should not be used to catch an AssertionError. |
| d. | AssertionError is a checked exception. |
class JMM103 {
public static void main(String args[]) {
byte b = -1;
switch(b) {
case 0: System.out.print("zero "); break;
case 100: System.out.print("100 "); break;
case 1000: System.out.print("1000 "); break;
default: System.out.print("Default ");
}}}
What is the result of attempting to compile and run the program?
| a. | Prints: zero |
| b. | Prints: 100 |
| c. | Prints: 1000 |
| d. | Prints: Default |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?
| a. | With assertions enabled it prints ABC followed by an AssertionError message. |
| b. | With assertions disabled it prints ABC followed by an AssertionError message. |
| c. | Assertions should not be used within the default case of a switch statement. |
| d. | In this code example an assert statement could not be used in place of the "throw" statement. |
class JMM104 {
public static void main (String args[]) {
char c = 'b';
switch(c) {
case 'a': System.out.print("1");
case 'b': System.out.print("2");
case 'c': System.out.print("3");
default: System.out.print("4");
}}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3 |
| b. | Prints: 34 |
| c. | Prints: 234 |
| d. | Prints: 1234 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
return "E";
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?
| a. | With assertions enabled it prints ABC followed by an AssertionError message. |
| b. | With assertions enabled it prints ABCE followed by an AssertionError message. |
| c. | With assertions disabled it prints ABC |
| d. | With assertions disabled it prints ABCE |
| e. | Assertions should not be used within the default case of a switch statement. |
class JMM105 {
public static void main(String args[]) {
int x = 6; int success = 0;
do {
switch(x) {
case 0: System.out.print("0"); x += 5; break;
case 1: System.out.print("1"); x += 3; break;
case 2: System.out.print("2"); x += 1; break;
case 3: System.out.print("3"); success++; break;
case 4: System.out.print("4"); x -= 1; break;
case 5: System.out.print("5"); x -= 4; break;
case 6: System.out.print("6"); x -= 5; break;
}
} while ((x != 3) || (success < 2));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 60514233 |
| b. | Prints: 6152433 |
| c. | Prints: 61433 |
| d. | Prints: 6143 |
| e. | Run-time error |
| f. | Compile-time error |
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?
| a. | With assertions enabled it prints ABC followed by an AssertionError message. |
| b. | With assertions disabled it prints ABC followed by an AssertionError message. |
| c. | Assertions should not be used within the default case of a switch statement. |
| d. | In this code example a throw statement must be used in place of the assert statement. |
| e. | Compile-time error |
class JMM106 {
public static void main(String args[]) {
int x = -5; int success = 0;
do {
switch(x) {
case 0: System.out.print("0"); x += 5; break;
case 1: System.out.print("1"); x += 3; break;
case 2: System.out.print("2"); x += 1; break;
case 3: System.out.print("3"); success++; break;
case 4: System.out.print("4"); x -= 1; break;
case 5: System.out.print("5"); x -= 4; break;
case 6: System.out.print("6"); x -= 5; break;
default: x += x < 0 ? 2 : -2;
}
} while ((x != 3) || (success < 2));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 60514233 |
| b. | Prints: 1433 |
| c. | Prints: 61433 |
| d. | Prints: 051433 |
| e. | Run-time error |
| f. | Compile-time error |
class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}
java JMM101 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
| a. | Prints: JMM101ABCDEF |
| b. | Prints: ABCDEF |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |