Certified Java Programmer Mock Exam


Question 1

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

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

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

Question 2


class GRC10 {
  public static void main (String[] s) {
    System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 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: ABC
b. Prints: BCD
c. Prints: CDE
d. Prints: A B C
e. Prints: B C D
f. Prints: C D E
g. Compile-time error
h. Run-time error
i. None of the above

Question 3

class EBH201 {
  public static void main (String[] args) {
    int a = 1 || 2 ^ 3 && 5;
    int b = ((1 || 2) ^ 3) && 5;
    int c = 1 || (2 ^ (3 && 5));
    System.out.print(a + "," + b + "," + c);
}}

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

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

Question 4

class GFC100 {
  public static void main(String[] args) {
    final short s1 = 1; // 1
    final char c1 = 1;  // 2
    byte b1 = s1;       // 3
    byte b2 = c1;       // 4
    byte b3 = 1;        // 5
    byte b4 = 1L;       // 6
    byte b5 = 1.0;      // 7
    byte b6 = 1.0d;     // 8
}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8

Question 5

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 6

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 7

class EBH001 {
  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, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Run-time error
e. Compile-time error
f. None of the above

Question 8

class Magenta {
  static byte a = (byte)127;
  static byte b = (byte)128;
  static byte c = (byte)255;
  static byte d = (byte)256;
  public static void main(String args[]) {
    System.out.print(a + " " + b + " " + c + " " + d);
}}

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

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

Question 9

interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
  public static void main(String args[]) {
    Sub s1 = new Sub();
    I2 i2 = s1;          // 1
    I1 i1 = s1;          // 2
    Base base = s1;      // 3
    Sub s2 = (Sub)base;  // 4
}}

A compile-time error is generated at which line?

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

Question 10

class Green {
  public static void main (String args[]) {
    int[] i = null;  // 1
    Cloneable c = i; // 2
    i = (int [])c;   // 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 11

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

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 13

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 14

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 15

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 16

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 17

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 18

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

Question 19

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


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