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 MCZ13 {
public static void main (String[] args) {
String s = null;
System.out.print(s);
}}
What is the result of attempting to compile and run the program?
| a. | Prints nothing. |
| b. | Prints: null |
| c. | Compile-time error |
| d. | Run-time error |
| e. | 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. | public |
| c. | static |
| d. | synchronized |
| e. | transient |
| f. | volatile |
Which of the following represent the full range of type char?
| a. | -0x8000 to 0x7fff |
| b. | 0x0000 to 0xffff |
| c. | -0100000 to 077777 |
| d. | 0 to 0177777 |
| e. | 0 to 32767 |
| f. | 0 to 65535 |
| g. | -32768 to 32767 |
class EBH012 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}}
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. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
| g. | 7 |
| h. | 8 |
A class can not be called "tightly encapsulated" unless which of the following is true?
| a. | All member fields are declared final. |
| b. | The class is not anonymous. |
| c. | The internal data model can be read and modified only through accessor and mutator methods. |
| d. | The class is an inner class. |
| e. | None of the above |
Which of the following is a checked exception?
| a. | IllegalMonitorStateException |
| b. | IllegalThreadStateException |
| c. | IllegalArgumentException |
| d. | InterruptedException |
| e. | None of the above |
class MWC106 {
static void m1(String s) {
s = s.trim(); s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
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 SRC109 {
public static void main (String[] args) {
short x3 = 0; short x4 = 1;
short b1 = Math.min(x3,x4); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(b1+","+c1+","+d1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0 |
| b. | Compile-time error at 1 |
| c. | Compile-time error at 2 |
| d. | Compile-time error at 3 |
| e. | Run-time error |
| f. | None of the above |
class C {
public static void main (String args[]) {
String s1 = "1", s2 = "2";
Byte b1 = Byte.parseByte(s1);
Byte b2 = Byte.parseByte(s2);
System.out.print(b1.byteValue() + b2.byteValue());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3 |
| b. | Prints: 12 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class A {
public static void main (String[] args) {
String s = "11";
int i1 = Integer.parseInt(s);
System.out.print(new Integer(i1).equals(new Integer(i1)) + ",");
System.out.print(new Integer(i1).equals(new Integer(s)) + ",");
System.out.print(new Integer(i1) == new Integer(i1));
}}
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 |
Which of the method invocation expressions would produce a run-time error?
| a. | Long.parseLong("1") |
| b. | Long.parseLong("1L") |
| c. | Long.parseLong("010") |
| d. | Long.parseLong("0x10") |
| e. | Long.parseLong("1.0") |
Which method of the java.lang.Double class returns an instance of type Double?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
| h. | None of the above |
| • | Elements are not key/value pairs. | |
| • | Contains no duplicate elements. | |
| • | The entries can be sorted using 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 GFC106 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Map)+",");
System.out.print((b instanceof Map)+",");
System.out.print(c instanceof Map);
}}
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 A {
int i1, i2;
public void setI1(int i) {i1 = i;}
public int getI1() {return i1;}
public void setI2(int i) {i2 = i;}
public int getI2() {return i2;}
public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;}
public boolean equals(Object obj) {
if (obj instanceof A) {
return (i1 == ((A)obj).getI1());
}
return false;
}
public int hashCode() {
// Insert statement here.
}}
Which of the following statements could be inserted at the specified location without violating the hash code contract?
| a. | return 31; |
| b. | return getI1(); |
| c. | return getI2(); |
| d. | return 31 * getI1() + getI2(); |
class JSC104{
public float a; // 1
protected float b; // 2
private float c; // 3
static float d; // 4
synchronized float 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 Z {
void m1() {
abstract class A {} // 1
final class B {} // 2
private class C {} // 3
protected class D {} // 4
public class E {} // 5
}
}
Which class declarations result in compile-time errors?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
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 GFM17 {
int x; // 1
public static void main(String[] args) { // 2
int y = 0; // 3
System.out.print(x+","); // 4
System.out.print(y); // 5
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0 |
| b. | Compile-time error at line 1. |
| 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. | Compile-time error at line 5. |
| h. | Run-time error |
| i. | None of the above |
class GFC205 {} class GFC206 extends GFC205 {}
class GFC207 extends GFC206 {
static void m(GFC205 x, GFC205 y) {System.out.print("GFC205,GFC205");}
static void m(GFC205 x, GFC206 y) {System.out.print("GFC205,GFC206");}
static void m(GFC206 x, GFC205 y) {System.out.print("GFC206,GFC205");}
static void m(GFC206 x, GFC206 y) {System.out.print("GFC206,GFC206");}
public static void main(String[] args) {
GFC207 gfc207 = new GFC207(); m(gfc207, gfc207);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: GFC205,GFC205 |
| b. | Prints: GFC205,GFC206 |
| c. | Prints: GFC206,GFC205 |
| d. | Prints: GFC206,GFC206 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
String s1 = "C";
void printIt() {
System.out.print(((A)this).s1 + ((B)this).s1 +
((C)this).s1 + ((I)this).s1);
}
public static void main (String[] args) {new C().printIt();}
}
What is the result of attempting to compile and run the program?
| a. | Prints: ABCI |
| b. | Run-time error |
| c. | Compile-time error |
| d. | None of the above |
class G {
final String s1 = "G.s1";
class Z {
String s1;
void m1() {System.out.println(???);}
}
public static void main(String args[]) {
G g = new G(); g.new Z().m1();
}}
Which name or expression could be used in place of ??? to cause the program to print "G.s1"?
| a. | s1 |
| b. | G.s1 |
| c. | ((G)this).s1 |
| d. | G.this.s1 |
| e. | G.super.s1 |
| 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 A(); C c1 = new A(); C 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 A {
private static int f1 = 1;
private int f2 = 2;
void m1(int p1, final int p2) {
int l1 = 5;
final int l2 = 6;
Object x = new Object() {
int a = f1; // 1
int b = f2; // 2
int c = p1; // 3
int d = p2; // 4
int e = l1; // 5
int f = l2; // 6
};}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
class EBH107 {
static int m(int i) {System.out.print(i + ","); return i;}
public static void main(String s[]) {
int i=0, j = m(++i) + m(++i) * m(++i) % m(++i) + m(++i);
System.out.print(j%5);
}}
What is the result of attempting to compile and run the above program?
| a. | Prints: 1,2,3,4,5,0 |
| b. | Prints: 1,2,3,4,5,1 |
| c. | Prints: 1,2,3,4,5,2 |
| d. | Prints: 1,2,3,4,5,3 |
| e. | Prints: 1,2,3,4,5,4 |
| f. | Prints: 1,2,3,4,5,5 |
| g. | Run-time error |
| h. | Compile-time error |
| i. | None of the above |
class GFC308 {
int[] i1 = {1}, i2 = {3};
void m1() {
m2(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}
void m2(int[] i1, int[] i2) {
int[] i3 = i1;
this.i1 = i2;
this.i2 = i3;
}
public static void main (String[] args) {
new GFC308().m1();
}}
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 |
class A13 {}
class A14 {
public static void main(String[] arg) {
A13[] a1 = new A13[1]; // 1
A13[][] a2 = new A13[2][1]; // 2
A13[][][] a3 = new A13[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A13(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}}
What is the result of attempting to compile and run the program?
| a. | Prints: null |
| b. | Prints: nullnull |
| 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. | Compile-time error at 8. |
| k. | Run-time error |