Certified Java Programmer Mock Exam


Question 1

class EBH101 {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }
  public static void main(String s[]) {
    int i = 1;
    m(m(++i) + m(i++) + m(-i) + m(i++));
}}

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

a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Run-time error
i. Compile-time error
j. None of the above

Question 2

class EBH102 {
  static int m(int i) {
    System.out.print(i + ",");
    return i;
  }
  public static void main(String s[]) {
    int i = 1;
    int 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 3

class EBH103 {
  public static void main (String[] args) {
    byte a = 1, b = 2, c, d, e;
    c = (byte)a++;
    d = (byte)++b;
    e = (byte)a + b;
    System.out.print(c + d + e);
}}

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

a. Prints: 1 2 3
b. Prints: 6
c. Prints: 2 3 5
d. Prints: 10
e. Prints: 1 3 4
f. Prints: 8
g. Prints: 1 3 5
h. Prints: 9
i. Run-time error.
j. Compile-time error.
k. None of the above

Question 4

class EBH104 {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i = 1;
    m(m(++i) - m(i++) + m(-i) * m(~i));
}}

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

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

Question 5

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 6

class EBH106 {
  public static void main(String args[]) {
    int a = 1;
    a += ++a + a++;
    System.out.print(a);
}}

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

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

Question 7

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

Question 8

class EBH108 {
  public static void main(String s[]) {
    int i=0;
    int j = ++i + ((++i * ++i) % ++i) + ++i;
    System.out.print(j%5);
}}

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

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


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