Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

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.


Question 1

class GRC4 {public static void main(String[] args) {}} // 1
class GRC5 {public static void main(String []args) {}} // 2
class GRC6 {public static void main(String args[]) {}} // 3

What is the result of attempting to compile and run the above programs?

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 GRC4 from the command line fails.
e. An attempt to run GRC5 from the command line fails.
f. An attempt to run GRC6 from the command line fails.
g. None of the above

Question 2

class MCZ27 {
  public static void main (String[] args) {
    char a = '\f';  // 1
    char b = '\n';  // 2
    char c = '\r';  // 3
    char d = '\l';  // 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

Question 3

class GFC402 {
  static int x=1;
  void m1(int i) {x++; i++;}
  public static void main (String[] args) {
    int y=3; m1(y);
    System.out.println(x + "," + y);
}}

What is the result of attempting to compile and run the program?

a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,4
d. Prints: 2,4
e. Run-time error
f. Compile-time error
g. None of the above

Question 4

Which of the following modifiers can be applied to a constructor?

a. protected
b. public
c. static
d. synchronized
e. transient

Question 5

Which of the following modifiers can be applied to the declaration of a field?

a. static
b. synchronized
c. transient
d. volatile
e. native

Question 6

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.

Question 7

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

Question 8

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.

Question 9

Which of the follow are true statements.

a. A local class is declared within a method, constructor or block.
b. An anonymous class is always a local class.
c. A local class is a nested class.
d. A local class is a member class.
e. A local class is always a named class.

Question 10

class JMM102 {
  public static void main(String args[]) {
    for (int i = 0; i<5 ;i++) {
      switch(i) {
        case 0: System.out.print("v ");break;
        case 1: System.out.print("w ");
        case 2: System.out.print("x ");break;
        case 3: System.out.print("y ");
        case 4: System.out.print("z ");break;
        default: System.out.print("d ");
}}}}

What is the result of attempting to compile and run the program?

a. Prints: v w x y z
b. Prints: v w x y z d
c. Prints: v w x x y z z
d. Prints: v w w x y y z d
e. Prints: d d d d d d
f. Run-time error
g. Compile-time error
h. None of the above

Question 11

class A {
  public static void main (String[] args) {
    Object error = new Error();
    Object runtimeException = new RuntimeException();
    System.out.print((error instanceof Exception) + ",");
    System.out.print(runtimeException instanceof Exception);
}}

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

Question 12

// 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.

Question 13

Which of the following statements are true?

a. An interface declaration can be a member of an interface.
b. A method declared within an interface must have a body represented by empty curly braces.
c. An interface can implement another interface.
d. An abstract class that implements an interface must implement all abstract methods declared within the interface.
e. An abstract method declaration can be a member of an interface.

Question 14

class JJF2 {
  public static void main (String args[]) {
    System.out.print(Short.MIN_VALUE+",");
    System.out.print(Short.MAX_VALUE);
}}

What is the result of attempting to compile and run the program?

a. Prints: -32767,32768
b. Prints: -32768,32767
c. Prints: 0,65535
d. Prints: 0,65536
e. Compile-time error
f. Run-time error
g. None of the above

Question 15

class EBH202 {
  static boolean a, b, c;
  public static void main (String[] args) {
    boolean x = (a = true) || (b = true) && (c = true);
    System.out.print(a + "," + b + "," + c);
}}

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. Run-time error
j. Compile-time error
k. None of the above

Question 16

class GFC101 {
  public static void main(String[] args) {
    byte b1 = -128;    // 1
    byte b2 = 128;     // 2
    short s1 = -32769; // 3
    short s2 = 32767;  // 4
    char c1 = 0;       // 5
    char c2 = 65536;   // 6
}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6

Question 17

class B {
  private static int x1;
  public void setX(int x) {x1 = x;}
  public int getX() {return x1;}
  public static void main(String[] args) {
    int i1 = 0, j1 = 0;
    do {
      System.out.print(j1++);
      assert (i1 = j1 + x1) < 6;
    } while (i1 < 3);
}}

Assuming that the setX method is never invoked, which statements are true?

a. With assertions enabled it prints 012345 followed by an AssertionError message.
b. With assertions enabled it prints 012.
c. With assertions disabled it prints: 012345
d. With assertions disabled it prints: 012
e. With assertions disabled it attempts to print an infinite sequence of numbers.
f. With assertions disabled the variable i1 is incremented with each pass through the loop.
g. As a rule, the boolean expression of an assert statement should not be used to perform actions that are required for normal operation of the program.

Question 18

class GFM12 {
  static int x;                             // 1
  public static void main(String[] args) {  // 2
    int y;                                  // 3
    System.out.println("x="+x);             // 4
    System.out.println("y="+y);             // 5
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. Compile-time error at line 4.
e. Compile-time error at line 5.
f. Run-time error
g. None of the above

Question 19

class EBH002 {
  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, 0,
b. Prints: 1, 2, 3, 4, 3,
c. Prints: 1, 2, 3, 4, 9,
d. Prints: 1, 2, 3, 4, 12,
e. Prints: 2, 3, 4, 1, 9,
f. Prints: 2, 3, 4, 1, 3,
g. Run-time error
h. Compile-time error
i. None of the above

Question 20

class Primitives {
  static void printFloat(float f) {System.out.println(f);}
  static void printDouble(double d) {System.out.println(d);}
  public static void main(String[] args) {
    byte b = 1;     // 1
    short s = b;    // 2
    char c = s;     // 3
    int i = c;      // 4
    long l = i;     // 5
    float f = l;    // 6
    printFloat(i);  // 7
    printFloat(l);  // 8
    printDouble(l); // 9
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. None of the above

Question 21

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
  public static void main(String args[]) {
    Base base = new Base();
    I1 i1 = base;           // 1
    Sub sub = (Sub)base;    // 2
}}

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. None of the above

Question 22

class Purple {
  public static void main (String []args) {
    int[] i = {1,2,3};  // 1
    Object obj = i;     // 2
    i = obj;            // 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

Question 23

class GFC216 {
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    char 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

Question 24

class C {
  void printS1() {System.out.print("C.printS1 ");}
  static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
  void printS1(){System.out.print("D.printS1 ");}
  void printS2() {System.out.print("D.printS2 ");}
  public static void main (String args[]) {
    C c = new D(); c.printS1(); c.printS2();
}}

What is the result of attempting to compile and run the program?

a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 25

class B {
  private static String s1 = "s1";
  final String s2 = "s2";
  B () {new Z("s5","s6");}
  static class Z {
    final String s3 = "s3";
    static String s4 = "s4";
    Z (final String s5, String s6) {
      System.out.print(???);
  }}
  public static void main(String args[]) {new B();}
}

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

Question 26

Which of these words belong to the set of Java keywords?

a. virtual
b. goto
c. ifdef
d. typedef
e. friend
f. struct
g. implements
h. union
i. const

Question 27

class EBH102 {
  static int m(int i) {System.out.print(i + ","); return i;}
  public static void main(String s[]) {
    int i = 1, j = 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,0
b. Prints: 1,2,3,4,1
c. Prints: 1,2,3,4,2
d. Prints: 1,2,3,4,3
e. Prints: 1,2,3,4,4
f. Prints: 1,2,3,4,5
g. Run-time error
h. Compile-time error
i. None of the above

Question 28

class GFC303 {
  private String name;
  public GFC303(String name) {this.name = name;}
  public void setName(String name) {this.name = name;}
  public String getName() {return name;}
  public static void m1(GFC303 pet1, GFC303 pet2) {
    pet1 = new GFC303("Fish");
    pet2 = null;
  }
  public static void main (String[] args) {
    GFC303 pet1 = new GFC303("Dog");
    GFC303 pet2 = new GFC303("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,Fish
c. Prints: Fish,Cat
d. Prints: Fish,Fish
e. Compile-time error
f. Run-time error
g. None of the above

Question 29

class I {
  private String name;
  public String name() {return name;}
  public I(String s) {name = s;}
}
class J {
  public static void m1(I i) {i = null;}
  public static void main (String[] args) {
    I i = new I("X");           // 1
    m1(i);                      // 2
    System.out.print(i.name()); // 3
}}

Which of the following is a true statement?

a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. The program compiles, runs and prints X.
d. The program fails to compile.
e. None of the above


Copyright © 2002-2004, Dan Chisholm
All rights reserved.