Certified Java Programmer Mock Exam


Question 1

public class Basics {}   // 1
public class Basics2 {}  // 2
public class Basics3 {}  // 3
public class Basics4 {}  // 4

Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. A compile-time error is not generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

Question 2

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

a. qualified
b. record
c. repeat
d. restricted
e. label
f. to
g. type
h. until
i. value
j. virtual
k. xor
l. None of the above

Question 3

class MCZ30 {
  public static void main (String[] args) {
    char a = '\t';  // 1
    char b = '\\';  // 2
    char c = '\?';  // 3
    char d = '\"';  // 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 4

class JSC205 {
  static int m1(int i) {return i;}     // 1
  static void m2(int i) {return i;}    // 2
  static int m3(int i) {return;}       // 3
  public static void main(String[] args) {
    System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}

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

a. Prints: 123
b. Prints: 6
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.

Question 5

Which of the following modifiers can be applied to a local class?

a. public
b. protected
c. private
d. abstract
e. static
f. final

Question 6

class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 3;
    try {
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
      } a++; }
      catch (Level2Exception e) {b++;}
      finally {c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}

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

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

Question 7

Which of the following is a modifier that can be applied to an interface that is a member of a directly enclosing class or interface?

a. static
b. synchronized
c. transient
d. volatile
e. implements
f. None of the above.

Question 8

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

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

a. Prints: 0000,ffff
b. Prints: 00000000,ffffffff
c. Prints: 7fff,8000
d. Prints: 8000,7fff
e. Prints: 7fffffff,80000000
f. Prints: 80000000,7fffffff
g. Compile-time error
h. Run-time error
i. None of the above

Question 9

A class can not be called "tightly encapsulated" unless which of the following are true?

a. The data members can not be directly manipulated by external code.
b. The class is declared final.
c. It has no public mutator methods.
d. The superclass is tightly encapsulated.

Question 10

A timeout argument can be passed to which of the following methods?

a. join
b. notify
c. notifyAll
d. run
e. sleep
f. start
g. yield
h. wait

Question 11

Which of these methods modify the contents of the String instance on which it is invoked?

a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. valueOf
h. None of the above.

Question 12

class SRC106 {
  public static void main(String[] args) {
    double d1 = Math.floor(0.5);
    double d2 = Math.floor(1.5);
    System.out.print(d1 + "," + d2);
}}

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

a. Prints: 0.0,1.0
b. Prints: 0.0,2.0
c. Prints: 1.0,1.0
d. Prints: 1.0,2.0
e. Compile-time error
f. Run-time error
g. None of the above

Question 13

class MWC204 {
  static void m1(StringBuffer s1) {
    s1 = s1.append("B"); System.out.print(s1);
  }
  static void m2(StringBuffer s1) {
    s1.append("C"); System.out.print(s1);
  }
  public static void main(String[] s) {
    StringBuffer s1 = new StringBuffer("A");
    m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AAA
b. Prints: ABAA
c. Prints: ABACA
d. Prints: ABABAB
e. Prints: ABABCAB
f. Prints: ABABCABC
g. Prints: ABCABCABC
h. Compile-time error
i. Run-time error
j. None of the above

Question 14

class E {
  public static void main (String[] args) {
    Byte b1 = new Byte("1"), b2 = new Byte("1");
    int a = b1.hashCode(), b = b2.hashCode();
    System.out.print((b1.equals(b2))+","+(a==b));
}}

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 15

class JMM104 {
  public static void main (String args[]) {
    char c = 'b';
    switch(c) {
      case 'a': System.out.print("1");
      case 'b': System.out.print("2");
      case 'c': System.out.print("3");
      default:  System.out.print("4");
}}}

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

a. Prints: 3
b. Prints: 34
c. Prints: 234
d. Prints: 1234
e. Run-time error
f. Compile-time error
g. None of the above

Question 16

class C {
  String m1(int i) {
    switch (i) {
      case 0: return "A";
      case 1: return "B";
      case 2: return "C";
      default: throw new AssertionError();
  }}
  public static void main(String[] args) {
    C c = new C();
    for (int i = 0; i < 4; i++) {
      System.out.print(c.m1(i));
}}}

Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example an assert statement could not be used in place of the "throw" statement.

Question 17

class I {
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private void m1() {
    I i1 = new I(), i2 = new I();
    I i3 = new I(), i4 = new I();
    i1.other(i3); i2.other(i1);
    i3.other(i2); i4.other(i4);
  }
  public static void main (String[] args) {
    new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

Question 18

class GFM15 {
  static int a; static float b; static double c;
  static boolean d; static String s;
  public static void main(String[] args) {
    System.out.println(a+","+b+","+c+","+d+","+s);
}}

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

a. Prints: 0,0,0,false,null
b. Prints: 0,0,0,false,
c. Prints: 0,0.0,0.0,false,null
d. Prints: 0,0.0,0.0,false,
e. Prints: 0,0.0,0.0,true,null
f. Prints: 0,0.0,0.0,true,
g. Prints: 0,0,0,true,null
h. Prints: 0,0,0,true,
i. Compile-time error
j. Run-time error
k. None of the above

Question 19

class EBH007{
  public static void main (String[] s) {
    byte b = 5;
    System.out.println(b<<33);
}}

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

a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Run-time error
g. Compile-time error
h. None of the above

Question 20

class Sienna {
  static double a;
  static float b;
  static int c;
  static char d;
  public static void main(String[] args) {
    a = b = c = d = 'a';
    System.out.println(a+b+c+d == 4 * 'a');
}}

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

a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above

Question 21

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

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. Compile-time error at line 4
h. Run-time error at line 4
i. None of the above

Question 22

class White {
  public static void main(String args[]) {
    int[] i = {1,2,3,4,5};     // 1
    long[] l1 = new long[5];   // 2
    long []l2 = l1;            // 3
    long l3[] = (long[])i;     // 4
    long l4[] = new long[5];   // 5
    l4[1] = i[1];              // 6
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the above

Question 23

class GFC200 {}
class GFC201 {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  static void m(GFC200 x) {System.out.print("GFC200");}
  public static void main(String[] args) {
    m(null);
}}

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

a. Prints: Object
b. Prints: String
c. Prints: GFC200
d. Compile-time error
e. Run-time error
f. None of the above

Question 24

class R {
  private void printS1(){System.out.print("R.printS1 ");}
  protected void printS2() {System.out.print("R.printS2 ");}
  protected void printS1S2(){printS1();printS2();}
}
class S extends R {
  private void printS1(){System.out.print("S.printS1 ");}
  protected void printS2(){System.out.print("S.printS2 ");}
  public static void main(String[] args) {
    new S().printS1S2();
}}

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

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

Question 25

class E {
  E() {System.out.print("E");}
  static class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
   new E.Z();
}}

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

a. Prints: E
b. Prints: Z
c. Prints: EZ
d. Prints: ZE
e. Run-time error
f. Compile-time error
g. None of the above

Question 26

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 {
  public static void main(String[] args) {
    A a1 = new A(); A b1 = new B();
    A c1 = new C(); C c4 = new C();
    a1.m1(c4); b1.m1(c4); c1.m1(c4);
}}

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

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above

Question 27

class EBH105 {
  static int m(int i) {
    System.out.print(i + ",");
    return 0;
  }
  public static void main (String[] args) {
    int i = 0;
    i = i++ + m(i);
    System.out.print(i);
}}

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

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

Question 28

class GFC306 {
  static int[] i1 = {1}, i2 = {3};
  static void m1(int[] i1) {
    int[] i3 = i1; i1 = i2; i2 = i3;
  }
  public static void main (String[] args) {
    m1(i1);
    System.out.print(i1[0] + "," + i2[0]);
}}

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

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


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