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.
Which of these words belong to the set of Java keywords?
| a. | next |
| b. | catch |
| c. | function |
| d. | instanceof |
| e. | mod |
| f. | const |
| g. | or |
| h. | Boolean |
| i. | goto |
| j. | import |
| k. | transient |
class MCZ16 {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
class Red {
public int a; public static int b;
public static void main (String[] in) {
Red r1 = new Red(), r2 = new Red(); r1.a++; r1.b++;
System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0, 0, 0, 0 |
| b. | Prints: 0, 1, 1, 1 |
| c. | Prints: 1, 1, 1, 0 |
| d. | Prints: 1, 1, 0, 1 |
| e. | Prints: 1, 1, 0, 0 |
| f. | Prints: 1, 1, 1, 1 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Which of these words belong to the set of Java keywords?
| a. | byte |
| b. | short |
| c. | int |
| d. | long |
| e. | decimal |
| f. | int64 |
| g. | float |
| h. | single |
| i. | double |
| j. | boolean |
| k. | char |
| l. | unsigned |
| m. | array |
| n. | string |
class MCZ17 {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\u000a"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
Which of the following is a modifier that can be applied to a field declaration within an interface?
| a. | static |
| b. | synchronized |
| c. | transient |
| d. | volatile |
| e. | None of the above. |
class EBH014 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((y % x) + ",");
System.out.print(y == ((y/x)*x + (y%x)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,true |
| b. | Prints: 2,true |
| c. | Prints: 1,false |
| d. | Prints: 2,false |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class GFC505 extends GFC504 {
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
class GFC504 extends GFC503 {
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC503 {String name;}
Which class is tightly encapsulated?
| a. | GFC503 |
| b. | GFC504 |
| c. | GFC505 |
| d. | None of the above |
class MWC108 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.append("D");
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: A B C |
| c. | Prints: ABCD |
| d. | Prints: ABDC |
| e. | Prints: A B CD |
| f. | Prints: A B DC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class SRC111 {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(char i) {return "char";}
static String m(int i) {return "int";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0; short s = 0; char c = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(s,s))+",");
System.out.print(m(Math.min(c,c)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: byte,byte,byte |
| b. | Prints: short,short,short |
| c. | Prints: int,int,int |
| d. | Prints: byte,short,int |
| e. | Prints: short,short,int |
| f. | Prints: byte,short,char |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Which of the following methods of the java.lang.Integer class returns a primitive int type?
| a. | intValue |
| b. | parseInt |
| c. | valueOf |
Which methods of the java.lang.Double class declare a NumberFormatException in the throws clause?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
| • | Entries are not organized as key/value pairs. | |
| • | Generally accepts duplicate elements. | |
| • | Entries may be accessed by means of an index. |
Which interface of the java.util package offers the specified behavior?
| a. | List |
| b. | Map |
| c. | Set |
| d. | None of the above |
import java.util.*;
import java.io.Serializable;
class GFC108 {
public static void main (String args[]) {
HashMap a = new HashMap();
boolean b1, b2, b3;
b1 = (a instanceof Cloneable) & (a instanceof Serializable);
b2 = a instanceof Map;
b3 = a instanceof Collection;
System.out.print(b1 + "," + b2 + "," + b3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | None of the above |
class MWC208 {
public static void main(String[] args) {
int[][] a1 = ((1,2),(3,4,5),(6,7,8,9));
System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 14 |
| b. | Prints: 16 |
| c. | Prints: 1,5,9 |
| d. | Prints: 2,4,8 |
| e. | Prints: 2,5,9 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class A {
A() {} // 1
int A; // 2
void A() {} // 3
class A {} // 4
}
Which line results in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 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 |
Which of the following modifiers can be applied to a class that is declared within an enclosing interface?
| a. | public |
| b. | protected |
| c. | private |
| d. | abstract |
| e. | static |
| f. | final |
class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
public static void main(String[] args) {
C c = new C(); c.m(); B b = c; b.m(); A a = b; a.m();
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABC |
| c. | Prints: CBA |
| d. | Prints: CCC |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class Red {
private static final int a = 10; // 1
protected static int b = 20; // 2
int c = 30; // 3
static class StaticNested {
int d = a; // 4
int e = b; // 5
int f = c; // 6
}
class NonStaticInner {
int g = a; // 7
int h = b; // 8
int i = c; // 9
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
| g. | 7 |
| h. | 8 |
| i. | 9 |
synchronized (expression) block
The synchronized statement has the form shown above. Which of the following are true statements?
| a. | A compile-time error occurs if the expression produces a value of any reference type |
| b. | A compile-time error occurs if the expression produces a value of any primitive type |
| c. | A compile-time error does not occur if the expression is of type boolean |
| d. | The sychronized block may be processed normally if the expression is null |
| e. | If execution of the block completes normally, then the lock is released |
| f. | If execution of the block completes abruptly, then the lock is released |
| g. | A thread can hold more than one lock at a time |
| h. | Synchronized statements can be nested |
| i. | Synchronized statements with identical expressions can be nested |
class EBH015 {
public static void main (String[] args) {
System.out.print((new Object() instanceof Object)+",");
System.out.print((new Object() instanceof String)+",");
System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
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 A {
private String name;
private A otherA;
public A(String name) {this.name = name;}
public void other(A otherA) {this.otherA = otherA;}
public A other() {return otherA;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class B {
public static void m1() {
A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
a1.other(a2); a2.other(a3); a3.other(a1);
for(int i = 0; i<4; i++){System.out.print(a0 = a0.other());}
}
public static void main(String[] args) {m1(); System.gc();}
}
Which of the following could be a result of attempting to compile and run the program?
| a. | A1A2A3A1 |
| b. | A0A0A0A0A1A2A3 |
| c. | A1A2A3A1A2A3 |
| d. | A1A2A3A1A1A2A3 |
| e. | A1A2A3A1A3A2A1 |
| f. | A0A1A2A3A1A2A3 |
class A {} class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a1; B b1; m(null,null); m(a1=null,b1=null); m(b1, a1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: BBABAB |
| b. | Prints: BBABBA |
| c. | Prints: BBBBAB |
| d. | Prints: BBBBBA |
| e. | Prints: BBBBBB |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
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. |
class B {
private String name;
public B(String name) {this.name = name;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class H {
static B ba = new B("Ba");
static int i = 1;
static B m1(B b) {return b = new B("B" + i++);}
public static void main (String[] args) {
B x = m1(ba); m1(x);
System.out.println(", " + ba + ", " + x);
}}
Which of the following could be a result of attempting to compile and run the program?
| a. | Ba, B1, B2 |
| b. | B1, Ba, B2 |
| c. | , Ba, B1 |
| d. | B2, Ba, B1 |
| e. | BaB1b2, null, null |
| f. | B1B2, ba, null |