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 BookSurge.com.
Are you a university student studying Java programming? Do you agree that my book would serve as a helpful workbook and companion to be used along with the Java fundamentals textbook that is currently being used in your class? If so, then please ask your professor to consider using my book in future classes.
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.
I would also like to read your response to the following questions.
Which of these words belong to the set of Java keywords?
| a. | Double |
| b. | goto |
| c. | min |
| d. | extern |
| e. | new |
| f. | signed |
| g. | finally |
| h. | const |
| i. | and |
| j. | array |
| k. | do |
A class can not be called "tightly encapsulated" unless which of the following is true?
| a. | The class is a nested class. |
| b. | The constructors are declared private. |
| c. | The mutator methods are declared private. |
| d. | The class implements the Encapsulated interface. |
| e. | None of the above |
Which of the following instance methods should only be called by a thread that holds the lock of the instance on which the method is invoked?
| a. | join |
| b. | notify |
| c. | notifyAll |
| d. | run |
| e. | start |
| f. | wait |
Which of these methods is a static member of the java.lang.String class?
| a. | append |
| b. | concat |
| c. | delete |
| d. | insert |
| e. | replace |
| f. | substring |
| g. | valueOf |
| h. | None of the above. |
class SRC107 {
public static void main (String[] args) {
int a = -1; long b = -2;
float c = -3.0f; double d = -4.0;
a = Math.abs(a); b = Math.abs(b);
c = Math.abs(c); d = Math.abs(d);
System.out.print(a+b+c+d);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 10.0 |
| b. | Compile-time error |
| c. | Run-time error |
| d. | None of the above |
class MWC205 {
static void m1(StringBuffer s1) {
s1.append("B"); System.out.print(s1);
}
static void m2(StringBuffer s1) {
s1 = new StringBuffer("C"); System.out.print(s1);
}
public static void main(String[] s) {
StringBuffer s1 = new StringBuffer("A");
m1(s1); m2(s1);
System.out.print(s1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABCA |
| c. | Prints: ABCAB |
| d. | Prints: ABCABC |
| e. | Prints: ABCAC |
| f. | Prints: ABABCABC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class F {
public static void main (String[] args) {
System.out.print(Byte.MIN_VALUE+","+Byte.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: -128,127 |
| b. | Prints: -127,128 |
| c. | Prints: 0,255 |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
class D {
public static void main (String args[]) {
boolean b1 = Integer.MIN_VALUE == 0x80000000;
boolean b2 = Integer.MAX_VALUE == 0x7FFFFFFF;
System.out.print(b1 + "," + b2);
}}
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 G {
public static void main (String[] args) {
Long l1 = new Long("1"), l2 = new Long("1");
int h1 = l1.hashCode(), h2 = l2.hashCode();
StringBuffer s1 = new StringBuffer("1");
StringBuffer s2 = new StringBuffer("1");
int h3 = s1.hashCode(), h4 = s2.hashCode();
System.out.print((l1.equals(l2) & (h1==h2)) + ",");
System.out.print(s1.equals(s2) | (h3==h4));
}}
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 |
Which methods of the java.lang.Double class return a primitive value?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
class E {
static String m1(boolean b) {return "b";}
static String m1(Boolean b) {return "B";}
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
System.out.print(m1(Boolean.valueOf(null)));
System.out.print(m1(b1.booleanValue()));
System.out.println(m1(Boolean.TRUE));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: bbb |
| b. | Prints: bbB |
| c. | Prints: bBb |
| d. | Prints: bBB |
| e. | Prints: Bbb |
| f. | Prints: BbB |
| g. | Prints: BBb |
| h. | Prints: BBB |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class F {
static String m(float f) {return "f";}
static String m(Float f) {return "F";}
public static void main (String[] args) {
Float f1 = new Float(1);
System.out.print(m(f1.parseFloat("1")));
System.out.print(m(f1.floatValue()));
System.out.print(m(f1.valueOf("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: fff |
| b. | Prints: ffF |
| c. | Prints: fFf |
| d. | Prints: fFF |
| e. | Prints: Fff |
| f. | Prints: FfF |
| g. | Prints: FFf |
| h. | Prints: FFF |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
| • | Entries are not organized as key/value pairs. | |
| • | Duplicate entries are rejected. |
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.*;
class GFC105 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}}
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 |
Which of the following classes override both the equals and hashCode methods?
| a. | java.lang.Byte |
| b. | java.lang.Integer |
| c. | java.util.Vector |
| d. | java.lang.String |
| e. | java.lang.StringBuffer |
class JSC103 {
transient float e = 1; // 1
volatile float f = 1; // 2
abstract float j = 1; // 3
final float g = 1; // 4
private final float k = 1; // 5
private transient float l = 1; // 6
volatile final float m = 1; // 7
}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
| g. | 7 |
Which of the following statements are true?
| a. | If an accessible superclass method is static, then any method with the same signature in a subclass must also be static. |
| b. | If a superclass method is synchronized, then the overriding method must also be synchronized. |
| c. | If a superclass method is public, then the overriding method must also be public. |
| d. | If a superclass method is native, then the overriding method must also be native. |
| e. | If a superclass method is protected, then the overriding method must be protected or public. |
protected class D {} // 1
synchronized class F {} // 2
volatile class H {} // 3
final class B {} // 4
Suppose these are top-level class declarations and not nested class declarations. Which of these declarations would not produce a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
class Z {
static class F {} // 1
synchronized class G {} // 2
transient class H {} // 3
volatile class I {} // 4
}
Which class declaration does not result in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 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 T {
private int i1, i2;
void printI1I2() {System.out.print("T, i1="+i1+", i2="+i2);}
T(int i1, int i2) {this.i1=i1; this.i2=i2;}
}
class U extends T {
private int i1, i2;
void printI1I2() {System.out.print("U, i1="+i1+", i2="+i2);}
U(int i1, int i2) {this.i1=i1; this.i2=i2;}
public static void main(String[] args) {
T t = new U(1,2); t.printI1I2();
}}
What is the result of attempting to compile and run the program?
| a. | Prints: U, i1=1, i2=2 |
| b. | Prints: T, i1=1, i2=2 |
| c. | Prints: U, i1=null, i2=null |
| d. | Prints: T, i1=null, i2=null |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class F {
public void m1() {Z.m1();} // 1
private static class Y {
private static void m1() {
System.out.print("Y.m1 ");
}}
private static class Z {
private static void m1(){
System.out.print("Z.m1 ");
Y.m1(); // 2
}}
public static void main(String[] args) {
new F().m1();
}}
What is the result of attempting to compile and run the program?
| a. | Compile-time error at line 1 |
| b. | Compile-time error at line 2 |
| c. | Run-time error at line 1 |
| d. | Run-time error at line 2 |
| e. | Prints: Z.m1 Y.m1 |
| f. | None of the above |
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A a1 = new A(); B b1 = new B(); C c1 = new C(); A c2 = new C();
c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABC |
| c. | Prints: CCC |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
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 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 I {
private String name;
protected void finalize() {System.out.print(name);}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
System.gc();
}}
After method m1 returns, the object created on which line is not eligible for garbage collection?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
| f. | Compile-time error |
| g. | Run-time error |
class A11 {public String toString() {return "A11";}}
class A12 {
public static void main(String[] arg) {
A11[] a1 = new A11[1]; // 1
A11[][] a2 = new A11[2][]; // 2
A11[][][] a3 = new A11[3][][]; // 3
a1[0] = new A11(); // 4
a2[0] = a2[1] = a1; // 5
a3[0] = a3[1] = a3[2] = a2; // 6
System.out.print(a3[2][1][0]); // 7
}}
What is the result of attempting to compile and run the program?
| a. | Prints: null |
| b. | Prints: A11 |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
| e. | Compile-time error at 3. |
| f. | Compile-time error at 4. |
| g. | Compile-time error at 5. |
| h. | Compile-time error at 6. |
| i. | Compile-time error at 7. |
| j. | Run-time error |
| k. | None of the above |
abstract class A {
private int x = 4, y = 2;
public A(int i1, int i2) {x=i1;y=i2;}
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
public abstract int math();
}
class B {
static A a1 = new A(2,1) {public int math() {return x()+y();}};
static A a2 = new A(2,1) {public int math() {return x()-y();}};
static A a3 = new A(2,1) {public int math() {return x()*y();}};
static A a4 = new A(2,1) {public int math() {return x()/y();}};
public static void main(String[] args) {
System.out.print("" + a1.math() + a2.math() +
a3.math() + a4.math());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 8 |
| b. | Prints: 3122 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |