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?
Question 2
class MCZ29 {
public static void main (String[] args) {
char a = '\a'; // 1
char b = '\b'; // 2
char c = '\f'; // 3
char d = '\n'; // 4
char e = '\r'; // 5
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | None of the above |
Question 3
class C {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));
}}
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. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
Question 4
Which of the instance creation expressions
produce a run-time error?
| a. | new Float('A') |
| b. | new Float("A") |
| c. | new Float(1L) |
| d. | new Float("1L") |
| e. | new Float(0x10) |
| f. | new Float("0x10") |
| g. | new Float("010") |
Question 5
Which of the following class instance creation expressions
would generate a run-time error?
| a. | new Short("1") |
| b. | new Short("-1") |
| c. | new Short("+1") |
| d. | new Short("1.0") |
| e. | new Short("0x1") |
| f. | new Short("011") |
Question 6
| | • | Entries are organized as key/value pairs. |
| | • | Duplicate entries replace old entries. |
Which interface of the java.util package
offers the specified behavior?
| a. | List |
| b. | Map |
| c. | Set |
| d. | None of the above |
Question 7
import java.util.*;
class GFC102 {
public static void main (String args[]) {
Object a = new HashSet();
System.out.print((a instanceof Set)+",");
System.out.print(a instanceof SortedSet);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | None of the above |
Question 8
If two instances of a class type
are not equal according to the
equals
method,
then the same integer value must not be returned by the
hashCode
method of the two objects.
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
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 11
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 12
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 13
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 14
Which of the following modifiers can be applied to a
member class?
| a. | abstract |
| b. | final |
| c. | public |
| d. | protected |
| e. | private |
| f. | static |
| g. | synchronized |
| h. | transient |
Question 15
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 2;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,1,0,0,1 |
| b. | Prints: 0,1,0,0,0,0 |
| c. | Prints: 0,1,1,0,0,1 |
| d. | Prints: 0,1,0,0,0,1 |
| e. | Prints: 1,1,1,0,0,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 16
Which of the following are modifiers that can be applied
to an interface that is a member of a directly enclosing class?
| a. | abstract |
| b. | extends |
| c. | final |
| d. | private |
| e. | protected |
| f. | public |
Question 17
class JJF4 {
public static void main(String args[]) {
System.out.print(Long.toHexString(Byte.MAX_VALUE)+",");
System.out.print(Long.toHexString(Character.MAX_VALUE)+",");
System.out.print(Long.toHexString(Short.MAX_VALUE));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: f,ff,7f |
| b. | Prints: f,ff,ff |
| c. | Prints: 7f,ffff,7fff |
| d. | Prints: ff,ffff,ffff |
| e. | Prints: 7fff,ffffff,7fffff |
| f. | Prints: ffff,ffffff,ffffff |
| g. | Compile-time error |
| h. | Run-time error |
| i. | 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 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 20
Which statements are true?
| a. | Assertions should never be placed at any point in the code that should not be reached under normal circumstances. |
| b. | The compiler will generate an error if an assert statement is "unreachable" as defined by the Java Language Specification. |
| c. | A catch clause should not be used to catch an AssertionError. |
| d. | AssertionError is a checked exception. |
Question 21
class Q {
private int id;
public void finalize() {System.out.print(id);}
public Q(int i) {id = i;}
}
class R {
public static void main(String[] args) {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(i); // 1
}
System.gc(); // 2
}}
When the processing of line 2 begins, how many objects
of type Q that were created at line 1 are eligible
for garbage collection?
| a. | 0 |
| b. | 1 |
| c. | 9 |
| d. | 10 |
| e. | Indeterminate. |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 22
class GFM14 {
static byte a; static short b; static char c;
static int d; static long e; static String s;
public static void main(String[] args) {
System.out.println(a+","+b+","+(int)c+","+d+","+e+","+s);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,0,0,null |
| b. | Prints: 0,0,0,0,0, |
| c. | Prints: 0,0, ,0,0, |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
Question 23
class EBH005 {
public static void main (String[] s) {
byte b = 127; b <<= 2;
System.out.println(b);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: -4 |
| b. | Prints: -3 |
| c. | Prints: -2 |
| d. | Prints: 0 |
| e. | Prints: 1 |
| f. | Prints: 127 |
| g. | Prints: 508 |
| h. | Run-time error |
| i. | Compile-time error |
| j. | None of the above |
Question 24
class Teal {
public static void main (String[] args) {
byte b = 1; // 1
long l = 1000; // 2
b += l; // 3
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | None of the above |
Question 25
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
public static void main(String []args) {
Base[] base = {new Base()}; // 1
Sub sub[] = {new Sub()}; // 2
Object obj = sub; // 3
base = obj; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 26
abstract class A {
private int x = 4;
private int y = 2;
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
}
interface B {int math();}
class C {
static class D extends A implements B {
public int math() {return x()+y();}
}
static A a1 = new A() implements B {
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(new C.D().math());
System.out.print(a1.math());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 12 |
| b. | Prints: 66 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
Question 27
Which of these lists contains at least one word that is not a Java keyword?
| a. | interface, static, void, catch, final |
| b. | char, strictfp, finally, long, volatile |
| c. | native, super, class, float, while |
| d. | const, for, new, switch, import |
| e. | continue, finalize, goto, package, synchronized |
| f. | None of the above |
Question 28
class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: ATBFCT |
| b. | Prints: ATBFCTDF |
| c. | Prints: BFCTAT |
| d. | Prints: BTCTDFAT |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 29
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 |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.