Certified Java Programmer Mock Exam


Question 1

public class Basics {}      // 1
class Basics1 {}            // 2
protected class Basics2 {}  // 3
private class Basics3 {}    // 4
Class Basics4 {}            // 5

Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5

Question 2

public class Basics {}   // 1
public class Basics2 {}  // 2
public class Basics3 {}  // 3
public class Basics4 {}  // 4

Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. A compile-time error is not generated at which line?

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

Question 3

Which of the following is used to prevent the serialization of a non-static field?

a. final
b. protected
c. synchronized
d. transient
e. volatile
f. native

Question 4

Which of the following modifiers can not be used with the abstract modifier in a method declaration?

a. final
b. private
c. protected
d. public
e. static
f. synchronized
g. native

Question 5

class JSC203 {
  static int m1(byte b) {return b;}   // 1
  static int m2(char c) {return c;}   // 2
  static int m3(long l) {return l;}   // 3
  public static void main(String[] args) {
    byte b = 1; char c = '\u0002'; long l = 4L;
    System.out.print(""+m1(b)+m2(c)+m3(l));
}}

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

a. Prints: 124
b. Prints: 7
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Run-time error

Question 6

public class A {int i1; void m1() {}}

Which of the following statements are true?

a. class A extends Object.
b. Field i1 is implicitly public, because class A is public.
c. Method m1 is implicitly public, because class A is public.
d. The compiler will insert a default constructor implicitly.
e. The default constructor has no throws clause.
f. The default constructor of class A has package access.
g. The default constructor accepts one parameter for each field in class A.
h. The default constructor invokes the no-parameter constructor of the superclass.

Question 7

Which of the following statements are true?

a. The compiler will create a default constructor if no other constructor is declared.
b. The default constructor takes no arguments.
c. If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass.
d. The default constructor declares Exception in the throws clause.
e. The default constructor is always given the private access modifier.
f. The default constructor is always given the public modifier.
g. The default constructor is always given default package access.

Question 8

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
   public void m1() {System.out.print("A.m1, ");}
   protected void m2() {System.out.print("A.m2, ");}
   private void m3() {System.out.print("A.m3, ");}
   void m4() {System.out.print("A.m4, ");}
}
// Class C is declared in a file named C.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
  public static void main(String[] args) {
    C c = new C();
    c.m1();  // 1
    c.m2();  // 2
    c.m3();  // 3
    c.m4();  // 4
}}

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

a. Prints: A.m1, A.m2, A.m3, A.m4,
b. Compile-time error at 1.
c. Compile-time error at 2.
d. Compile-time error at 3.
e. Compile-time error at 4.

Question 9

class MWC104 {
  public static void main(String[] args) {
    int[5] a1;  // 1
    int []a2;   // 2
    int[   ]a3; // 3
    int a4[];   // 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

Which of the following statements are true?

a. A value can not be assigned to a final field more than once.
b. A value can be assigned to a final field at any time or not at all.
c. Only static variables can be declared final.
d. A compile-time error is thrown if a blank final instance variable is not assigned a value before the end of each constructor.
e. A field can not be declared both final and volatile.

Question 11

Which of the following statements is true?

a. An abstract method can not be overridden by an abstract method.
b. An instance method that is not abstract can not be overridden by an abstract method.
c. An abstract method declaration can not include a throws clause.
d. The body of an abstract method is represented by a set of empty brackets.
e. None of the above.

Question 12

class JSC204 {
  static int m1(short s)  {return s;}   // 1
  static int m2(float f) {return f;}    // 2
  public static void main(String[] args) {
    short s = 3; float f = 5.0f;
    System.out.print(""+m1(s)+m2(f));
}}

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

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

Question 13

Suppose that the compiler generates a default constructor for a class. If a compile-time error is to be avoided, which of the following must be true?

a. The superclass must not have any constructor other than a default constructor.
b. The superclass must not have an accessible no-argument constructor.
c. The no-argument superclass constructor must not have a throws clause that includes a checked exception.
d. The no-argument superclass constructor must be declared private.
e. None of the above

Question 14

class JSC101 {
  void m1() {
    public int a;      // 1
    protected int b;   // 2
    private int c;     // 3
    static int d;      // 4
    transient int e;   // 5
    volatile int f;    // 6
    final int g = 1;   // 7
}}

Compile-time errors are generated at which lines?

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

Question 15

Which of the following statements is not true?

a. A static method is also known as a class method.
b. A class method is not associated with a particular instance of the class.
c. The keyword, this, can not be used inside the body of a static method.
d. The keyword, super, may be used in the body of a static method.
e. A method that is not static is known as an instance method.
f. None of the above.

Question 16

class JSC205 {
  static int m1(int i) {return i;}     // 1
  static void m2(int i) {return i;}    // 2
  static int m3(int i) {return;}       // 3
  public static void main(String[] args) {
    System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}

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

a. Prints: 123
b. Prints: 6
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.

Question 17

class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {} // 3

Which of the following statements is true?

a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. None of the above

Question 18

class MWC204 {
  public static void main(String[] args) {
    int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
    System.out.print(a1.length);
}}

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

a. Prints: 0
b. Prints: 3
c. Prints: 4
d. Prints: 9
e. Prints: 10
f. Prints: 11
g. Compile-time error
h. Run-time error
i. None of the above

Question 19

class MWC105 {
  static boolean b1;
  public static void main(String[] args) {
    boolean[] array = new boolean[1];
    boolean b2;
    System.out.print(b1+",");
    System.out.print(array[0]+",");
    System.out.print(b2);
}}

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

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

Question 20

class MWC205 {
  public static void main(String[] args) {
    int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
    for (int i = 0; i < a1.length; i++) {
      System.out.print(a1[i].length+",");
}}}

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

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


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