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.
package com.dan.chisholm;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
class B {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
Assume that the code appears in a single file named A.java. What is the result of attempting to compile and run the program?
| a. | Prints: A.m1, A.m2, A.m3, A.m4, |
| b. | Compile-time error at 1. |
| c. | Compile-time error at 2. |
| d. | Compile-time error at 3. |
| e. | Compile-time error at 4. |
| f. | None of the above |
class GFM11{
public static void main (String[] args) {
int x,y,z;
System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?
| a. | Prints nothing. |
| b. | Prints an undefined value. |
| c. | Prints: null |
| d. | Prints: 0 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class GRC10 {
public static void main (String[] s) {
System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
| a. | Prints: ABC |
| b. | Prints: BCD |
| c. | Prints: CDE |
| d. | Prints: A B C |
| e. | Prints: B C D |
| f. | Prints: C D E |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MCZ11 {
public static void main (String[] args) {
char a = '\c'; // 1
char b = '\r'; // 2
char c = '\"'; // 3
char d = '\b'; // 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 |
class MWC101 {
public static void main(String[] args) {
int[] a1 = new int[]; // 1
int a2[] = new int[5]; // 2
int[] a3 = new int[]{1,2}; // 3
int []a4 = {1,2}; // 4
int[] a5 = new int[5]{1,2,3,4,5}; // 5
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
class MWC201 {
public static void main(String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.print(a1[0][2]+","+a1[1][0]+","+a1[2][1]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3,4,8 |
| b. | Prints: 7,2,6 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class A {A(int i) {}} // 1
class B extends A {} // 2
Which of the following statements are true?
| a. | The compiler attempts to create a default constructor for class A. |
| b. | The compiler attempts to create a default constructor for class B. |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
Which of the following statements are true?
| a. | A constructor can invoke another constructor of the same class using the alternate constructor invocation, "this(argumentListopt);". |
| b. | A constructor can invoke itself using the alternate constructor invocation, "this(argumentListopt);". |
| c. | The alternate constructor invocation, "this(argumentListopt);", can legally appear anywhere in the constructor body. |
| d. | A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation, "super(argumentListopt);". |
| e. | The number of constructor invocations that may appear in any constructor body can equal but not exceed the number of alternate constructors declared in the same class. |
| f. | A constructor is not permitted to throw an exception. |
class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {C() {}} // 3
Which of the following statements are true?
| a. | class A extends Object. |
| b. | Compile-time error at 1. |
| c. | Compile-time error at 2. |
| d. | Compile-time error at 3. |
Which of the following modifiers can be applied to a constructor?
| a. | private |
| b. | abstract |
| c. | final |
| d. | volatile |
| e. | native |
| f. | None of the above. |
Which of the following modifiers can be applied to the declaration of a field?
| a. | abstract |
| b. | final |
| c. | private |
| d. | protected |
| e. | public |
Which of the following modifiers can not be applied to a method?
| a. | abstract |
| b. | private |
| c. | protected |
| d. | public |
| e. | volatile |
| f. | None of the above. |
class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 13 |
| b. | Prints: 4 |
| c. | Compile-time error at 1 |
| d. | Compile-time error at 2 |
| e. | Run-time error |
| f. | None of the above |
Which of the following modifiers can be applied to a class that is not a nested class?
| a. | public |
| b. | protected |
| c. | private |
| d. | abstract |
| e. | static |
| f. | final |
Which of the follow are true statements.
| a. | A nested class is any class that is declared within the body of another class or interface. |
| b. | A nested class can not be declared within the body of an interface declaration. |
| c. | An inner class is a nested class that is not static. |
| d. | A nested class can not be declared static. |
| e. | A named class is any class that is not anonymous. |
class A {
public static void main (String[] args) {
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + ",");
System.out.print(error instanceof Throwable);
}}
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 B {
private String name;
public B(String s) {name = s;}
protected void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m(); System.gc();
}}
Which of the following could not be a result of attempting to compile and run the program?
| a. | Prints: XY |
| b. | Prints: YX |
| c. | Prints: XXYY |
| d. | Nothing is printed. |
| e. | None of the above |
Which of the following statements is not true?
| a. | An interface that is declared within the body of a class or interface is known as a nested interface. |
| b. | A constant can be a member of an interface. |
| c. | A class declaration can be a member of an interface. |
| d. | If an interface is named in the implements clause of a class, then the class must implement all of the methods declared within the interface. |
| e. | None of the above. |
class JJF1 {
public static void main (String args[]) {
System.out.print(Byte.MIN_VALUE+",");
System.out.print(Byte.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,255 |
| b. | Prints: 0,256 |
| c. | Prints: -127,128 |
| d. | Prints: -128,127 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class GRC1 {public static void main(String[] args) {}} // 1
class GRC2 {protected static void main(String[] args) {}} // 2
class GRC3 {private static void main(String[] args) {}} // 3
What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line?
| a. | Compile-time error at line 1. |
| b. | Compile-time error at line 2. |
| c. | Compile-time error at line 3. |
| d. | An attempt to run GRC1 from the command line fails. |
| e. | An attempt to run GRC2 from the command line fails. |
| f. | An attempt to run GRC3 from the command line fails. |
class EBH201 {
public static void main (String[] args) {
int a = 1 || 2 ^ 3 && 5;
int b = ((1 || 2) ^ 3) && 5;
int c = 1 || (2 ^ (3 && 5));
System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0 |
| b. | Prints: 0,0,3 |
| c. | Prints: 0,3,0 |
| d. | Prints: 0,3,3 |
| e. | Prints: 3,0,0 |
| f. | Prints: 3,0,3 |
| g. | Prints: 3,3,0 |
| h. | Prints: 3,3,3 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
class GFC100 {
public static void main(String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 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 |
Which of the following is a true statement?
| a. | An anonymous class can extend only the Object class. |
| b. | An anonymous class can not implement an interface. |
| c. | An anonymous class declaration can not have an implements clause. |
| d. | An anonymous class declaration can name more than one interface in the implements clause. |
| e. | The class instance creation expression for an anonymous class must never include arguments. |
| f. | None of the above |
class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}
Which statements are true?
| a. | With assertions enabled it prints 210210-1 followed by an AssertionError message. |
| b. | With assertions enabled it prints 210210 followed by an AssertionError message. |
| c. | With assertions enabled it prints only 210210. |
| d. | With assertions enabled it prints nothing. |
| e. | With assertions disabled it prints 210210-1 |
| f. | With assertions disabled it prints only 210210 |
| g. | Assertions should not be used within the default case of a switch statement. |
Which of these words belong to the set of Java keywords?
| a. | transient |
| b. | serializable |
| c. | runnable |
| d. | run |
| e. | volatile |
| f. | externalizable |
| g. | cloneable |
class EBH001 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(1) - m(2) + m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1, 2, 3, 4, 8, |
| b. | Prints: 1, 2, 3, 4, 11, |
| c. | Prints: 3, 4, 1, 2, 11, |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}
java JMM101 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
| a. | Prints: JMM101ABCDEF |
| b. | Prints: ABCDEF |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class EBH101 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
int i = 1; m(m(++i) + m(i++) + m(-i) + m(i++));
}}
What is the result of attempting to compile and run the above program?
| a. | Prints: 1, 2, 3, 4, 10, |
| b. | Prints: 1, 2, -3, 4, 4, |
| c. | Prints: 2, 2, -3, -3, -2, |
| d. | Prints: 2, 2, -3, 3, 4, |
| e. | Prints: 2, 3, -3, -2, 0, |
| f. | Prints: 2, 3, -3, 4, 6, |
| g. | Prints: 2, 3, 4, 5, 14, |
| h. | Run-time error |
| i. | Compile-time error |
| j. | None of the above |
class Identifiers {
int i1; // 1
int _i2; // 2
int i_3; // 3
int #i4; // 4
int $i5; // 5
int %i6; // 6
int i$7; // 7
int 8i; // 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 |