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 amazon.com.
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.
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 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 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 following techniques can be used to prevent the instantiation of a class by any code outside of the class?
| a. | Do not declare any constructors. |
| b. | Do not use a return statement in the constructor. |
| c. | Declare all constructors using the keyword void to indicate that nothing is returned. |
| d. | Declare all constructors using the private access modifier. |
| e. | None of the above. |
Which of the following modifiers can be applied to a constructor?
| a. | protected |
| b. | public |
| c. | static |
| d. | synchronized |
| e. | transient |
Which of the following modifiers can be applied to the declaration of a field?
| a. | static |
| b. | synchronized |
| c. | transient |
| d. | volatile |
| e. | native |
Which of the following modifiers can not be applied to a method?
| a. | final |
| b. | static |
| c. | synchronized |
| d. | transient |
| e. | native |
| f. | None of the above. |
class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 24 |
| b. | Prints: 6 |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
| e. | Run-time error |
| f. | None of the above |
// Class A is declared in a file named A.java.
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 D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
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. |
Which of the follow statements is true.
| a. | An anonymous class can be declared abstract. |
| b. | A local class can be declared abstract. |
| c. | An abstract class can be instantiated. |
| d. | An abstract class is implicitly final. |
| e. | An abstract class must declare at least one abstract method. |
| f. | An abstract class can not extend a concrete class. |
Which of the following statements are true?
| a. | The compiler will create a default constructor if no other constructor is declared. |
| b. | The default constructor takes no arguments. |
| c. | If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass. |
| d. | The default constructor declares Exception in the throws clause. |
| e. | The default constructor is always given the private access modifier. |
| f. | The default constructor is always given the public modifier. |
| g. | The default constructor is always given default package access. |
Which of the following is used to prevent the serialization of a non-static field?
| a. | final |
| b. | protected |
| c. | synchronized |
| d. | transient |
| e. | volatile |
| f. | native |
Which of the following modifiers can not be used with the abstract modifier in a method declaration?
| a. | final |
| b. | private |
| c. | protected |
| d. | public |
| e. | static |
| f. | synchronized |
| g. | native |
class JSC203 {
static int m1(byte b) {return b;} // 1
static int m2(char c) {return c;} // 2
static int m3(long l) {return l;} // 3
public static void main(String[] args) {
byte b = 1; char c = '\u0002'; long l = 4L;
System.out.print(""+m1(b)+m2(c)+m3(l));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 124 |
| b. | Prints: 7 |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
| e. | Compile-time error at 3. |
| f. | Run-time error |
// Class A is declared in a file named A.java.
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 C is declared in a file named C.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
public static void main(String[] args) {
C c = new C();
c.m1(); // 1
c.m2(); // 2
c.m3(); // 3
c.m4(); // 4
}}
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. |
public class A {int i1; void m1() {}}
Which of the following statements are true?
| a. | class A extends Object. |
| b. | Field i1 is implicitly public, because class A is public. |
| c. | Method m1 is implicitly public, because class A is public. |
| d. | The compiler will insert a default constructor implicitly. |
| e. | The default constructor has no throws clause. |
| f. | The default constructor of class A has package access. |
| g. | The default constructor accepts one parameter for each field in class A. |
| h. | The default constructor invokes the no-parameter constructor of the superclass. |