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.
class Basics {
int x = 1, y = 2;
public static void main (String[] args) {System.out.println(x+y);}
}
What is the result of attempting to compile and run the program?
| a. | Prints: x+y |
| b. | Prints: 12 |
| c. | Prints: 3 |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
Which of these words belongs to the set of Java keywords?
| a. | clause |
| b. | loop |
| c. | expression |
| d. | overrides |
| e. | statement |
| f. | assertion |
| g. | validate |
| h. | exception |
| i. | cast |
| j. | None of the above |
class MCZ18 {
public static void main (String[] args) {
String a = "abcd"; // 1
String b = "'\u0041'"; // 2
String c = "\u0041"; // 3
String d = "\uD7AF"; // 4
System.out.print(a+b+c+d); // 5
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | None of the above |
class GFC506 {private String name;}
class GFC507 extends GFC506 {
String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
class GFC508 extends GFC506 {
private String name;
public GFC508(String name) {setName(name);}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
Which class is not tightly encapsulated?
| a. | GFC506 |
| b. | GFC507 |
| c. | GFC508 |
| d. | None of the above |
class SRC112 {
static String m(byte i) {return "byte";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(b,1))+",");
System.out.print(m(Math.min(b,1L)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: byte,byte,byte |
| b. | Prints: byte,int,long |
| c. | Prints: int,int,int |
| d. | Prints: int,int,long |
| e. | Prints: long,long,long |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class F {
public static void main (String[] args) {
Integer i1 = new Integer("1");
Integer i2 = new Integer("1");
int h1 = i1.hashCode(), h2 = i2.hashCode();
StringBuffer sb1 = new StringBuffer("1");
StringBuffer sb2 = new StringBuffer("1");
int h3 = sb1.hashCode(), h4 = sb2.hashCode();
System.out.print((h1==h2)+","+(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 |
class F {
public static void main (String args[]) {
Double d1 = new Double("0x10"); // 1
double d2 = Double.parseDouble("0x10"); // 2
Double d3 = Double.valueOf("0x10"); // 3
System.out.print(d1+","+d2+","+d3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 10,10,10 |
| b. | Prints: 16,16,16 |
| c. | Compile-time error at line 1 |
| d. | Compile-time error at line 2 |
| e. | Compile-time error at line 3 |
| f. | Run-time error |
| g. | None of the above |
Which of the following classes allow unsynchronized read operations by multiple threads?
| a. | Vector |
| b. | Hashtable |
| c. | TreeMap |
| d. | TreeSet |
| e. | HashMap |
| f. | HashSet |
class MWC209 {
public static void main(String[] args) {
int[][] a1 = [[1,2],[3,4,5],[6,7,8,9]];
int[][] a2 = [[1,2],[3,4,5],[6,7,8,9]];
int[][] a3 = [[1,2],[3,4,5],[6,7,8,9]];
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[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 {
int B; // 1
void B() {} // 2
class B {} // 3
}
Which line results in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 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 |
interface A {
int a = 1; // 1
public int b = 2; // 2
public static int c = 3; // 3
public static final int d = 4; // 4
}
Which field declaration results in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}}
What is the result of attempting to compile and run the program?
| a. | false,false,false |
| b. | false,false,true |
| c. | false,true,false |
| d. | false,true,true |
| e. | true,false,false |
| f. | true,false,true |
| g. | true,true,false |
| h. | true,true,true |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
class A {void m1(String s1) {}}
class B extends A {
void m1(String s1) {} // 1
void m1(boolean b) {} // 2
void m1(byte b) throws Exception {} // 3
String m1(short s) {return new String();} //4
private void m1(char c) {} // 5
protected void m1(int i) {} // 6
}
What is the result of attempting to compile the program?
| a. | Compile-time error at line 1 |
| b. | Compile-time error at line 2 |
| c. | Compile-time error at line 3 |
| d. | Compile-time error at line 4 |
| e. | Compile-time error at line 5 |
| f. | Compile-time error at line 6 |
| g. | None of the above |
class MWC110 {
static void m1(String s1, String s2) {
s1.insert(1,"D"); s1 = s1 + s2;
}
public static void main(String[] s) {
String s1 = "A", s2 = "B";
m1(s1,s2);
System.out.print(s1 + s2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AB |
| b. | Prints: ABB |
| c. | Prints: ADB |
| d. | Prints: ADBB |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class SRC113 {
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(long i) {return "long";}
static String m(float i) {return "float";}
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,1L))+",");
System.out.print(m(Math.min(s,1.0f))+",");
System.out.print(m(Math.min(c,1.0)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: int,int,int |
| b. | Prints: byte,short,char |
| c. | Prints: long,float,double |
| d. | Prints: double,double,double |
| e. | Prints: long,long,long |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
static void m1(A x) {System.out.print(x.s1);}
static void m1(B x) {System.out.print(x.s1);}
static void m1(C x) {System.out.print(x.s1);}
public static void main(String[] args) {
A a; B b; C c; a = b = c = new C(); m1(a); m1(b); m1(c);
}}
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 {
static class StaticNested {interface ABC {}} // 1
class NonStaticInner {interface DEF {}} // 2
interface GHI {} // 3
}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | None of the above |
Which of the following is a true statement?
| a. | The process of executing a synchronized method requires the thread to acquire a lock |
| b. | Any overriding method of a synchronized method is implicitly synchronized |
| c. | If any method in a class is synchronized, then the class itself must also be declared using the synchronized modifier |
| d. | If a thread invokes a static synchronized method on an instance of class A, then the thread must acquire the lock of that instance of class A |
| e. | None of the above |
class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}}
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 |
import java.util.*;
class GFC109 {
public static void main (String[] args) {
Object v = new Vector();
System.out.print((v instanceof Collections)+",");
System.out.print((v instanceof Arrays)+",");
System.out.print(v instanceof List);
}}
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 GFC214 {
static void m1(boolean b1) {System.out.print("boolean ");}
static void m1(byte b1) {System.out.print("byte ");}
static void m1(short s1) {System.out.print("short ");}
static void m1(char c1) {System.out.print("char ");}
static void m1(int i1) {System.out.print("int ");}
public static void main(String[] args) {
byte b1; m1(b1 = 1); m1(b1); m1(b1 == 1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: byte byte byte |
| b. | Prints: byte byte boolean |
| c. | Prints: int int int |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
class A {
private static int counter;
public static int getCounter(){return counter++;}
private static int innerCounter;
public static int getInnerCounter(){return innerCounter++;}
private String name;
A() {name = "A" + getCounter();}
class B {
private String name;
B() {
name = "B" + getInnerCounter();
System.out.print(A.this.name + name); // 1
}}
public static void main(String[] args) {
new A().new B(); // 2
A a1 = new A();
a1.new B(); // 3
a1.new B(); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A0B0A1B1A1B2 |
| b. | Prints: A0B0A1B1A2B2 |
| c. | Compile-time error at line 1 |
| d. | Compile-time error at line 2 |
| e. | Compile-time error at line 3 |
| f. | Compile-time error at line 4 |
| g. | Other compile-time error. |
| h. | Run-time error |
| i. | None of the above |
Which of the following thread state transitions model the lifecycle of a thread?
| a. | The Dead state to the Ready state |
| b. | The Ready state to the Not-Runnable state |
| c. | The Ready state to the Running state |
| d. | The Running state to the Not-Runnable state |
| e. | The Running state to the Ready state |
| f. | The Not-Runnable state to the Ready state |
| g. | The Not-Runnable state to the Running state |
class A {} class B extends A {}
class C extends B {
static void m1(A x) {System.out.print("m1A");}
static void m2(B x) {System.out.print("m2B"); m1(x);}
static void m2(A x) {System.out.print("m2A"); m1(x);}
static void m3(C x) {System.out.print("m3C"); m2(x);}
static void m3(B x) {System.out.print("m3B"); m2(x);}
static void m3(A x) {System.out.print("m3A"); m2(x);}
public static void main(String[] args) {m3(new C());}
}
What is the result of attempting to compile and run the program?
| a. | Prints: m3Am2Am1A |
| b. | Prints: m3Bm2Bm1A |
| c. | Prints: m3Cm2Bm1A |
| d. | Prints: m3Cm2Am1A |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
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 |
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 B {
private String name;
public B(String name) {this.name = name;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class J {
static B bc;
static int i = 1;
static B m1(B b) {bc = b; return new B("B" + i++);}
public static void main (String[] args) {
B x = m1(new B("Ba")), y = m1(new B("Bb"));
System.out.println(", " + x + ", " + y + ", " + bc);
}}
Which of the following could be a result of attempting to compile and run the program?
| a. | BaBb, B1, B2, B2 |
| b. | B1B2, null, null, Bb |
| c. | , Ba, Bb, Bb |
| d. | BaBbB1B2, null, null, null |
| e. | Ba, B1, B2, Bb |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |