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.
interface F {abstract void main(String[] args);} // 1
interface G {synchronized void main(String[] args);} // 2
interface H {final void main(String[] args);} // 3
interface I {native void main(String[] args);} // 4
Which interface declaration does not generate a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
class SRC122 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0f)) + m1(Math.abs(1.0)));
System.out.print(m1(Math.round(1.0f)) + m1(Math.round(1.0)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: IIII |
| b. | Prints: LLLL |
| c. | Prints: FFFF |
| d. | Prints: DDDD |
| e. | Prints: FDFD |
| f. | Prints: ILIL |
| g. | Prints: FDIL |
| h. | Prints: ILFD |
| i. | None of the above |
class JMM122 {
public static void main (String[] args) {
for (int i = 0; i < 4; i++) {
switch (i) {
case 0: System.out.print("A");
case 1: System.out.print("B");
case 2: System.out.print("C");
}}}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: ABCC |
| c. | Prints: CBA |
| d. | Prints: ABCBCC |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
interface A {String s1 = "A"; String m1();}
interface B implements A {String s1 = "B"; String m1();}
class C implements B {
public String m1() {return s1;}
public static void main(String[] args) {
A a = new C(); System.out.print(a.m1());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
Which of the following methods of the java.lang.Math class accepts an argument of type float or double, but has a return type of type int or long respectively.
| a. | abs |
| b. | ceil |
| c. | floor |
| d. | max |
| e. | min |
| f. | random |
| g. | round |
| h. | sin |
| i. | cos |
| j. | tan |
| k. | sqrt |
| l. | None of the above |
class JMM123 {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (int h = 0; h < 6; h++) {
label2:
do { i++; k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label2;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j<5);
}
System.out.println(h + "," + i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,1,0 |
| b. | Prints: 0,2,1 |
| c. | Prints: 1,3,1 |
| d. | Prints: 2,4,1 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
interface A {int i = 1; int m1();}
interface B extends A {int i = 10; int m1();}
class C implements B {
public int m1() {return ++i;}
public static void main(String[] args) {
System.out.print(new C().m1());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 2 |
| b. | Prints: 11 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
Which of the following methods of the java.lang.Math class declares a non-primitive argument type?
| a. | abs |
| b. | ceil |
| c. | floor |
| d. | max |
| e. | min |
| f. | random |
| g. | round |
| h. | sin |
| i. | cos |
| j. | tan |
| k. | sqrt |
| l. | None of the above |
Which of the following are true statements?
| a. | The Enumeration interface was introduced with the collections framework with Java 1.2. |
| b. | The Enumeration interface declares only two methods: hasMoreElements and nextElement. |
| c. | The Iterator interface extends the Enumeration interface. |
| d. | The Iterator interface declares a total of three methods. |
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A(); a.start(); System.out.print(a.i);
}}
What are the possible results of attempting to compile and run the program?
| a. | Prints nothing |
| b. | Prints: 0 |
| c. | Prints: 1 |
| d. | Prints: 01 |
| e. | Prints: 10 |
| f. | Compile-time error |
| g. | Run-time error |
Which of the following methods of the java.lang.Math class declares a non-primitive return type?
| a. | abs |
| b. | ceil |
| c. | floor |
| d. | max |
| e. | min |
| f. | random |
| g. | round |
| h. | sin |
| i. | cos |
| j. | tan |
| k. | sqrt |
| l. | None of the above |
Which of the following is a true statement?
| a. | The Iterator interface declares only two methods: hasMoreElements and nextElement. |
| b. | The ListIterator interface extends both the List and Iterator interfaces. |
| c. | The ListIterator interface was introduced with Java 1.2 to replace the older Iterator interface that was released with Java 1.0. |
| d. | The ListIterator interface declares only three methods: hasNext, next and remove. |
| e. | None of the above. |
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A(); a.run(); System.out.print(a.i);
}}
What is the result of attempting to compile and run the program?
| a. | Prints nothing |
| b. | Prints: 0 |
| c. | Prints: 1 |
| d. | Prints: 01 |
| e. | Prints: 10 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Which of the following statements is true in terms of the
| a. | The returned value is of a floating-point primitive type. |
| b. | The returned value is always of type int. |
| c. | The returned value is always of type long. |
| d. | The returned value is of type long only if the argument is of type long. |
| e. | The returned value is of type long only if the argument is of type double. |
| f. | None of the above |
class A extends Thread {
public void run() {
try {sleep(10000);} catch (InterruptedException ie){}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime);
}}
What are the possible results of attempting to compile and run the program?
| a. | Prints a number greater than or equal to 0 |
| b. | The number printed must always be greater than 10000 |
| c. | This program will run for at least ten seconds |
| d. | Compile-time error |
| e. | Run-time error |
Which of the following classes would provide the most efficient implementation of a First In First Out queue?
| a. | ArrayList |
| b. | LinkedHashMap |
| c. | LinkedHashSet |
| d. | LinkedList |
| e. | HashMap |
| f. | HashSet |
| g. | Hashtable |
| h. | TreeMap |
| i. | TreeSet |
| j. | Vector |
| k. | None of the above |
In addition to implementing the List interface, which of the following also provides methods to get, add and remove elements from the head and tail of the list without specifying an index?
| a. | Collection |
| b. | ArrayList |
| c. | LinkedList |
| d. | List |
| e. | Vector |
| f. | None of the above |
Which of the following is a true statement?
| a. | All implementations of the List interface provide fast random access. |
| b. | A LinkedList provides faster random access than an ArrayList. |
| c. | The LinkedList implements the RandomAccess interface. |
| d. | Each collection that implements the List interface must also implement the RandomAccess interface. |
| e. | The RandomAccess interface declares methods that use of an index argument to access elements of the List. |
| f. | The RandomAccess interface declares the next and hasNext methods. |
| g. | None of the above. |
class JMM124 {
public static void main(String args[]) {
int k;
for (int i=0, j=0; i<2; i++,j++) {System.out.print(i);} // 1
for (int i=0, k=0; i<2; i++,k++) {System.out.print(i);} // 2
for (int i=0, int j=0; i<2; i++,j++) {System.out.print(i);} // 3
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 012345 |
| b. | Prints: 010101 |
| 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 |
interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C10 implements I10, I20 { // 1
public static void main(String[] args) {
System.out.print(s10+","); // 2
System.out.print(s20+","); // 3
System.out.print(name); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Prints: I10.s10,I20.s20,I10 |
| b. | Prints: I10.s10,I20.s20,I20 |
| c. | Prints: I10.s10,I20.s20, |
| d. | Prints: I10.s10,I20.s20,null |
| e. | Compile-time error at line 1 |
| f. | Compile-time error at line 2 |
| g. | Compile-time error at line 3 |
| h. | Compile-time error at line 4 |
| i. | Run-time error |
| j. | None of the above |
class JMM125 {
static int i;
public static void main(String args[]) {
for (i=1; i<3; i++) {System.out.print(i);} // 1
for (int i=1; i<3; i++) {System.out.print(i);} // 2
int i; // 3
for (i=0; i<2; i++) {System.out.print(i);} // 4
System.out.print(JMM125.i);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1212010 |
| b. | Prints: 1212013 |
| c. | Compile-time error at line 1 |
| d. | Compile-time error at line 2 |
| e. | Compile-time error at line 4 |
| f. | Run-time error |
| g. | None of the above |
interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C20 implements I10, I20 { // 1
public static void main(String[] args) {
System.out.print(I10.s10+","); // 2
System.out.print(I20.s20+","); // 3
System.out.print(I20.name); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Prints: I10.s10,I20.s20,I10 |
| b. | Prints: I10.s10,I20.s20,I20 |
| c. | Prints: I10.s10,I20.s20, |
| d. | Prints: I10.s10,I20.s20,null |
| e. | Compile-time error at line 1 |
| f. | Compile-time error at line 2 |
| g. | Compile-time error at line 3 |
| h. | Compile-time error at line 4 |
| i. | Run-time error |
| j. | None of the above |
Which of the following is not a true statement?
| a. | The Iterator interface declares only three methods: hasNext, next and remove. |
| b. | The ListIterator interface extends both the List and Iterator interfaces. |
| c. | The ListIterator interface provides forward and backward iteration capabilities. |
| d. | The ListIterator interface provides the ability to modify the List during iteration. |
| e. | The ListIterator interface provides the ability to determine its position in the List. |
| f. | None of the above. |
class JMM126 {
static int i;
public static void main(String args[]) {
for (i=1; i<3; i++) {System.out.print(i);} // 1
for (int i=1; i<3; i++) {System.out.print(i);} // 2
int i; // 3
for (int i=0; i<2; i++) {System.out.print(i);} // 4
System.out.print(JMM126.i);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1212010 |
| b. | Prints: 1212013 |
| c. | Compile-time error at line 1 |
| d. | Compile-time error at line 2 |
| e. | Compile-time error at line 4 |
| f. | Run-time error |
| g. | None of the above |
class JMM121 {
public static void main (String args[]) {
int h = 0, i = 0, j = 0, k = 0;
label1:
for (;;) { h++;
label2:
do { i++; k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j < 5);
}
System.out.println(h + "," + i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,2,3 |
| b. | Prints: 1,3,2 |
| c. | Prints: 2,2,2 |
| d. | Prints: 2,4,1 |
| e. | Prints: 2,4,2 |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
class A extends Thread {
public void run() {System.out.print("A");}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
a.start(); // 1
}
}
What is the result of attempting to compile and run the program?
| a. | The program compiles and runs without error |
| b. | The second attempt to start thread t1 is successful |
| c. | The second attempt to start thread t1 is ignored |
| d. | Compile-time error at marker 1 |
| e. | An IllegalThreadStateException is thrown at run-time |
| f. | None of the above |
class A extends Thread {
public void run() {System.out.print("A");}
}
class B {
public static void main (String[] args) {
A a = new A(); a.start();
try {
a.join();
} catch (InterruptedException ie) {ie.printStackTrace();}
a.start(); // 1
}}
What is the result of attempting to compile and run the program?
| a. | The program compiles and runs without error |
| b. | The second attempt to start thread t1 is successful |
| c. | The second attempt to start thread t1 is ignored |
| d. | Compile-time error at marker 1 |
| e. | An IllegalThreadStateException is thrown at run-time |
| f. | None of the above |
class A extends Thread {
private static B b = new B();
private String s1;
public void run() {System.out.print(b.m1(s1));}
A(String threadName, String s1) {
super(threadName); this.s1 = s1;
}
public static void main (String[] args) {
A a = new A("T1","A"), b = new A("T2","B"); a.start(); b.start();
}}
class B {
private String s1;
public synchronized String m1(String s) {
s1 = s;
try {Thread.sleep(1);} catch (InterruptedException ie) {}
return "["+Thread.currentThread().getName()+","+s1+"]";
}}
What are the possible results of attempting to compile and run the program?
| a. | Prints nothing |
| b. | Prints: [T1,A][T2,B] |
| c. | Prints: [T1,B][T2,B] |
| d. | Prints: [T2,B][T1,A] |
| e. | Prints: [T2,A][T1,A] |
| f. | Compile-time error |
| g. | Run-time error |