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 JSC102 {
public static void main (String[] args) {
private int x = 1; protected int y = 2; public int z = 3;
System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 123 |
| b. | Prints: 1 2 3 |
| c. | Prints: 6 |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
Which of these words belong to the set of Java keywords?
| a. | exit |
| b. | strictfp |
| c. | enum |
| d. | super |
| e. | abort |
| f. | event |
| g. | goto |
| h. | native |
| i. | exception |
class MCZ31 {
public static void main (String[] args) {
char a = '\t'; // 1
char b = '\\'; // 2
char c = '\"'; // 3
char d = '\''; // 4
char e = '\?'; // 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 |
Which of the following methods are static members of the java.lang.Double class?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
class D {
public static void main (String[] args) {
Boolean b1 = Boolean.valueOf("trUE"); // 1
Boolean b2 = Boolean.valueOf("Even more true"); // 2
Boolean b3 = Boolean.valueOf(null); // 3
System.out.print((b1==b2) + ",");
System.out.print((b2==b3) + ",");
System.out.println(b3==b1);
}}
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. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class E {
static String m(float f) {return "f";}
static String m(Float f) {return "F";}
public static void main (String[] args) {
System.out.print(m(Float.parseFloat("1")));
System.out.print(m(Float.floatValue()));
System.out.print(m(Float.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 |
class E {
static String m(short s) {return "p";}
static String m(Short s) {return "S";}
public static void main (String[] args) {
Short s1 = new Short("1");
System.out.print(m(s1.shortValue())+",");
System.out.print(m(Short.parseShort("1"))+",");
System.out.print(m(Short.valueOf("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: p,p,p |
| b. | Prints: p,p,S |
| c. | Prints: p,S,p |
| d. | Prints: p,S,S |
| e. | Prints: S,p,p |
| f. | Prints: S,p,S |
| g. | Prints: S,S,p |
| h. | Prints: S,S,S |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
| • | Stores key/value pairs. | |
| • | Duplicate entries replace old entries. | |
| • | Entries are sorted using a Comparator or the Comparable interface. |
Which of these classes provides the specified features?
| a. | LinkedList |
| b. | TreeMap |
| c. | TreeSet |
| d. | HashMap |
| e. | HashSet |
| f. | Hashtable |
| g. | None of the above |
import java.util.*;
class GFC104 {
public static void main (String args[]) {
LinkedList a1 = new LinkedList();
ArrayList b1 = new ArrayList();
Vector c1 = new Vector();
System.out.print((a1 instanceof List)+",");
System.out.print((b1 instanceof List)+",");
System.out.print(c1 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 B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}
Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1. Which class has a hashCode method that is not consistent with the hash code contract?
| a. | B |
| b. | C |
| c. | D |
| d. | None of the above |
Which of the following statements are true?
| a. | A final method can not be overridden. |
| b. | All methods declared in a final class are implicitly final. |
| c. | The methods declared in a final class must be explicitly declared final or a compile-time error occurs. |
| d. | It is a compile-time error if a private method is declared final. |
| e. | A machine-code generator can inline the body of a final method. |
abstract class A {} // 1
transient class G {} // 2
private class C {} // 3
static class E {} // 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 {
abstract class A {} // 1
final class B {} // 2
private class C {} // 3
protected class D {} // 4
public class E {} // 5
}
Which class declaration results in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| 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 = 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 |
Suppose that an interface, I1, is not a member of an enclosing class or interface. Which of the following modifiers can be applied to interface I1?
| a. | abstract |
| b. | final |
| c. | private |
| d. | protected |
| e. | public |
Which of the following represent the full range of type char?
| a. | '\u0000' to '\u7fff' |
| b. | '\u0000' to '\uffff' |
| c. | 0 to 32767 |
| d. | 0 to 65535 |
| e. | -32768 to 32767 |
| f. | -65536 to 65535 |
class EBH011 {
public static void main (String[] args) {
float a = Float.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
double c = Double.NaN;
System.out.print((a == b)+","+(c == c)+","+(c != c));
}}
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 A {
public static void main(String[] args) {
char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
System.out.print(a + b + "" + a + b);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 390 |
| b. | Prints: 195195 |
| c. | Prints: 195ab |
| d. | Prints: ab195 |
| e. | Prints: abab |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
abstract class A {
private int x = 4, y = 2;
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 A(int i1, int i2) {x(i1);y(i2);};
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.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 |
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;
}
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 I {
private String name;
public I(String s) {name = s;}
private I other;
public void other(I i) {other = i;}
}
class J {
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
private void m1() {
i1.other(i2); i2.other(i1); i3.other(i3);
i1 = i3; i2 = i3;
m2();
}
private void m2() {/* Do amazing things. */}
public static void main (String[] args) {
new J().m1();
}}
Which of the three objects, A, B or C, is not eligible for garbage collection when method m2 begins to execute?
| a. | A |
| b. | B |
| c. | C |
| d. | None of the above |
class GFM16 {
static int m1 (int i1, int i2) {
int i3;
if (i1 > 0) {i3 = i1 + i2;}
return i3;
}
public static void main(String[] args) {
System.out.println(m1(1,2));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0 |
| b. | Prints: 1 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class UltraViolet {
public static void main (String[] args) {
char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = Dollar Sign $
System.out.print(a + b); // 1
System.out.print(" ABC" + a + b); // 2
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 78 ABC*$ |
| b. | Prints: *$ ABC*$ |
| c. | Prints: 78 ABC78 |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
class GFC202 {} class GFC203 extends GFC202 {}
class GFC204 {
static void m(GFC202 x) {System.out.print("GFC202");}
static void m(GFC203 x) {System.out.print("GFC203");}
public static void main(String[] args) {m(null);}
}
What is the result of attempting to compile and run the program?
| a. | Prints: GFC202 |
| b. | Prints: GFC203 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class MWC206 {
public static void main (String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a1[j][i]);
}}}}
What is the result of attempting to compile and run the program?
| a. | Prints: 123456789 |
| b. | Prints: 147258369 |
| c. | Prints: 321654987 |
| d. | Prints: 369258147 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class EBH106 {
public static void main(String args[]) {
int a = 1; a += ++a + a++; System.out.print(a);
}}
What is the result of attempting to compile and run the above program?
| a. | Prints: 3 |
| b. | Prints: 4 |
| c. | Prints: 5 |
| d. | Prints: 6 |
| e. | Prints: 7 |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
class Amber {
public static void main(String[] args) {
int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
Object[] obj = (Object[])a.clone(); // 2
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 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 GFC307 {
static void m1(int[] i1, int[] i2) {
i1 = i2 = null;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0 |
| b. | Prints: 1,1 |
| c. | Prints: 1,3 |
| d. | Prints: 3,1 |
| e. | Prints: null,null |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |