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 GFC402 {
  static int x=1;
  void m1(int i) {x++; i++;}
  public static void main (String[] args) {
    int y=3; m1(y);
    System.out.println(x + "," + y);
}}

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

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

Question 3

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

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

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

Question 4

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

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

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

Question 5

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 6

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

Question 7

class GFC304 {
  static void m1(int[] i1, int[] i2) {
    int[] i3 = i1; i1 = i2; i2 = i3;
  }
  public static void main (String[] args) {
    int[] i1 = {1}, i2 = {3}; m1(i1, i2);
    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

Question 8

class GFC305 {
  static void m1(int[] i1, int[] i2) {
    int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
  }
  public static void main (String[] args) {
    int[] i1 = {1}, i2 = {3}; m1(i1, i2);
    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

Question 9

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

Question 10

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

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

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

Question 11

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

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

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


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