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.
class GFC401 {
static int m1(int x) {return ++x;}
public static void main (String[] args) {
int x = 1;
int y = m1(x);
System.out.println(x + "," + y);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,1 |
| b. | Prints: 1,2 |
| c. | Prints: 2,1 |
| d. | Prints: 2,2 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Which of the following are true statements?
| a. | Encapsulation is a form of data hiding. |
| b. | A tightly encapsulated class is always immutable. |
| c. | Encapsulation is always used to make programs run faster. |
| d. | Encapsulation helps to protect data from corruption. |
| e. | Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged. |
Which of the following methods are members of the Object class?
| a. | join |
| b. | notify |
| c. | notifyAll |
| d. | run |
| e. | sleep |
| f. | start |
| g. | yield |
| h. | wait |
class SRC102 {
public static void main (String[] args) {
int i1 = Math.round(0.5f);
int i2 = Math.round(1.5d);
System.out.print(i1 + "," + i2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,1 |
| b. | Prints: 0,2 |
| c. | Prints: 1,1 |
| d. | Prints: 1,2 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class A {
public static void main (String args[]) {
byte primitiveByte = 1; // 1
Byte b1 = new Byte(primitiveByte); // 2
Byte b2 = new Byte(1); // 3
System.out.print(b1.byteValue() + b2.byteValue());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 2 |
| b. | Prints: 11 |
| c. | Compile-time error at 1 |
| d. | Compile-time error at 2 |
| e. | Compile-time error at 3 |
| f. | Run-time error |
| g. | None of the above |
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
char primitiveChar = 'b'-'a';
int primitiveInt = 1;
short primitiveShort = 1;
String s = "1";
Integer i1 = new Integer(primitiveByte);
Integer i2 = new Integer(primitiveChar);
Integer i3 = new Integer(primitiveShort);
Integer i4 = new Integer(primitiveInt);
Integer i5 = new Integer(s);
int p1 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();
System.out.print(p1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 5 |
| b. | Prints: 5.0 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
int primitiveInt = 1;
long primitiveLong = 1L;
float primitiveFloat = 1f;
String s = "1";
Long i1 = new Long(primitiveByte);
Long i2 = new Long(primitiveInt);
Long i3 = new Long(primitiveLong);
Long i4 = new Long(primitiveFloat);
Long i5 = new Long(s);
int i6 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();
System.out.print(i6);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 5 |
| b. | Prints: 5.0 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
char primitiveChar = 1;
double primitiveDouble = 1;
String s = "1";
Double d1 = new Double(primitiveByte);
Double d2 = new Double(primitiveChar);
Double d3 = new Double(primitiveDouble);
Double d4 = new Double(s);
double d5 = d1.doubleValue() + d2.doubleValue() +
d3.doubleValue() + d4.doubleValue();
System.out.print(d5);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 4 |
| b. | Prints: 4.0 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class A {
public static void main(String[] args) {
Boolean b1 = new Boolean(true); // 1
Boolean b2 = new Boolean(false); // 2
Boolean b3 = new Boolean(TRUE); // 3
Boolean b4 = new Boolean(FALSE); // 4
Boolean b5 = new Boolean("TrUe"); // 5
Boolean b6 = new Boolean("fAlSe"); // 6
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
class C {
public static void main (String[] args) {
Float f1 = new Float(1.0); // 1
Float f2 = new Float("1.0"); // 2
Float f3 = new Float("1.0f"); // 3
Float f4 = new Float("1e1f"); // 4
Float f5 = new Float(".1e1d"); // 5
}}
What is the result of attempting to compile and run the program?
| a. | Compile-time error at 1 |
| b. | Compile-time error at 2 |
| c. | Compile-time error at 3 |
| d. | Compile-time error at 4 |
| e. | Compile-time error at 5 |
| f. | Run-time error at 1 |
| g. | Run-time error at 2 |
| h. | Run-time error at 3 |
| i. | Run-time error at 4 |
| j. | Run-time error at 5 |
| k. | None of the above |
Which of the following class instance creation expressions would generate a compile-time error?
| a. | new Short(1) |
| b. | new Short('1') |
| c. | new Short('b' - 'a') |
| d. | new Short((short)1 - (short)2) |
| e. | new Short((byte)1) |
| f. | new Short((short)1) |
Which implementation of the List interface produces the slowest access to an element in the middle of the list by means of an index?
| a. | Vector |
| b. | ArrayList |
| c. | LinkedList |
| d. | None of the above |
import java.util.*;
class GFC100 {
public static void main (String args[]) {
Object a1 = new LinkedList(), b1 = new TreeSet();
Object c1 = new TreeMap();
System.out.print((a1 instanceof Collection)+",");
System.out.print((b1 instanceof Collection)+",");
System.out.print(c1 instanceof Collection);
}}
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 |
Suppose that an instance of class C has legal implementations of the hashCode and equals methods. Within any one execution of the Java application, the hash code contract requires that each invocation of the hashCode method on the same instance of class C must consistently return the same result as long as the fields used for the equals comparison remain unchanged.
| a. | false |
| b. | true |
class MWC102 {
public static void main (String[] args) {
byte[] a = new byte[1]; long[] b = new long[1];
float[] c = new float[1]; Object[] d = new Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,null |
| b. | Prints: 0,0,0.0,null |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class MWC202 {
public static void main(String[] args) {
int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}};
int []a2[] = {{1,2},{3,4,5},{6,7,8,9}};
int a3[][] = {{1,2},{3,4,5},{6,7,8,9}};
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 14 |
| b. | Prints: 16 |
| c. | Prints: 1,5,9 |
| d. | Prints: 2,4,8 |
| e. | Prints: 2,5,9 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
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. |
Suppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?
| a. | Static variables declared in this class or any superclass. |
| b. | Instance variables declared in this class or any superclass. |
| c. | Static methods declared in this class or any superclass. |
| d. | Instance methods declared in this class or any superclass. |
| e. | The keyword this. |
| f. | The keyword super. |
class Magenta {
static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 127 128 255 256 |
| b. | Prints: 127 128 255 0 |
| c. | Prints: 127 -1 -127 0 |
| d. | Prints: 127 -128 -1 0 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
public static void main(String args[]) {
Sub s1 = new Sub();
I2 i2 = s1; // 1
I1 i1 = s1; // 2
Base base = s1; // 3
Sub s2 = (Sub)base; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
class Green {
public static void main (String args[]) {
int[] i = null; // 1
Cloneable c = i; // 2
i = (int [])c; // 3
}}
What is the result of attempting to compile and run the program?
| a. | Compile-time error at line 1. |
| b. | Run-time error at line 1. |
| c. | Compile-time error at line 2. |
| d. | Run-time error at line 2. |
| e. | Compile-time error at line 3. |
| f. | Run-time error at line 3. |
| g. | None of the above |
class GFC215 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: float,float |
| b. | Prints: float,double |
| c. | Prints: double,float |
| d. | Prints: double,double |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: B.s1 A.s2 B.s1 A.s2 |
| b. | Prints: B.s1 A.s2 A.s1 A.s2 |
| c. | Prints: A.s1 A.s2 B.s1 A.s2 |
| d. | Prints: A.s1 A.s2 A.s1 A.s2 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
class A {
private static String s1 = "s1";
final String s2 = "s2";
A () { new Z("s5","s6");}
class Z {
final String s3 = "s3";
String s4 = "s4";
Z (final String s5, String s6) {
System.out.print(???);
}}
public static void main(String args[]) {new A();}
}
Which variable can not be substituted for ??? without causing a compile-time error?
| a. | s1 |
| b. | s2 |
| c. | s3 |
| d. | s4 |
| e. | s5 |
| f. | s6 |
| g. | 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 extends C {
void m1(D d) {System.out.print("D");}
public static void main(String[] args) {
A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D();
d1.m1(a1); d1.m1(b1); d1.m1(c1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABC |
| c. | Prints: DDD |
| d. | Prints: ABCD |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class MWC101 {
public static void main(String[] args) {
String s1 = "A", s2 = "a", s3 = "b";
s1.toLowerCase(); s3.replace('b','a');
System.out.print((s1.equals(s2)) + "," + (s2.equals(s3)));
}}
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 MWC200 {
public static void main (String[] args) {
String s1 = "ABC";
StringBuffer s2 = new StringBuffer(s1);
System.out.print(s2.equals(s1) + "," + s1.equals(s2));
}}
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 |
Which of the following are true statements?
| a. | An anonymous class is implicitly abstract. |
| b. | An anonymous class is implicitly final. |
| c. | An anonymous class is implicitly static. |
| d. | A static reference variable can reference an instance of an anonymous class. |
| e. | An anonymous class declaration must have at least one explicit constructor declaration. |
| f. | An anonymous class declaration can have more than one explicit constructor declaration. |
class GFC301 {
private String name;
public GFC301(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC301 r1, GFC301 r2) {
r1.setName("Bird");
r2 = r1;
}
public static void main (String[] args) {
GFC301 pet1 = new GFC301("Dog");
GFC301 pet2 = new GFC301("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: Dog,Cat |
| b. | Prints: Dog,Bird |
| c. | Prints: Bird,Cat |
| d. | Prints: Bird,Bird |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |