Certified Java Programmer Mock Exam


Question 1

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 2

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

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

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

Question 3

class JMM107 {
  public static void main(String[] args) {
    boolean b = true;
    if (b = false) {System.out.print("A");
    } else if (b) {System.out.print("B");
    } else {System.out.print("C");}
}}

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

a. Prints: A
b. Prints: B
c. Prints: C
d. Run-time error
e. Compile-time error
f. None of the above

Question 4

class JMM108 {
  static boolean b;
  public static void main(String[] args) {
    if (b) {System.out.print("A");
    } else if (b = false) {System.out.print("B");
    } else if (b) {System.out.print("C");
    } else if (!b) {System.out.print("D");
    } else {System.out.print("E");}
}}

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

a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Run-time error
g. Compile-time error
h. None of the above

Question 5

class JMM109 {
  public static void main(String[] args) {
    boolean b;
    if (b = false) {System.out.print("A");
    } else if (b) {System.out.print("B");
    } else if (!b) {System.out.print("C");
    } else {System.out.print("D");}
}}

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

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

Question 6

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 7

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 8

class JMM103 {
  public static void main(String args[]) {
    byte b = -1;
    switch(b) {
      case 0:    System.out.print("zero "); break;
      case 100:  System.out.print("100 ");  break;
      case 1000: System.out.print("1000 "); break;
      default: System.out.print("Default ");
}}}

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

a. Prints: zero
b. Prints: 100
c. Prints: 1000
d. Prints: Default
e. Run-time error
f. Compile-time error
g. None of the above

Question 9

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 10

class JMM105 {
  public static void main(String args[]) {
    int x = 6; int success = 0;
    do  {
      switch(x) {
        case 0: System.out.print("0"); x += 5; break;
        case 1: System.out.print("1"); x += 3; break;
        case 2: System.out.print("2"); x += 1; break;
        case 3: System.out.print("3"); success++; break;
        case 4: System.out.print("4"); x -= 1; break;
        case 5: System.out.print("5"); x -= 4; break;
        case 6: System.out.print("6"); x -= 5; break;
      }
    } while ((x != 3) || (success < 2));
}}

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

a. Prints: 60514233
b. Prints: 6152433
c. Prints: 61433
d. Prints: 6143
e. Run-time error
f. Compile-time error

Question 11

class JMM106 {
  public static void main(String args[]) {
    int x = -5; int success = 0;
    do  {
      switch(x) {
        case 0: System.out.print("0"); x += 5; break;
        case 1: System.out.print("1"); x += 3; break;
        case 2: System.out.print("2"); x += 1; break;
        case 3: System.out.print("3"); success++; break;
        case 4: System.out.print("4"); x -= 1; break;
        case 5: System.out.print("5"); x -= 4; break;
        case 6: System.out.print("6"); x -= 5; break;
        default: x += x < 0 ? 2 : -2;
      }
    } while ((x != 3) || (success < 2));
}}

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

a. Prints: 60514233
b. Prints: 1433
c. Prints: 61433
d. Prints: 051433
e. Run-time error
f. Compile-time error

Question 12

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 13

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 14

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 15


class JMM101 {
  public static void main(String[] args) {
    int i = 0;
    while (i++ < args.length) {
      System.out.print(args[i]);
}}}
java JMM101 A B C D E F

What is the result of attempting to compile and run the program using the specified command line?

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

Question 16

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

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

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

Question 17

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

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

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

Question 18

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

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

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

Question 19

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

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

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

Question 20

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 21

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 22

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 23

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 24

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 25

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 26

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 27

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.