Certified Java Programmer Mock Exam


Question 1

Which of the following are command-line switches used to enable assertions in non-system classes?

a. -ea
b. -ae
c. -aon
d. -aoff
e. -enableassertions
f. -assertionsenable
g. -assertionson
h. -assertionsoff

Question 2

Which of the following are command-line switches used to disable assertions in non-system classes?

a. -da
b. -ad
c. -aon
d. -aoff
e. -disableassertions
f. -assertionsdisable
g. -assertionson
h. -assertionsoff

Question 3

class RedException extends Exception {}
class BlueException extends Exception {}
class White {
  void m1() throws RedException {throw new RedException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c,d; a = b = c = d = 0;
    try {white.m1(); a++;}
      catch (RedException e) {b++;}
      catch (BlueException e) {c++;}
      finally {d++;}
    System.out.print(a+","+b+","+c+","+d);
}}

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

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

Question 4

Which of the following are command-line switches used to disable assertions in non-system classes?

a. -disableassertions
b. -disableAssertions
c. -assertionsDisable
d. -assertionsOn
e. -assertionsOff
f. None of the above

Question 5

Which of the following are command-line switches used to enable assertions in system classes?

a. -saon
b. -saoff
c. -esa
d. -sae
e. -systemassertionson
f. -systemassertionsoff
g. -enablesystemassertions
h. -systemassertionsenable

Question 6

Which of the following statements are true?

a. By default assertions are disabled at run-time.
b. Assertions can be selectively enabled for any named class.
c. If assertions are selectively enabled for a named class then assertions are automatically enabled for any subclass of the named class.
d. Assertions can be selectively enabled for any named package.
e. If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages.
f. Assertions can be selectively enable for the unnamed package in the current working directory.
g. Assertions can be selectively enable for any unnamed package in any named directory.

Question 7

Which of the following is thrown to indicate that an assertion has failed?

a. AssertError
b. AssertException
c. AssertionError
d. AssertionException
e. None of the above

Question 8

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

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

a. Prints: ABC
b. Prints: ABCC
c. Prints: CBA
d. Prints: ABCBCC
e. Run-time error
f. Compile-time error
g. None of the above

Question 9

class JMM123 {
  public static void main (String args[]) {
    int i = 0, j = 0, k = 0;
label1:
    for (int h = 0; h < 6; h++) {
label2:
      do { i++; k = h + i + j;
        switch (k) {
          default: break label1;
          case 1: continue label2;
          case 2: break;
          case 3: break label2;
          case 4: continue label2;
          case 5: continue label1;
        }
      } while (++j<5);
    }
    System.out.println(h + "," + i + "," + j);
}}

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

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

Question 10

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 = 1;
    try {
      throw new Level1Exception();
      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 11

class JMM124 {
  public static void main(String args[]) {
    int k;
    for (int i=0, j=0; i<2; i++,j++) {System.out.print(i);}     // 1
    for (int i=0, k=0; i<2; i++,k++) {System.out.print(i);}     // 2
    for (int i=0, int j=0; i<2; i++,j++) {System.out.print(i);} // 3
}}

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

a. Prints: 012345
b. Prints: 010101
c. Compile-time error at line 1
d. Compile-time error at line 2
e. Compile-time error at line 3
f. Run-time error

Question 12

class JMM125 {
  static int i;
  public static void main(String args[]) {
    for (i=1; i<3; i++) {System.out.print(i);}     // 1
    for (int i=1; i<3; i++) {System.out.print(i);} // 2
    int i;                                         // 3
    for (i=0; i<2; i++) {System.out.print(i);}     // 4
    System.out.print(JMM125.i);
}}

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

a. Prints: 1212010
b. Prints: 1212013
c. Compile-time error at line 1
d. Compile-time error at line 2
e. Compile-time error at line 4
f. Run-time error
g. None of the above

Question 13

class JMM126 {
  static int i;
  public static void main(String args[]) {
    for (i=1; i<3; i++)     {System.out.print(i);} // 1
    for (int i=1; i<3; i++) {System.out.print(i);} // 2
    int i;                                         // 3
    for (int i=0; i<2; i++) {System.out.print(i);} // 4
    System.out.print(JMM126.i);
}}

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

a. Prints: 1212010
b. Prints: 1212013
c. Compile-time error at line 1
d. Compile-time error at line 2
e. Compile-time error at line 4
f. Run-time error
g. None of the above

Question 14

class JMM114 {
  public static void main (String[] args) {
    int i = 0, j = 0;
    while (i++ < 3) do
      System.out.print(j);
    while (j++ < 3);
}}

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

a. Prints: 0001
b. Prints: 001122
c. Prints: 012012
d. Prints: 012345
e. Prints: 1112
f. Prints: 111222
g. Prints: 121212
h. Run-time error
i. Compile-time error
j. None of the above

Question 15

class JMM115 {
  static int m1(String s, int i) {
    System.out.print(s + i);
    return i;
  }
  public static void main (String[] args) {
    int i = 0, j = 0, k = 0;
    do while (m1("i", ++i) < 2)
      System.out.print("k" + ++k);
    while (m1("j", ++j) < 2);
}}

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

a. Prints: i1k1i2k2j1i3j2
b. Prints: i1k1i2k2j1i1k1i2k2j2
c. Prints: i1k1i2j1i3j2
d. Run-time error
e. Compile-time error
f. None of the above

Question 16

class JMM116 {
  static int m1(String s, int i) {
    System.out.print(s + i);
    return i;
  }
  public static void main (String[] args) {
    int j = 0;
    for (int i = m1("A",0); m1("B",i) < 2; m1("C",++i)) {
      m1("J",++j);
    }
}}

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

a. Prints: A0B0C1J1B1C2J2B2
b. Prints: A0B0J1C1B1J2C2B2
c. Prints: A0B0J1C1A1B1J2C2A2B2
d. Run-time error
e. Compile-time error
f. None of the above

Question 17

class JMM117 {
  public static void main (String[] args) {
    int i = 0, j = 9;
    do {
      i++;
      if (j-- < i++) {break;}
    } while (i < 5);
    System.out.print(i + "," + j);
}}

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

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

Question 18

class JMM118 {
  public static void main (String[] args) {
    int i = 0, j = 9;
    while (i++ <= j--) {i++; if (j < 5) break;}
    System.out.print(i + "," + j);
}}

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

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 7,2
d. Prints: 8,5
e. Prints: 9,4
f. Run-time error
g. Compile-time error
h. None of the above

Question 19

class JMM119 {
  public static void main (String[] args) {
    int i = 0, j = 9;
    do {
      if (j < 4) {break;} else if (j-- < 7) {continue;}
      i++;
    } while (i++ < 7);
    System.out.print(i + "," + j);
}}

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

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Run-time error
h. Compile-time error
i. None of the above

Question 20

class JMM120 {
  public static void main (String args[]) {
    int i = 0, j = 0, k = 0;
label1:
    for (;;) { i++;
label2:
      do {
        k = i + j;
        switch (k) {
          case 0: continue label2;
          case 1: continue label1;
          case 2: break;
          case 3: break label2;
          case 4: break label1;
          default: break label1;
        }
      } while (++j<5);
    }
    System.out.println(i + "," + j);
}}

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

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

Question 21

class JMM121 {
  public static void main (String args[]) {
    int h = 0, i = 0, j = 0, k = 0;
label1:
    for (;;) { h++;
label2:
      do { i++; k = h + i + j;
        switch (k) {
          default: break label1;
          case 1: continue label1;
          case 2: break;
          case 3: break label2;
          case 4: continue label2;
          case 5: continue label1;
        }
      } while (++j < 5);
    }
    System.out.println(h + "," + i + "," + j);
}}

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

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


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