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 cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.
class JMM107 {
public static void main(String[] args) {
boolean b = true;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else {System.out.print("C");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,1,0,0 |
| b. | Prints: 1,1,0,0 |
| c. | Prints: 0,1,1,0 |
| d. | Prints: 1,1,1,0 |
| e. | Prints: 1,1,1,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class JMM108 {
static boolean b;
public static void main(String[] args) {
if (b) {System.out.print("A");
} else if (b = false) {System.out.print("B");
} else if (b) {System.out.print("C");
} else if (!b) {System.out.print("D");
} else {System.out.print("E");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Prints: D |
| e. | Prints: E |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new ColorException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,1,0,0 |
| b. | Prints: 1,1,0,1 |
| c. | Prints: 0,1,0,1 |
| d. | Prints: 0,1,1,1 |
| e. | Prints: 1,1,1,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class JMM109 {
public static void main(String[] args) {
boolean b;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else if (!b) {System.out.print("C");
} else {System.out.print("D");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Prints: D |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (WhiteException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,1,0,0 |
| b. | Prints: 1,1,0,1 |
| c. | Prints: 0,1,0,1 |
| d. | Prints: 0,1,1,1 |
| e. | Prints: 1,1,1,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 Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 1;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,1,1 |
| b. | Prints: 0,0,1,1,1 |
| c. | Prints: 0,1,1,1,1 |
| d. | Prints: 1,1,1,1,1 |
| e. | Prints: 0,0,1,0,1 |
| f. | Prints: 0,1,0,0,1 |
| g. | Prints: 1,0,0,0,1 |
| h. | Compile-time error |
| i. | Run-time error |
| j. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 2;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,1,1 |
| b. | Prints: 0,0,1,1,1 |
| c. | Prints: 0,1,1,1,1 |
| d. | Prints: 1,1,1,1,1 |
| e. | Prints: 0,0,1,0,1 |
| f. | Prints: 0,1,0,0,1 |
| g. | Prints: 1,0,0,0,1 |
| h. | Compile-time error |
| i. | Run-time error |
| j. | None of the above |
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 4;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,1,1 |
| b. | Prints: 0,0,1,1,1 |
| c. | Prints: 0,1,1,1,1 |
| d. | Prints: 1,1,1,1,1 |
| e. | Prints: 0,0,1,0,1 |
| f. | Prints: 0,1,0,0,1 |
| g. | Prints: 1,0,0,0,1 |
| h. | Compile-time error |
| i. | Run-time error |
| j. | None of the above |
class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
abstract void m1() throws ColorException;
}
class White extends Color {
void m1() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,c; a = b = c = 0;
try {white.m1(); a++;}
catch (WhiteException e) {b++;}
finally {c++;}
System.out.print(a+","+b+","+c);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0 |
| b. | Prints: 0,0,1 |
| c. | Prints: 0,1,0 |
| d. | Prints: 0,1,1 |
| e. | Prints: 1,0,0 |
| f. | Prints: 1,0,1 |
| g. | Prints: 1,1,0 |
| h. | Prints: 1,1,1 |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
if (!b2 & !b1) {System.out.print("A");
} else if (!b2 & b1) {System.out.print("B");
} else if (b2 & !b1) {System.out.print("C");
} else {assert false;}
}
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}
Which statements are true?
| a. | With assertions enabled it prints an AssertionError message. |
| b. | With assertions enabled it prints nothing. |
| c. | With assertions disabled it prints an AssertionError message. |
| d. | With assertions disabled it prints nothing. |
| e. | An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions. |
| f. | The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program. |
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}
Which statements are true?
| a. | If assertions are enabled at run time it prints an error message. |
| b. | With assertions enabled it prints nothing. |
| c. | With assertions disabled it prints an error message. |
| d. | With assertions disabled it prints 1112. |
| e. | With assertions disabled it prints nothing. |
| f. | The assert statements are being used to check a precondition--something that must be true when the method is invoked. |
| g. | Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method. |
| h. | Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method. |
class B {
int a, b, c;
private void setA(int i) {a = i;}
private void setB(int i) {b = i;}
private int m1 (int i) {
c = a + b + i; assert c < 200 : c; return c;
}
public static void main (String[] args) {
B b = new B(); b.setA(50); b.setB(100); b.m1(50);
}}
Which statements are true?
| a. | If assertions are not enabled at run time it prints an error message. |
| b. | If assertions are not enabled at run time it prints nothing. |
| c. | With assertions enabled it prints an error message. |
| d. | With assertions enabled it prints nothing. |
| e. | The assert statement is being used to check a postcondition--something that must be true when the method completes successfully. |
class C {
int a, b, c;
public void setA(int i) {a = i; assert validateC() : c;}
public void setB(int i) {b = i; assert validateC() : c;}
private boolean validateC() {
return c > a + 2 * b;
}
public int m1(int i) {
c = a + b + i;
assert validateC() : c;
return c;
}
public C(int i) {
c = i; assert validateC() : c;
}
public static void main(String[] args) {
C c = new C(251); c.setA(50); c.setB(100);
}}
Which statements are true?
| a. | If assertions are not enabled at run time it prints an error message. |
| b. | If assertions are not enabled at run time it prints nothing. |
| c. | With assertions enabled it prints an error message. |
| d. | With assertions enabled it prints nothing. |
| e. | The assert statement is being used to check a class invariant--something that must be true about each instance of the class. |
| f. | The assert statements are being used to check a precondition--something that must be true when the method is invoked. |
class E {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public void m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {System.out.print("A");
} else if (!b3 & !b2 & b1) {System.out.print("B");
} else if (!b3 & b2 & !b1) {System.out.print("C");
} else { // Only b3 is true.
assert b3 & !b2 & !b1;
}
System.out.print(b1 + "," + b2 + "," + b3);
b1 = b2 = b3 = false;
}
public static void main (String[] args) {
E e = new E(); e.setB1(true); e.m1(2);
}}
Which statements are true?
| a. | With assertions enabled it prints an error message. |
| b. | With assertions enabled it prints: true,true,false |
| c. | With assertions disabled it prints an error message. |
| d. | With assertions disabled it prints: true,true,false |
| e. | With assertions disabled it prints nothing. |
| f. | The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2 or b3, to be true. |
| g. | The assert statement is being used to check a precondition--something that must be true when the method begins. |
| h. | The assert statement is being used to check an internal invariant--something that the programmer assumes to be true at a particular point in the program. |
class F {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public String m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {return "None are true.";
} else if (!b3 & !b2 & b1) {return "Only b1 is true.";
} else if (!b3 & b2 & !b1) {return "Only b2 is true.";
} else if (b3 & !b2 & !b1) {return "Only b3 is true.";
} else {throw new AssertionError();}
}
public static void main (String[] args) {
F f = new F();
f.setB1(true);
System.out.println(f.m1(5));
System.out.println(f.m1(6));
}}
Which statements are true?
| a. | Prints "Only b1 is true" followed by an error message. |
| b. | An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions. |
| c. | The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2, or b3, to be true. |
| d. | The assert statement is being used to check a precondition--something that must be true when the method begins. |
| e. | The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program. |
| f. | The throw statement could be replaced by an assert statement. |
An assert statement can be used to check a control-flow invariant to verify which of the following?
| a. | A particular assumption is true when the flow of control enters a method. |
| b. | The flow of control does not reach a particular point in the program. |
| c. | The normal flow of control has reached a particular point in the program. |
| d. | The normal flow of control has reached the end of a method. |
| e. | The default case of a switch statement is not reached. |
| f. | The else block of an if/else statement is not reached. |
class JMM110 {
public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2;)
System.out.print(i);
while (j++ < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
class JMM111 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i < 2; i++) do
System.out.print(i);
while (j++ < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
class JMM113 {
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |