0% found this document useful (0 votes)
43 views187 pages

Java Programmer Mock Exam Questions

The document contains a series of programming questions related to Java language fundamentals, focusing on class structures, keywords, and compile/run outcomes. Each question presents a code snippet or a concept, followed by multiple-choice answers regarding errors or outputs. The questions assess understanding of Java syntax, data types, and the behavior of the Java compiler and runtime environment.

Uploaded by

LucianoCanonico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views187 pages

Java Programmer Mock Exam Questions

The document contains a series of programming questions related to Java language fundamentals, focusing on class structures, keywords, and compile/run outcomes. Each question presents a code snippet or a concept, followed by multiple-choice answers regarding errors or outputs. The questions assess understanding of Java syntax, data types, and the behavior of the Java compiler and runtime environment.

Uploaded by

LucianoCanonico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Capítulo 2 – Language Fundamentals (A)

Question 1
1. class Basics1 {
2. public static void main(String[] args) {}
3. }
4. class Basics2 {
5. public static void main(String []args) {}
6. }
7. class Basics3 {
8. public static void main(String args[]) {}
9. }
What is the result of attempting to compile and run the above programs?
a. Compiler error at line 2.
b. Compiler error at line 5.
c. Compiler error at line 8.
d. Run time error at line 2.
e. Run time error at line 5.
f. Run time error at line 8.
g. None of the Above

Question 2
class Basics {
public static void main (String[] args) {
int x;
int y;
int z;
System.out.println(x+y+z);
}
}
What is the result of attempting to compile and run the above program?
a. Prints nothing.
b. Prints an undefined value.
c. Prints: null
d. Prints: 0
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 3
Which of these words belong to the set of java keywords?
a. next
b. catch
c. function
d. instanceof
e. mod
f. const
g. or
h. Boolean
i. goto
j. import
k. transient
l. None of the Above

Question 4
class Blue {
public static void main (String[] args) {
String s = null;
System.out.print(s);
}
}
What is the result of attempting to compile and run the program?
a. Prints nothing.
b. Prints: null
c. Compiler error
d. Runtime error
e. None of the Above

Question 5
Which of these words belong to the set of java keywords?
a. declare
b. global
c. const
d. preserve
e. continue
f. Float

Certified Java Programmer Mock Exam 1


g. extends
h. select
i. break
j. dim
k. instanceOf
l. None of the Above

Question 6
1. class Basics1 {
2. public void main(String[] args) {}
3. }
4. class Basics2 {
5. public void main(String []args) {}
6. }
7. class Basics3 {
8. public void main(String args[]) {}
9. }
What is the result of attempting to compile and run the above programs?
a. Compiler error at line 2.
b. Compiler error at line 5.
c. Compiler error at line 8.
d. Run time error at line 2.
e. Run time error at line 5.
f. Run time error at line 8.
g. None of the Above

Question 7
Which of these words belong to the set of java keywords?
a. dispose
b. goto
c. begin
d. package
e. synchronized
f. default
g. interface
h. volatile
i. addressof
j. None of the Above

Question 8
class A {
public static void main (String[] args) {
// Insert code here.
}
}
Which of the following lines can be inserted at the specified location without generating a compile-time error?
a. char a = a;
b. char b = abc;
c. char c = \u0041;
d. char d = \uabcd;
e. None of the Above

Question 9
Which of these words belong to the set of java keywords?
a. virtual
b. goto
c. ifdef
d. typedef
e. friend
f. struct
g. implements
h. union
i. const
j. None of the Above

Question 10
class Violet {
public static void main (String[] s) {
System.out.print(s[1] + s[2] + s[3]);
}
}
Assume the above main method is invoked using the following
command line.
java Violet 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
Certified Java Programmer Mock Exam 2
e. Prints: B C D
f. Prints: C D E
g. Compiler Error
h. Run time Exception
i. None of the Above

Question 11
class D {
public static void main (String args[]) {
System.out.print(Byte.MIN_VALUE+",");
System.out.print(Byte.MAX_VALUE);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,255
b. Prints: 0,256
c. Prints: -127,128
d. Prints: -128,127
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 12
class A {
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. Compiler Error
h. Runtime Error
i. None of the Above

Question 13
class A {
public static void main (String[] args) {
// Insert code here.
}
}
Which of the following lines can be inserted at the specified location without generating a compile-time error?
a. boolean b1 = true;
b. boolean b2 = TRUE;
c. boolean b3 = 'true';
d. boolean b4 = "TRUE";
e. boolean b5 = "Very True";
f. boolean b6 = 0;
g. None of the Above

Question 14
class E {
public static void main (String args[]) {
System.out.print(Short.MIN_VALUE+",");
System.out.print(Short.MAX_VALUE);
}
}
What is the result of attempting to compile and run the program?
a. Prints: -32767,32768
b. Prints: -32768,32767
c. Prints: 0,65535
d. Prints: 0,65536
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 15
class C {
public static void main(String args[]) {
System.out.print(Long.toHexString(Long.MIN_VALUE)+",");
System.out.print(Long.toHexString(Long.MAX_VALUE));
}
}
Certified Java Programmer Mock Exam 3
What is the result of attempting to compile and run the program?
a. Prints: 80000000,7fffffff
b. Prints: 8000000000000000,7fffffffffffffff
c. Prints: 7fffffff,80000000
d. Prints: 7fffffffffffffff,8000000000000000
e. Prints: 00000000,ffffffff
f. Prints: 0000000000000000,ffffffffffffffff
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 16
1. class Basics1 {
2. public static void main(String[] args) {}
3. }
4. class Basics2 {
5. protected static void main(String[] args) {}
6. }
7. class Basics3 {
8. private static void main(String[] args) {}
9. }
What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line using jdk
1.4 or any jdk that is compliant with section 12.1.4 of The Java Language Specification?
a. Compiler error at line 2.
b. Compiler error at line 5.
c. Compiler error at line 8.
d. Run time error at line 2.
e. Run time error at line 5.
f. Run time error at line 8.
g. None of the Above

Question 17
class Teal {
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 above program?
a. Prints: true,true,true
b. Prints: false,false,false
c. Prints: null,null,null
d. Prints: false,true,false
e. Compiler Error.
f. Runtime Error.
g. None of the Above.

Question 18
class Gray {
static int a;
static float b;
static double c;
static boolean d;
static String s;
public static void main(String[] args) {
System.out.println(a+","+b+","+c+","+d+","+s);
}
}
What is the result of attempting to compile and run the above program?
a. Prints 0,0,0,false,null
b. Prints 0,0,0,false,
c. Prints 0,0.0,0.0,false,null
d. Prints 0,0.0,0.0,false,
e. Prints 0,0.0,0.0,true,null
f. Prints 0,0.0,0.0,true,
g. Prints 0,0,0,true,null
h. Prints 0,0,0,true,
i. Compiler Error.
j. Runtime Error.
k. None of the Above.

Question 19
class White {
static byte a;
static short b;
Certified Java Programmer Mock Exam 4
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 above program?
a. Prints: 0,0,0,0,0,null
b. Prints: 0,0,0,0,0,
c. Prints: 0,0, ,0,0,
d. Compiler Error.
e. Runtime Exception.
f. None of the Above.

Capítulo 2 – Language Fundamentals (B)


Question 1
Which of these words belong to the set of java keywords?
a. Clause
b. Loop
c. Expression
d. Overrides
e. Statement
f. Assertion
g. Validate
h. Exception
i. Cast
j. None of the Above

Question 2
Which of these words belong to the set of java keywords?
a. Exit
b. Strictfp
c. Enum
d. Super
e. Abort
f. Event
g. Goto
h. Native
i. Exception
j. None of the Above

Question 3
Which of these words belong to the set of java keywords?
a. Exit
b. Boolean
c. Short
d. Include
e. Const
f. Sizeof
g. Instanceof
h. Register
i. Goto
j. None of the Above

Question 4
class A {
public static void main (String[] args) {
// Insert code here.
}
}
Which of the following lines can be inserted at the specified location without generating a compile-time error?
a. String a = 'a';
b. String b = 'abc';
c. String c = '\u0041';
d. String d = '\uabcd';
e. None of the Above

Question 5
class Basics {
private static int x;
protected static int y;
public static int z;
public static void main (String[] args) {
System.out.println(x+y+z);

Certified Java Programmer Mock Exam 5


}
}
What is the result of attempting to compile and run the above program?
a. Prints nothing.
b. Prints an undefined value.
c. Prints: null
d. Prints: 0
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 6
1. class Green {
2. public static void main (String[] args) {
3. char a = '\b';
4. char b = '\c';
5. char c = '\d';
6. char d = '\f';
7. char e = '\l';
8. char f = '\n';
9. char g = '\r';
10. char h = '\t';
11. char i = '\\';
12. char j = '\"';
13. char k = '\'';
14. }
15. }
What is the result of attempting to compile and run the program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Compiler error at line 8.
g. Compiler error at line 9.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. None of the Above

Question 7
Which of these words belong to the set of java keywords?
a. Transient
b. Serializable
c. Runnable
d. Run
e. Volatile
f. externalizable
g. Cloneable
h. None of the Above

Question 8
Which of these words belong to the set of java keywords?
a. Byte
b. Short
c. Int
d. Long
e. Decimal
f. int64
g. Float
h. Single
i. Double
j. Boolean
k. Char
l. Unsigned
m. Array
n. String
o. None of the Above

Question 9
class A {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}
}
Certified Java Programmer Mock Exam 6
A compile-time error is generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 10
class A {
public static void main (String[] args) {
String a = "abcd"; // 1
String b = "'\u0041'"; // 2
String c = "\u0041"; // 3
String d = "\uabcd"; // 4
System.out.print(a+b+c+d); // 5
}
}
A compile-time error is generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the Above

Question 11
class A {
public static void main(String args[]) {
System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toHexString(Byte.MAX_VALUE));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 1111111,177,127,7f
b. Prints: 11111111,377,256,ff
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 12
class A {
public static void main(String args[]) {
byte x = 85;
System.out.print(Integer.toBinaryString(x)+",");
System.out.print(Integer.toOctalString(x)+",");
System.out.print(Integer.toString(x)+",");
System.out.print(Integer.toHexString(x));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 11111111,377,85,ff
b. Prints: 1010101,125,85,55
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 13

class Orange {
public static void main (String[] args) {
char c = '\u000a'; // '\u000a' = Linefeed
System.out.print("ABC" + c);
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABC
b. Prints: ABCc
c. Prints: ABC10
d. Prints: ABCa
e. Prints: ABC0x000a
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 14
class A {
public static void main (String[] args) {
Certified Java Programmer Mock Exam 7
// Insert code here.
}
}
Which of the following lines can be inserted at the specified location without generating a compile-time error?
a. char a = 0x0041;
b. char b = '\u0041';
c. char c = 0101;
d. char d = -1;
e. char e = (char)-1;
f. None of the Above

Question 15
class B {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0000,ffff
b. Prints: 00000000,ffffffff
c. Prints: 7fff,8000
d. Prints: 8000,7fff
e. Prints: 7fffffff,80000000
f. Prints: 80000000,7fffffff
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 16
class X {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}
}
}
Assume the above main method is invoked using the following
command line.
java X 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: ABCDE
b. Prints: BCDEF
c. Prints: ABCDEF
d. Compile-time error
e. Run-time exception
f. None of the Above

Question 17
1. class Violet {
2. int x;
3. public static void main(String[] args) {
4. int y;
5. System.out.print("x="+x);
6. System.out.print(", y="+y);
7. }
8. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. Compiler error at line 6.
g. Runtime Exception
h. None of the Above

Question 18
class Black {
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+c+d+e+s);
}
Certified Java Programmer Mock Exam 8
}
What is the result of attempting to compile and run the above program?
a. Prints: 00000null
b. Prints: 00000
c. Prints: 0null
d. Prints: 0
e. Prints: null
f. Compiler Error.
g. Runtime Exception.
h. None of the Above.

Question 19
class E {
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 above 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. Runtime Exception
f. Compiler Error
g. None of the Above

Capítulo 3 – Operator and Assignments (A)


Question 1
class Basics {
static int x=1;
void m(int i) {x++;i++;}
public static void main (String[] args) {
int y=3;
m(y);
System.out.println(x + "," + y);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 2
class A {
static boolean a;
static boolean b;
static boolean 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 above 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. Runtime error
j. Compiler error
k. None of the above

Question 3
class X {
public static void main(String args[]) {

Certified Java Programmer Mock Exam 9


double a = 1.0f / 3.0f;
double b = 1.0d / 3.0f;
double c = 1.0d / 3.0d;
System.out.print((a==b) + ",");
System.out.print((a==c) + ",");
System.out.print(b==c);
}
}
What is the result of attempting to compile and run the above 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. Runtime error
j. Compiler error
k. None of the above

Question 4
class A {
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args) {
boolean x = a || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}
What is the result of attempting to compile and run the above 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. Runtime error
j. Compiler error
k. None of the above

Question 5
class A {
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 above 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. Runtime error
j. Compiler error
k. None of the above

Question 6
class L {
public static void main (String s[]) {
int i = 1 | 2 ^ 3 * 2 & 13 | 2;
System.out.println(i%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
Certified Java Programmer Mock Exam 10
f. Runtime error
g. Compiler error
h. None of the above

Question 7
class Basics {
private static int x=1;
static void m(int i) {x++;i++;}
public static void main (String[] args) {
int y=3;
m(y);
System.out.println(x + "," + y);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 8
class UltraViolet {
public static void main (String[] args) {
char a = '\u002a'; // Asterisk
char b = '\u0024'; // Dollar Sign
System.out.print(a + b);
System.out.print(" ABC" + a + b);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compiler error.
e. Runtime error.
f. None of the Above

Question 9
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Gray {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = {new Sub()};
9. Object obj = sub;
10. base = obj;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 10

class Red {
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
Certified Java Programmer Mock Exam 11
d. Prints: 10
e. Prints: 1 3 4
f. Prints: 8
g. Prints: 1 3 5
h. Prints: 9
i. Runtime error.
j. Compiler error.
k. None of the Above

Question 11
class M {
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,1
b. Prints: 1,2,3,4,5,2
c. Prints: 1,2,3,4,5,3
d. Prints: 1,2,3,4,5,4
e. Prints: 1,2,3,4,5,5
f. Runtime error
g. Compiler error
h. None of the above

Question 12
1. class Purple {
2. public static void main (String []args) {
3. int[] i = {1,2,3};
4. Object obj = i;
5. i = obj;
6. }
7. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 2.
b. Runtime error at line 2.
c. Compiler error at line 3.
d. Runtime error at line 3.
e. Compiler error at line 4.
f. Runtime error at line 4.
g. Compiler error at line 5.
h. Runtime error at line 5.
i. Compiles and runs without error.

Question 13
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a;
B b;
m(null,null);
m(a=null,b=null);
m(b, a);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: BBABAB
b. Prints: BBABBA
c. Prints: BBBBAB
d. Prints: BBBBBA
e. Prints: BBBBBB
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 14
class S {
Certified Java Programmer Mock Exam 12
public static void main(String args[]) {
byte b = -1,c,d; // 1
c = (byte)(b >>> 1); // 2
d = (byte)(b >>> 25); // 3
System.out.print(c+","+d);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 127,0
b. Prints: 0,127
c. Prints: -1,0
d. Prints: -1,127
e. Prints: -1,-1
f. Prints: 127,127
g. Runtime error
h. Compiler error
i. None of the above

Question 15
import java.io.Serializable;
1. class Blue {
2. public static void main (String args[]) {
3. int[] i = {1,2,3};
4. Serializable s = i;
5. i = (int [])s;
6. }
7. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiles and runs without error.

Question 16
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5.
6. class Red {
7. public static void main(String args[]) {
8. Sub s1 = new Sub();
9. I2 i2 = s1;
10. I1 i1 = s1;
11. Base base = s1;
12. Sub s2 = (Sub)base;
13. }
14. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 9.
b. Compiler error at line 10.
c. Compiler error at line 11.
d. Compiler error at line 12.
e. Runtime error.
f. Compiles and runs without error.

Question 17
class L {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a = 1;
long b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 18
Certified Java Programmer Mock Exam 13
class Blue {
public static void main (String[] args) {
byte b1 = (byte)(2 * Byte.MAX_VALUE);
byte b2 = -1;
byte b3 = -2;
byte b4 = -3;
System.out.println((b1==b2)+","+(b1==b3)+","+(b1==b4));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: true,false,false
e. Compiler error.
f. Runtime error.
g. None of the Above

Capítulo 3 – Operators and Assignments (B)


Question 1

class A {
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 above program?
a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Runtime error
f. Compiler error
g. None of the above

Question 2
class A {
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
}
}
What is the result of attempting to compile the program?
a. Compiler error at 1
b. Compiler error at 2
c. Compiler error at 3
d. Compiler error at 4
e. Compiler error at 5
f. Compiler error at 6
g. Compiler error at 7
h. Compiler error at 8
i. Runtime error.
j. None of the Above

Question 3
class V {
public static void main (String[] args) {
float a = Float.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
double c = Double.NaN;
System.out.print((a == b)+","+(c == c)+","+(c != c));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false

Certified Java Programmer Mock Exam 14


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. Runtime error
j. Compiler error
k. None of the above

Question 4
class V {
public static void main (String[] args) {
System.out.print(Float.POSITIVE_INFINITY % 2 + ",");
System.out.print(Float.NEGATIVE_INFINITY % 2 + ",");
System.out.print(2 % Float.NEGATIVE_INFINITY);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: NaN,NaN,NaN
b. Prints: NaN,NaN,2.0
c. Prints: NaN,NaN,2
d. Prints: 2.0,2.0,2.0
e. Prints: 2,2,2
f. Runtime error
g. Compiler error
h. None of the above

Question 5
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = new Sub[1];
9. Object obj = base;
10. sub = (Sub[])obj;
11. I1 []i1 = (I1[])obj;
12. }
13. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 8.
b. Runtime error at line 8.
c. Compiler error at line 9.
d. Runtime error at line 9.
e. Compiler error at line 10.
f. Runtime error at line 10.
g. Compiler error at line 11.
h. Runtime error at line 11.
i. Compiles and runs without error.

Question 6
class Q {
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. Runtime error
i. Compiler error
j. None of the above

Question 7
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
Certified Java Programmer Mock Exam 15
4. class Sub extends Base implements I2 {}
5.
6. class Orange {
7. public static void main(String args[]) {
8. Base base = new Base();
9. I1 i1 = base;
10. Sub sub = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 9.
b. Runtime error at line 9.
c. Compiler error at line 10.
d. Runtime error at line 10.
e. Compiles and runs without error.

Question 8

1. class Maroon {
2. public static void main (String[] args) {
3. int i=1;
4. short s = 1;
5. long l=1,m=2;
6. i = l + i;
7. l = s + i;
8. }
9. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 4.
b. Runtime error at line 4.
c. Compiler error at line 5.
d. Runtime error at line 5.
e. Compiler error at line 6.
f. Runtime error at line 6.
g. Compiler error at line 7.
h. Runtime error at line 7.
i. Compiles and runs without error.

Question 9
class Sienna {
static double a;
static float b;
static int c;
static char d;
public static void main(String[] args) {

a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}
}
What is the result of attempting to compile and run the above program?
a. Prints: true
b. Prints: false
c. Compiler error.
d. Run time error.
e. None of the above.

Question 10
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above

Certified Java Programmer Mock Exam 16


Question 11

class U {
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. Runtime error
g. Compiler error
h. None of the above

Question 12
class C {
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. Runtime error
i. Compiler error
j. None of the above

Question 13

1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Yellow {
6. public static void main(String args[]) {
7. Base base = new Sub();
8. I1 i1 = base;
9. Sub sub = (Sub)base;
10. I2 i2 = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 14
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
Certified Java Programmer Mock Exam 17
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 15
1. class White {
2. public static void main(String args[]) {
3. int[] i = {1,2,3,4,5};
4. long[] l1 = new long[5];
5. long []l2 = l1;
6. long l3[] = (long[])i;
7. long l4[] = new long[5];
8. l4[1] = i[1];
9. }
10. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiler error at line 7.
j. Runtime error at line 7.
k. Compiler error at line 8.
l. Runtime error at line 8.
m. Compiles and runs without error.

Question 16
1. class Black {
2. public static void main(String args[]) {
3. int[] i = {1,2,3,4,5};
4. long[] l = new long[5];
5. for (int j=0;j < l.length(); j++) {
6. l[j] = i[j];
7. }
8. }
9. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiles and runs without error.

Question 17
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(A x) {System.out.print("A");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 18
1. class Amber {
2. public static void main(String[] args) {
3. int[][] a = {{1,2},{0,1,2},{-1,0,2}};
4. Object[] obj = (Object[])a.clone();
5. for(int i = 0;i<obj.length; i++) {
6. int[] ia = (int[])obj[i];
7. System.out.print(ia[i]);
8. }
9. }
10. }
Certified Java Programmer Mock Exam 18
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Run time error.
g. None of the above.

Capítulo 3 – Operators and Assignments (C)


Question 1
class A {
public static void main(String[] args) {
char a = 'a'; // 'a' = 97
char b = 'b'; // 'b' = 98
System.out.print(a + b + "" + a + b);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 290
b. Prints: 195195
c. Prints: 195ab
d. Prints: ab195
e. Prints: abab
f. Runtime error
g. Compiler error
h. None of the above

Question 2
class V {
public static void main (String[] args) {
System.out.print((0.0 * -0.0)+","+(-0.0 * -0.0)+",");
System.out.print((0.0 + -0.0));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0.0,0.0,0.0
b. Prints: 0.0,0.0,-0.0
c. Prints: 0.0,-0.0,0.0
d. Prints: 0.0,-0.0,-0.0
e. Prints: -0.0,0.0,0.0
f. Prints: -0.0,0.0,-0.0
g. Prints: -0.0,-0.0,0.0
h. Prints: -0.0,-0.0,-0.0
i. Runtime error
j. Compiler error
k. None of the above

Question 3
class A {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}
}
What is the result of attempting to compile the program?
a. Compiler error at 1
b. Compiler error at 2
c. Compiler error at 3
d. Compiler error at 4
e. Compiler error at 5
f. Compiler error at 6
g. Compiler error at 7
h. Compiler error at 8
i. Runtime error.
j. None of the Above

Question 4
class N {
public static void main (String[] s) {

Certified Java Programmer Mock Exam 19


byte b = 5;
System.out.println(b<<32);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Runtime error
f. Compiler error
g. None of the above

Question 5
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
System.out.println(b);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: -128
b. Prints: -127
c. Prints: -1
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 128
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 6
class L {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
char a = 1;
long b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 7
class Blue {
public static void main (String[] args) {
int i1 = (Integer.MAX_VALUE + 2);
int i2 = (Integer.MIN_VALUE + 1);
int i3 = (Integer.MIN_VALUE + 2);
int i4 = (Integer.MIN_VALUE + 3);
System.out.println((i1==i2)+","+(i1 ==i3)+","+(i1 ==i4));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: true,false,false
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 8
class Beige {
public static void main (String[] args) {
int i = (int)Float.NEGATIVE_INFINITY;
int j = (int)Float.POSITIVE_INFINITY;
int k = (int)Float.NaN;
System.out.println((i == Integer.MIN_VALUE) +
", " + (j == Integer.MAX_VALUE) +
Certified Java Programmer Mock Exam 20
", " + (k == 0));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false, false, false
b. Prints: false, false, true
c. Prints: true, true, false
d. Prints: true, true, true
e. Compiler error.
f. Run time error.

Question 9
1. class Green {
2. public static void main (String args[]) {
3. int[] i = null;
4. Cloneable c = i;
5. i = (int [])c;
6. }
7. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiles and runs without error.

Question 10
class Fuchsia {
public static void main (String[] args) {
System.out.println((short)Integer.MIN_VALUE +
", " + (short)Integer.MAX_VALUE +
", " + (int)(char)Integer.MIN_VALUE +
", " + (int)(char)Integer.MAX_VALUE);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: -32768, 32767, -32768, 32767
b. Prints: -32768, 32767, 0, 65535
c. Prints: 0, 0, 0, 0
d. Prints: 0, -1, 0, 65535
e. Compiler error.
f. Run time error.

Question 11
class A {
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. Runtime error
f. Compiler error
g. None of the above

Question 12
class M {
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?
Certified Java Programmer Mock Exam 21
a. Prints: 1,2,3,4,1
b. Prints: 1,2,3,4,2
c. Prints: 1,2,3,4,3
d. Prints: 1,2,3,4,4
e. Prints: 1,2,3,4,5
f. Runtime error
g. Compiler error
h. None of the above

Question 13
class A {}
class B extends A {}
class C extends B {
static void m1(A x) {System.out.print("m1A");}
static void m2(B x) {System.out.print("m2B"); m1(x);}
static void m2(A x) {System.out.print("m2A"); m1(x);}
static void m3(C x) {System.out.print("m3C"); m2(x);}
static void m3(B x) {System.out.print("m3B"); m2(x);}
static void m3(A x) {System.out.print("m3A"); m2(x);}
public static void main(String[] args) {
m3(new C());
}
}
What is the result of attempting to compile the program?
a. Prints: m3Am2Am1A
b. Prints: m3Bm2Bm1A
c. Prints: m3Cm2Bm1A
d. Prints: m3Cm2Am1A
e. Compiler error
f. Run time error
g. None of the Above

Question 14
class A {}
class B extends A {}
class C {
void m(A x) {System.out.print("A");}
void m(B x) {System.out.print("B");}
public static void main(String[] args) {
C c = new C();
c.m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A
b. Prints: B
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 15
class L {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a = 1;
double b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above 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 16
1. class Teal {
2. public static void main (String[] args) {
3. byte b=1;
4. long l=1000;
5. b += l;
6. }
7. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
Certified Java Programmer Mock Exam 22
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiles and runs without error.

Question 17
class R {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: -2, 3, 0, 3, 0, 6
b. Prints: -2, 3, 0, 2, 1, 4
c. Prints: -2, 3, 0, 2, 2, 5
d. Prints: -2, 3, 0, 3, 2, 6
e. Prints: -1, 3, 0, 3, 2, 7
f. Prints: -2, 0, 3, 3, 0, 6
g. Prints: -1, 0, 3, 2, 1, 4
h. Prints: -2, 0, 3, 2, 2, 5
i. Prints: -2, 0, 3, 3, 2, 6
j. Prints: -1, 0, 3, 3, 2, 7
k. Runtime error
l. Compiler error
m. None of the above

Capítulo 3 – Operators and Assignments (D)


Question 1
class E {
public static void main (String args[]) {
int i1 = 0xffffffff;
int i2 = i1 << 1;
int i3 = i1 >> 1;
int i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,7fffffff
c. Prints: ffffffff,7fffffff,ffffffff
d. Prints: ffffffff,7ffffffe,7ffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,7fffffff
g. Prints: fffffffe,7fffffff,ffffffff
h. Prints: fffffffe,7fffffff,7fffffff
i. Runtime error
j. Compiler error
k. None of the above

Question 2
class D {
public static void main (String args[]) {
int i1 = ~1;
int i2 = -1;
int i3 = -2;
System.out.print(Integer.toHexString(i1) + ",");
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,fffffffe
c. Prints: ffffffff,fffffffe,ffffffff
d. Prints: ffffffff,fffffffe,fffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,fffffffe

Certified Java Programmer Mock Exam 23


g. Prints: fffffffe,fffffffe,ffffffff
h. Prints: fffffffe,fffffffe,fffffffe
i. Runtime error
j. Compiler error
k. None of the above

Question 3
class V {
public static void main (String[] args) {
System.out.print((0.0 % -1)+","+(-0.0 % 1)+","+(-0.0 % -1));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0.0,0.0,0.0
b. Prints: 0.0,0.0,-0.0
c. Prints: 0.0,-0.0,0.0
d. Prints: 0.0,-0.0,-0.0
e. Prints: -0.0,0.0,0.0
f. Prints: -0.0,0.0,-0.0
g. Prints: -0.0,-0.0,0.0
h. Prints: -0.0,-0.0,-0.0
i. Runtime error
j. Compiler error
k. None of the above

Question 4
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color c1 = new Red();
Color c2 = new Blue();
Red r1 = new Red();
boolean b1 = c1 instanceof Color;
boolean b2 = c1 instanceof Blue;
boolean b3 = r1 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}
}
What is the result of attempting to compile and run the above program?
a. false,false,false
b. false,false,true
c. false,true,false
d. false,true,true
e. true,false,false
f. true,false,true
g. true,true,false
h. true,true,true
i. Runtime error
j. Compiler error
k. None of the above

Question 5
class W {
public static void main (String[] args) {
byte x = 3;
byte y = 5;
System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Runtime error
f. Compiler error
g. None of the above

Question 6
class A {
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);
}
}
Certified Java Programmer Mock Exam 24
What is the result of attempting to compile and run the above 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. Runtime error
j. Compiler error
k. None of the above

Question 7
class V {
public static void main (String[] args) {
System.out.print(Float.NaN % 2 + ",");
System.out.print(2 % Float.NaN);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: NaN,NaN
b. Prints: NaN,2.0
c. Prints: 2.0,NaN
d. Prints: 2.0,2.0
e. Runtime error
f. Compiler error
g. None of the above

Question 8
class L {
public static void main (String s[]) {
int i = 1 | 2 ^ 15 & 7 ^ 13 | 2;
System.out.println(i % 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. Runtime error
g. Compiler error
h. None of the above

Question 9
class Chartreuse {
public static void main (String[] args) {
short i = (short)Float.NEGATIVE_INFINITY;
short j = (short)Float.POSITIVE_INFINITY;
short k = (short)Float.NaN;
System.out.println((i == Short.MIN_VALUE) +
", " + (j == Short.MAX_VALUE) +
", " + (k == 0));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: false, false, false
b. Prints: false, false, true
c. Prints: true, true, false
d. Prints: true, true, true
e. Compiler error.
f. Run time error.

Question 10
class B {
static boolean m(boolean b) {
System.out.print(b + " ");
return b;
}
public static void main(String[] args) {
boolean a = false;
boolean b = false;
boolean c = true;
boolean d = false;
m(m(a | b == c & d) == m((a | b) == (c & d)));
}
}
Certified Java Programmer Mock Exam 25
What is the result of attempting to compile and run the above program?
a. Prints: true false false
b. Prints: false true false
c. Prints: true true true
d. Prints: false false true
e. Runtime error.
f. Compiler error.
g. None of the Above

Question 11
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
static void m(A x, C y) {System.out.print("AC");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Prints: AC
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 12
class A {
public static void main (String[] args) {
System.out.print((new Object() instanceof Object)+",");
System.out.print((new Object() instanceof String)+",");
System.out.print((new String() instanceof Object));
}
}
What is the result of attempting to compile and run the above 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. Runtime error
j. Compiler error
k. None of the above

Question 13
1. class Primitives {
2. static void printFloat(float f) {System.out.println(f);}
3. static void printDouble(double d) {System.out.println(d);}
4. public static void main(String[] args) {
5. byte b = 1;
6. short s = b;
7. char c = s;
8. int i = c;
9. long l = i;
10. float f = l;
11. printFloat(i);
12. printFloat(l);
13. printDouble(l);
14. }
15. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Compiler error at line 8.
g. Compiler error at line 9.
h. Compiler error at line 10.
Certified Java Programmer Mock Exam 26
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. Compiles and runs without error.

Question 14
class P{
public static void main (String[] s) {
byte b = 5;
System.out.println(b<<33);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Runtime error
g. Compiler error
h. None of the above

Question 15
class L {
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 above program?
a. Prints: 1, 2, 3, 4, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Runtime error
e. Compiler error
f. None of the above

Question 16
class K {
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 above program?
a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 17
class M {
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 above 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. Runtime error
Certified Java Programmer Mock Exam 27
h. Compiler error
i. None of the above

Capítulo 3 – Operators and Assignments (Opcionais)


Question 1
class A {
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 above program?
a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Runtime error
f. Compiler error
g. None of the above

Question 2
class A {
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
}
}
What is the result of attempting to compile the program?
a. Compiler error at 1
b. Compiler error at 2
c. Compiler error at 3
d. Compiler error at 4
e. Compiler error at 5
f. Compiler error at 6
g. Compiler error at 7
h. Compiler error at 8
i. Runtime error.
j. None of the Above

Question 3
class V {
public static void main (String[] args) {
float a = Float.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
double c = Double.NaN;
System.out.print((a == b)+","+(c == c)+","+(c != c));
}
}
What is the result of attempting to compile and run the above 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. Runtime error
j. Compiler error
k. None of the above

Question 4
class V {
public static void main (String[] args) {
System.out.print(Float.POSITIVE_INFINITY % 2 + ",");
System.out.print(Float.NEGATIVE_INFINITY % 2 + ",");
System.out.print(2 % Float.NEGATIVE_INFINITY);

Certified Java Programmer Mock Exam 28


}
}
What is the result of attempting to compile and run the above program?
a. Prints: NaN,NaN,NaN
b. Prints: NaN,NaN,2.0
c. Prints: NaN,NaN,2
d. Prints: 2.0,2.0,2.0
e. Prints: 2,2,2
f. Runtime error
g. Compiler error
h. None of the above

Question 5
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = new Sub[1];
9. Object obj = base;
10. sub = (Sub[])obj;
11. I1 []i1 = (I1[])obj;
12. }
13. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 8.
b. Runtime error at line 8.
c. Compiler error at line 9.
d. Runtime error at line 9.
e. Compiler error at line 10.
f. Runtime error at line 10.
g. Compiler error at line 11.
h. Runtime error at line 11.
i. Compiles and runs without error.

Question 6
class Q {
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. Runtime error
i. Compiler error
j. None of the above

Question 7
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5.
6. class Orange {
7. public static void main(String args[]) {
8. Base base = new Base();
9. I1 i1 = base;
10. Sub sub = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 9.
b. Runtime error at line 9.
c. Compiler error at line 10.
d. Runtime error at line 10.
Certified Java Programmer Mock Exam 29
e. Compiles and runs without error.

Question 8
1. class Maroon {
2. public static void main (String[] args) {
3. int i=1;
4. short s = 1;
5. long l=1,m=2;
6. i = l + i;
7. l = s + i;
8. }
9. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 4.
b. Runtime error at line 4.
c. Compiler error at line 5.
d. Runtime error at line 5.
e. Compiler error at line 6.
f. Runtime error at line 6.
g. Compiler error at line 7.
h. Runtime error at line 7.
i. Compiles and runs without error.

Question 9
class Sienna {
static double a;
static float b;
static int c;
static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}
}
What is the result of attempting to compile and run the above program?
a. Prints: true
b. Prints: false
c. Compiler error.
d. Run time error.
e. None of the above.

Question 10
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 11
class U {
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. Runtime error
g. Compiler error
Certified Java Programmer Mock Exam 30
h. None of the above

Question 12
class C {
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. Runtime error
i. Compiler error
j. None of the above

Question 13
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Yellow {
6. public static void main(String args[]) {
7. Base base = new Sub();
8. I1 i1 = base;
9. Sub sub = (Sub)base;
10. I2 i2 = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 14
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 15
1. class White {
2. public static void main(String args[]) {
3. int[] i = {1,2,3,4,5};
4. long[] l1 = new long[5];
5. long []l2 = l1;
6. long l3[] = (long[])i;
7. long l4[] = new long[5];
8. l4[1] = i[1];
9. }
10. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
Certified Java Programmer Mock Exam 31
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiler error at line 7.
j. Runtime error at line 7.
k. Compiler error at line 8.
l. Runtime error at line 8.
m. Compiles and runs without error.

Question 16
1. class Black {
2. public static void main(String args[]) {
3. int[] i = {1,2,3,4,5};
4. long[] l = new long[5];
5. for (int j=0;j < l.length(); j++) {
6. l[j] = i[j];
7. }
8. }
9. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiles and runs without error.

Question 17
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(A x) {System.out.print("A");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 18
1. class Amber {
2. public static void main(String[] args) {
3. int[][] a = {{1,2},{0,1,2},{-1,0,2}};
4. Object[] obj = (Object[])a.clone();
5. for(int i = 0;i<obj.length; i++) {
6. int[] ia = (int[])obj[i];
7. System.out.print(ia[i]);
8. }
9. }
10. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Run time error.
g. None of the above.

Capítulo 4 – Desclarations and Access Control (A)


Question 1
1. package com.dan.chisholm;
2.
3. public class A {
4. public void m1() {System.out.print("A.m1, ");}

Certified Java Programmer Mock Exam 32


5. protected void m2() {System.out.print("A.m2, ");}
6. private void m3() {System.out.print("A.m3, ");}
7. void m4() {System.out.print("A.m4, ");}
8. }
9.
10. class B {
11. public static void main(String[] args) {
12. A a = new A();
13. a.m1();
14. a.m2();
15. a.m3();
16. a.m4();
17. }
18. }
Assume that the above code appears in a single file named A.java. What is the result of attempting to compile and run the above program?
a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 13.
c. Compiler error at line 14.
d. Compiler error at line 15.
e. Compiler error at line 16.
f. None of the Above

Question 2
class Basics {
int x = 1;
int y = 2;
public static void main (String[] args) {
System.out.println(x+y);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1 2
b. Prints: 12
c. Prints: 3
d. Run time Exception
e. Compiler Error
f. None of the Above

Question 3
1. public class Basics {}
2. public class Basics2 {}
3. public class Basics3 {}
4. public class Basics4 {}
Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above

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
h. strictfp
i. None of the above.

Question 5
class A {
public static void main(String[] args) {
int[] a1 = {1,2,3};
int []a2 = {4,5,6};
int a3[] = {7,8,9};
System.out.print(a1[1]+","+a2[1]+","+a3[1]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 12
b. Prints: 15
c. Prints: 18
d. Prints: 1,4,7
e. Prints: 2,5,8
f. Prints: 3,6,9
g. Compiler Error.
Certified Java Programmer Mock Exam 33
h. Runtime Error.
i. None of the Above.

Question 6
Class A is declared in a file named A.java.
1. package com.dan.chisholm;
2.
3. public class A {
4. public void m1() {System.out.print("A.m1, ");}
5. protected void m2() {System.out.print("A.m2, ");}
6. private void m3() {System.out.print("A.m3, ");}
7. void m4() {System.out.print("A.m4, ");}
8. }

Class C is declared in a file named C.java.


9. package com.dan.chisholm.other;
10.
11. import com.dan.chisholm.A;
12.
13. public class C extends A {
14. public static void main(String[] args) {
15. C c = new C();
16. c.m1();
17. c.m2();
18. c.m3();
19. c.m4();
20. }
21. }
What is the result of attempting to compile and run the above program?
a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 16.
c. Compiler error at line 17.
d. Compiler error at line 18.
e. Compiler error at line 19.
f. None of the Above

Question 7
class J {
public static void main(String[] args) {
int[] a2 = {1,2}, a3 = {3,4,5}, a4 = {6,7,8,9}; // 1
int[][] a1 = {a2,a3,a4}; // 2
System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 8
Which of the following is used to force each thread to reconcile its working copy of a variable with the master copy in main memory?
a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 9
class A {
static byte m1() {
final char c = 'b'-'a';
return c; // 1
}
static byte m2() {
final short s = 2;
return s; // 2
}
Certified Java Programmer Mock Exam 34
static byte m3(final char c) {
return c; // 3
}
static byte m4(final short s) {
return s; // 4
}
public static void main(String[] args) {
char c = 'd'-'a';
short s = 4;
System.out.print(""+m1()+m2()+m3(c)+m4(s));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 1234
b. Prints: 10
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 10
class D {
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. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 11
Which of the following statements are 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 A {
A() throws Exception {} // 1
}
class B extends A {
B() throws Exception {} // 2
}
class C extends A {
C() {} // 3
}
Which of the following statements are true?
a. class A extends Object.
b. Compiler error at 1.
c. Compiler error at 2.
d. Compiler error at 3.
e. None of the Above

Question 13
Which of the following modifiers can be applied to a constructor?
a. abstract
b. final
c. private
d. protected
e. public
Certified Java Programmer Mock Exam 35
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 14
class B {
static int m1(byte b) {;
return b; // 1
}
static int m2(char c) {;
return c; // 2
}
static int m3(short s) {
return s; // 3
}
static int m4(long l) {
return l; // 4
}
static int m5(float f) {
return f; // 5
}
public static void main(String[] args) {
byte b = 1;
char c = 'c'-'a';
short s = 3;
long l = 4L;
float f = 5.0f;
System.out.print(""+m1(b)+m2(c)+m3(s)+m4(l)+m5(f));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 12345
b. Prints: 12345.0
c. Prints: 15
d. Prints: 15.0
e. Compiler Error at 1.
f. Compiler Error at 2.
g. Compiler Error at 3.
h. Compiler Error at 4.
i. Compiler Error at 5.
j. Runtime Error
k. None of the Above

Question 15
Which of the following statements are 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 A {
A(int i) {} // 1
}
class B extends A {} // 2
Which of the following statements are true?
a. The compiler attempts to create a default constructor for class A.
b. The compiler attempts to create a default constructor for class B.
c. Compiler error at 1.
d. Compiler error at 2.
e. None of the Above

Question 17
class A {
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
}
Certified Java Programmer Mock Exam 36
}
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. None of the above.

Question 18
Which of the following statements are true?
a. The value of a final field can not be assigned more than once.
b. The value of a final field can be assigned at any time or not at all.
c. A final field that is not assigned a value at compile time is called a blank final variable.
d. Only static variables can be declared final.
e. A blank final variable that is static must be definitely assigned in a static initializer.
f. At the end of the instance construction process all blank final variables must be definitely assigned.
g. A field can not be declared both final and volatile.
h. None of the above.

Question 19
class A {
public static void main(String[] args) {
int[][] a = {{1,2},{3,4,5},{6,7,8,9},{}};
for (int i = 0; i < a.length; i++) {
System.out.print(a[i].length+",");
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 2,3,4,0,
b. Prints: 1,2,5,0,
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Capítulo 4 – Desclarations and Access Control (B)


Question 1
class Red {
public int a;
public static int b;
public static void main (String[] in) {
Red r1 = new Red();
Red r2 = new Red();
r1.a++;
r1.b++;
System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0, 0, 0, 0
b. Prints: 0, 1, 1, 1
c. Prints: 1, 1, 1, 0
d. Prints: 1, 1, 0, 1
e. Prints: 1, 1, 0, 0
f. Prints: 1, 1, 1, 1
g. Compiler Error
h. Run time Exception
i. None of the Above

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

Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. None of the Above

Certified Java Programmer Mock Exam 37


Question 3
1. class Basics1 {}
2. class Basics2 {}
3. class Basics3 {}
4. class Basics4 {}
Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above

Question 4
class A {
public static void main (String[] args) {
private int x = 1;
protected int y = 2;
public int z = 3;
System.out.println(x+y+z);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 123
b. Prints: 1 2 3
c. Prints: 6
d. Run time error
e. Compiler Error
f. None of the Above

Question 5
Which of the following modifiers can be applied to a method?
a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 6
Which of the following statements are true?
a. If the superclass method is static, then any method with the same signature in a subclass must also be static.
b. If the superclass method is strictfp, then the overriding method must also be strictfp.
c. If the superclass method is synchronized, then the overriding method must also be synchronized.
d. If the superclass method is public, then the overriding method must also be public.
e. If the superclass method is native, then the overriding method must also be native.
f. If the superclass method is protected, then the overriding method must be protected or public.
g. None of the above.

Question 7
class E {
public static void main (String[] args) {
byte[] a = new byte[1];
long[] b = new long[1];
float[] c = new float[1];
Object[] d = new Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0,0,0,null
b. Prints: 0,0,0.0,null
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Question 8
abstract class A {}
final class B {}
private class C {}
protected class D {}
static class E {}
synchronized class F {}
Certified Java Programmer Mock Exam 38
transient class G {}
volatile class H {}
strictfp class I {}
Suppose these are top-level class declarations and not nested class declarations. Which of these results in a compiler error?
a. A
b. B
c. C
d. D
e. E
f. F
g. G
h. H
i. I
j. None of the Above

Question 9
Class A is declared in a file named A.java.
1. package com.dan.chisholm;
2.
3. public class A {
4. public void m1() {System.out.print("A.m1, ");}
5. protected void m2() {System.out.print("A.m2, ");}
6. private void m3() {System.out.print("A.m3, ");}
7. void m4() {System.out.print("A.m4, ");}
8. }
Class D is declared in a file named D.java.
9. package com.dan.chisholm.other;
10.
11. import com.dan.chisholm.A;
12.
13. public class D {
14. public static void main(String[] args) {
15. A a = new A();
16. a.m1();
17. a.m2();
18. a.m3();
19. a.m4();
20. }
21. }
What is the result of attempting to compile and run the above program?
a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 16.
c. Compiler error at line 17.
d. Compiler error at line 18.
e. Compiler error at line 19.
f. 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 super class must have an accessible constructor that takes no arguments.
b. The no-argument super class constructor must not have a throws clause.
c. None of the above.

Question 11
class A {
public float a; // 1
protected float b; // 2
strictfp float h; // 3
private float c; // 4
static float d; // 5
synchronized float i; // 6
}
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the above.

Question 12
Which of the following modifiers can be applied to a field?
a. abstract
b. final
c. private
d. protected
e. public
Certified Java Programmer Mock Exam 39
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 13
Suppose that the superclass constructor invocation statement, super, appears explicitly in a subclass constructor. If a compile-time error is to be
avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following?
a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword "this".
f. The keyword "super".
g. None of the above.

Question 14
Which of the following statements are true?
a. A constructor can invoke another constructor of the same class using the constructor invocation statement "this".
b. A constructor can invoke itself using the constructor invocation statement "this".
c. The constructor invocation statement, "this", can legally appear anywhere in the constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation statement "super".
e. The number of constructor invocation statements that may appear in any constructor body can equal but not exceed the number of alternate
constructors declared in the same class.
f. A constructor is not permitted to throw an exception.
g. None of the above.

Question 15
Which of the following statements are true?
a. A final method can not be overridden.
b. All methods declared in a final class are implicitly final.
c. The methods declared in a final class must be explicitly declared final or a compile-time error occurs.
d. It is a compile-time error if a private method is declared final.
e. A machine-code generator can inline the body of a final method.
f. None of the above.

Question 16
class A {
public static void main(String[] args) {
int[] a1[],a2[]; // 1
int []a3,[]a4; // 2
int []a5,a6[]; // 3
int[] a7,a8[]; // 4
}
}
Compile-time errors occur at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. None of the Above.

Question 17
class A {
public static void main(String[] args) {
int[][] a = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.print(a[0][2]+","+a[1][0]+","+a[2][1]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 3,4,8
b. Prints: 7,2,6
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Question 18
class A {public String toString() {return "A";}}
class B {
public static void main(String[] arg) {
A[] a1 = new A[1]; // 1
A[][] a2 = new A[2][]; // 2
A[][][] a3 = new A[3][][]; // 3
a1[0] = new A(); // 4
a2[0] = a2[1] = a1; // 5
a3[0] = a3[1] = a3[2] = a2; // 6
Certified Java Programmer Mock Exam 40
System.out.print(a3[2][1][0]); // 7
}
}
What is the result of attempting to compile and run the above program?
a. Prints: null
b. Prints: A
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Run-time Exception
k. None of the Above

Capítulo 4 – Desclarations and Access Control (C)


Question 1
1. class Red {
2. // Comment1
3. // // Comment2
4. /* Comment3 */
5. /* Comment4
6. */
7. /** Comment5 */
8. // /* Comment6 */
9. /* // Comment7 */
10. /* /* Comment8 */
11. /** /* // Comment9 // /* */
12. /* Comment10 */*/
13. /* Comment11 /* Comment12 */ */
14. }
What is the result of attempting to compile the program?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. Compiler error at line 6.
g. Compiler error at line 7.
h. Compiler error at line 8.
i. Compiler error at line 9.
j. Compiler error at line 10.
k. Compiler error at line 11.
l. Compiler error at line 12.
m. Compiler error at line 13.
n. None of the Above

Question 2
class A {
A() throws Exception {} // 1
}
class B extends A {
B() throws Exception {} // 2
}
class C extends A {} // 3
Which of the following statements are true?
a. Compiler error at 1.
b. Compiler error at 2.
c. Compiler error at 3.
d. None of the Above

Question 3
class G {
public static void main(String[] args) {
int[][] a1 = {{1;2};{3;4;5};{6;7;8;9}};
System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.

Certified Java Programmer Mock Exam 41


g. Runtime Error.
h. None of the Above.

Question 4
class I {
public static void main(String[] args) {
int[][] a1 = [[1,2],[3,4,5],[6,7,8,9]];
int[][] a2 = [[1,2],[3,4,5],[6,7,8,9]];
int[][] a3 = [[1,2],[3,4,5],[6,7,8,9]];
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 5
Which of the following modifiers can be applied to a class that is not a nested class?
a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 6

class B {
public static void main(String[] args) {
int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}};
int []a2[] = {{1,2},{3,4,5},{6,7,8,9}};
int a3[][] = {{1,2},{3,4,5},{6,7,8,9}};
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compiler Error.
g. Runtime Error.
h. None of the Above.

Question 7
class A {
public static void main(String[] args) {
int[] a1 = new int[]; // 1
int a2[] = new int[5]; // 2
int[] a3 = new int[]{1,2}; // 3
int []a4 = {1,2}; // 4
int[] a5 = new int[5]{1,2,3,4,5}; // 5
}
}
Compile-time errors occur at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. 5.
f. None of the Above.

Question 8
Which of the following is used to prevent the serialization of a field?
a. abstract
b. final
c. private
d. protected
e. public
f. static
Certified Java Programmer Mock Exam 42
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 9
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 implicit constructor declaration has no throws clause.
f. The implicitly declared constructor has package access.
g. The implicitly declared constructor accepts one parameter for each field in class A.
h. The implicitly declared constructor invokes the no-parameter constructor of the superclass.
i. None of the Above

Question 10
class A {
transient float e = 1; // 1
volatile float f = 1; // 2
abstract float j = 1; // 3
final float g = 1; // 4
private final float k = 1; // 5
private transient float l = 1; // 6
volatile final float m = 1; // 7
}
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. None of the above.

Question 11
class A {
public static void main(String[] args) {
int[5] a1; // 1
int []a2; // 2
int[ ]a3; // 3
int[] a4[]; // 4
}
}
Compile-time errors occur at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. None of the Above.

Question 12
Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class?
a. Do not declare any constructors.
b. Do not use a return statement in the constructor.
c. Declare all constructors with a void return type.
d. Declare all constructors using the private access modifier.
e. None of the above.

Question 13
class H {
public static void main(String[] args) {
int[][] a1 = ((1,2),(3,4,5),(6,7,8,9));
System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
Certified Java Programmer Mock Exam 43
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 14
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 parameters.
c. The default constructor invokes the superclass constructor with no arguments.
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.
h. None of the above.

Question 15
class C {
public static void main(String[] args) {
int[] a1[] = {new int[]{1,2},new int[]{3,4,5}};
int []a2[] = new int[][]{{1,2},{3,4,5}};
int a3[][] = {{1,2},new int[]{3,4,5}};
System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 14
b. Prints: 16
c. Prints: 1,2,4
d. Prints: 2,3,4
e. Prints: 2,3,5
f. Prints: 2,4,5
g. Compiler Error.
h. Runtime Error.
i. None of the Above.

Question 16
class A {
public static void main(String[] args) {
int[][] a = {{1,2},{3,4,5},{6,7,8,9},{}};
System.out.print(a.length);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0
b. Prints: 3
c. Prints: 4
d. Prints: 9
e. Prints: 10
f. Prints: 11
g. Compiler Error.
h. Runtime Error.
i. None of the Above.

Question 17
class A {}
class B {
public static void main(String[] arg) {
A[] a1 = new A[1]; // 1
A[][] a2 = new A[2][1]; // 2
A[][][] a3 = new A[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}
}
What is the result of attempting to compile and run the above program?
a. Prints: null
b. Prints: nullnull
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Compile-time error at 8.
Certified Java Programmer Mock Exam 44
k. Run-time Exception
l. None of the Above

Question 18
class A {
public static void main (String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a1[j][i]);
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 123456789
b. Prints: 147258369
c. Prints: 321654987
d. Prints: 369258147
e. Run time Exception
f. Compiler Error
g. None of the Above

Capítulo 5 – Flow Control and Exception Handling (A)


Question 1
class A {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (int h = 0; h < 6; h++) {
label2:
do {
i++;
k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label2;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j<5);
}
System.out.println(h + "," + i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0,1,0
b. Prints: 0,2,1
c. Prints: 1,3,1
d. Prints: 2,4,1
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 2
class A {
public static void main (String[] args) {
for (int i = 0; i < 4; i++) {
switch (i) {
case 0: System.out.print("A");
case 1: System.out.print("B");
case 2: System.out.print("C");
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: ABC
b. Prints: ABCC
c. Prints: CBA
d. Prints: ABCBCC
e. Runtime Exception
f. Compiler Error
g. None of the Above

Certified Java Programmer Mock Exam 45


Question 3
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 = 5;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
case 4: throw new Exception();
}
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: 1,0,0,0,0,0
b. Prints: 1,0,1,0,0,1
c. Prints: 0,0,1,0,0,1
d. Prints: 1,1,1,1,1,1
e. Compiler Error
f. Run Time Error
g. None of the Above

Question 4
class RedException extends Exception {}
class BlueException extends Exception {}
class White {
void m1() throws RedException {throw new RedException();}
public static void main (String[] args) {
White white = new White();
int a,b,c,d;
a = b = c = d = 0;
try {white.m1();a++;}
catch (RedException e) {b++;}
catch (BlueException e) {c++;}
finally {d++;}
System.out.print(a+","+b+","+c+","+d);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 5
1. class A {
2. void m1() {throw new ClassNotFoundException();}
3. void m2() {throw new ArithmeticException();}
4. void m3() {throw new ClassCastException();}
5. void m4() {throw new IllegalArgumentException();}
6. void m5() {throw new CloneNotSupportedException();}
7. void m6() {throw new NoSuchFieldException();}
8. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
Certified Java Programmer Mock Exam 46
f. Compiler error at line 7.
g. None of the Above

Question 6
1. class A {
2. void m1() {throw new IndexOutOfBoundsException();}
3. void m2() {throw new InstantiationException();}
4. void m3() {throw new IllegalAccessException();}
5. void m4() {throw new NullPointerException();}
6. void m5() {throw new NoSuchMethodException();}
7. void m6() {throw new SecurityException();}
8. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 7
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f;
a = b = c = d = f = 0;
int x = 1;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
catch (Level3Exception e) { b++;}
catch (Level2Exception e) { c++;}
catch (Level1Exception e) { d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 8
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 = 1;
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++;}
Certified Java Programmer Mock Exam 47
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,0,1,0,0
b. Prints: 0,0,1,1,0,1
c. Prints: 0,1,1,1,0,1
d. Prints: 1,0,1,1,0,1
e. Prints: 1,1,1,1,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 9
1. class A {
2. void m1() {throw new ClassNotFoundException();}
3. void m2() {throw new CloneNotSupportedException();}
4. void m3() {throw new IllegalAccessException();}
5. void m4() {throw new InstantiationException();}
6. void m5() {throw new InterruptedException();}
7. void m6() {throw new NoSuchMethodException();}
8. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 10
class A {
public static void main (String[] args) {
Object error = new Error();
Object runtimeException = new RuntimeException();
System.out.print((error instanceof Exception) + ",");
System.out.print(runtimeException instanceof Exception);
}
}
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. Compiler Error
f. Run Time Error
g. None of the Above

Question 11
class Red {
public static void main(String args[]) {
for (int i = 0; i<5 ;i++) {
switch(i) {
case 0: System.out.print("v ");break;
case 1: System.out.print("w ");
case 2: System.out.print("x ");break;
case 3: System.out.print("y ");
case 4: System.out.print("z ");break;
default: System.out.print("d ");
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: v w x y z
b. Prints: v w x y z d
c. Prints: v w x x y z z
d. Prints: v w w x y y z d
e. Prints: d d d d d d
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 12
class A {
public static void main (String[] args) {
Certified Java Programmer Mock Exam 48
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + ",");
System.out.print(error instanceof Throwable);
}
}
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. Compiler Error
f. Run Time Error
g. None of the Above

Question 13
class Orange {
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 above program?
a. Prints: zero
b. Prints: 100
c. Prints: 1000
d. Prints: Default
e. Runtime error
f. Compiler error
g. None of the above

Question 14
class Violet {
public static void main(String args[]) {
int x = -5;
int success = 0;
do {
switch(x) {
case 0: System.out.print("0");
x += 5; break;
case 1: System.out.print("1");
x += 3; break;
case 2: System.out.print("2");
x += 1; break;
case 3: System.out.print("3");
success++; break;
case 4: System.out.print("4");
x -= 1; break;
case 5: System.out.print("5");
x -= 4; break;
case 6: System.out.print("6");
x -= 5; break;
default:
x += x < 0 ? 2 : -2;
}
} while ((x != 3) || (success < 2));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0123456
b. Prints: 60514233
c. Prints: 1433
d. Prints: 61433
e. Prints: 051433
f. Prints: 33
g. Runtime Exception
h. Compiler Error
i. None of the Above
Certified Java Programmer Mock Exam 49
Question 15
class F {
static int m1(String s, int i) {
System.out.print(s + i);
return i;
}
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (m1("i", ++i) < 2)
System.out.print("k" + ++k);
while (m1("j", ++j) < 2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: i1k1i2k2j1i3j2
b. Prints: i1k1i2k2j1i1k1i2k2j2
c. Prints: i1k1i2j1i3j2
d. Runtime Exception
e. Compiler Error
f. None of the Above

Question 16
class I {
public static void main (String[] args) {
int i = 0, j = 9;
do {
if (j < 4) {
break;
} else if (j-- < 7) {
continue;
}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 17
class A {
public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2;)
System.out.print(i);
while (j++ < 2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above

Question 18
class G {
public static void main (String[] args) {
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {
break;
}
Certified Java Programmer Mock Exam 50
} while (i < 5);
System.out.print(i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 5,4
b. Prints: 6,3
c. Prints: 6,6
d. Prints: 7,2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 19
class C {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above

Capítulo 5 – Flow Control and Exception Handling (B)


Question 1
1. class A {
2. void m1() {throw new ArithmeticException();}
3. void m2() {throw new ClassCastException();}
4. void m3() {throw new IllegalArgumentException();}
5. void m4() {throw new IndexOutOfBoundsException();}
6. void m5() {throw new NullPointerException();}
7. void m6() {throw new SecurityException();}
8. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 2
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 = 3;
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++;}

Certified Java Programmer Mock Exam 51


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: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 3
class A {
static boolean b;
public static void main(String[] args) {
if (b) {
System.out.print("A");
} else if (b = false) {
System.out.print("B");
} else if (b) {
System.out.print("C");
} else if (!b) {
System.out.print("D");
} else {
System.out.print("E");
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 4
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f;
a = b = d = f = 0;
try {white.m1();a++;} catch (WhiteException e) {b++;}
try {white.m2();d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 5
class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
abstract void m1() throws ColorException;
}
class White extends Color {
void m1() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,c,d;
a = b = c = 0;
Certified Java Programmer Mock Exam 52
try {
white.m1();
a++;
} catch (WhiteException e) {b++;}
finally {c++;}
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,1
c. Prints: 0,1,0
d. Prints: 0,1,1
e. Prints: 1,0,0
f. Prints: 1,0,1
g. Prints: 1,1,0
h. Prints: 1,1,1
i. Compiler Error
j. Run Time Error
k. None of the Above

Question 6
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 = 4;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
case 4: throw new Exception();
}
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,0,0,0,1
b. Prints: 0,0,0,0,1,0
c. Prints: 0,0,1,0,0,1
d. Prints: 0,0,1,0,1,1
e. Prints: 0,1,1,1,1,1
f. Prints: 1,1,1,1,1,1
g. Compiler Error
h. Run Time Error
i. None of the Above

Question 7
class A {
public static void main(String[] args) {
boolean b = true;
if (b = false) {
System.out.print("A");
} else if (b) {
System.out.print("B");
} else {
System.out.print("C");
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A
b. Prints: B
c. Prints: C
d. Runtime Exception
e. Compiler Error
Certified Java Programmer Mock Exam 53
f. None of the Above

Question 8
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new ColorException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f;
a = b = d = f =0;
try {white.m1();a++;} catch (ColorException e) {b++;}
try {white.m2();d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 9

class Level1Exception extends Exception {}


class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f;
a = b = c = d = f = 0;
int x = 2;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
catch (Level3Exception e) { b++;}
catch (Level2Exception e) { c++;}
catch (Level1Exception e) { d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 10
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();
Certified Java Programmer Mock Exam 54
}
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. Compiler Error
g. Run Time Error
h. None of the Above

Question 11
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f;
a = b = c = d = f = 0;
int x = 4;

try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
catch (Level3Exception e) { b++;}
catch (Level2Exception e) { c++;}
catch (Level1Exception e) { d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 12
class A {
public static void main(String[] args) {
boolean b;
if (b = false) {
System.out.print("A");
} else if (b) {
System.out.print("B");
} else if (!b) {
System.out.print("C");
} else {
System.out.print("D");
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
Certified Java Programmer Mock Exam 55
e. Prints: E
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 13
class Purple {
public static void main(String args[]) {
int x = 6;
int success = 0;
do {
switch(x) {
case 0: System.out.print("0");
x += 5; break;
case 1: System.out.print("1");
x += 3; break;
case 2: System.out.print("2");
x += 1; break;
case 3: System.out.print("3");
success++; break;
case 4: System.out.print("4");
x -= 1; break;
case 5: System.out.print("5");
x -= 4; break;
case 6: System.out.print("6");
x -= 5; break;
default: System.out.print("Default ");
}
} while ((x != 3) || (success < 2));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0123456
b. Prints: 60514233
c. Prints: 6152433
d. Prints: 61433
e. Prints: 6143
f. Prints: 6
g. Prints: Default
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 14
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 = 1;
try {
throw new Level1Exception();
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: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compiler Error
g. Run Time Error
Certified Java Programmer Mock Exam 56
h. None of the Above

Question 15
class E {
public static void main (String[] args) {
int i = 0, j = 0;
while (i++ < 3) do
System.out.print(j);
while (j++ < 3);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0001
b. Prints: 001122
c. Prints: 012012
d. Prints: 012345
e. Prints: 1112
f. Prints: 111222
g. Prints: 121212
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 16
class H {
public static void main (String[] args) {
int i = 0, j = 9;
while (i++ <= j--) {
i++;
if (j < 5) break;
}
System.out.print(i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 4,7
b. Prints: 6,6
c. Prints: 7,2
d. Prints: 8,5
e. Prints: 9,4
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 17
class D {
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above

Question 18
class A {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (;;) {
i++;
label2:
do {
k = i + j;
switch (k) {
case 0: continue label2;
case 1: continue label1;
Certified Java Programmer Mock Exam 57
case 2: break;
case 3: break label2;
case 4: break label1;
default: break label1;
}
} while (++j<5);
}
System.out.println(i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 2,1
b. Prints: 2,2
c. Prints: 3,1
d. Prints: 3,2
e. Prints: 3,3
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 19
class A {
public static void main (String args[]) {
int h = 0, i = 0, j = 0, k = 0;
label1:
for (;;) {
h++;
label2:
do {
i++;
k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j<5);
}
System.out.println(h + "," + i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1,2,3
b. Prints: 1,3,2
c. Prints: 2,2,2
d. Prints: 2,4,1
e. Prints: 2,4,2
f. Runtime Exception
g. Compiler Error
h. None of the Above

Capítulo 5 – Flow Control and Exception Handling (Opcionais)


Question 1
class Green {
public static void main(String args[]) {
short[] s = {1, -1, 3, 4};
for (int i = 0; i<s.length;i++) {
switch(s[i]) {
case 2-1: System.out.print("v ");
break;
case 'd'-'a': System.out.print("w ");
break;
case 0: System.out.print("x ");
break;
case ~0: System.out.print("y ");
break;
case 4&5: System.out.print("z ");
break;
default: System.out.print("Default ");
}
}
}
}

Certified Java Programmer Mock Exam 58


What is the result of attempting to compile and run the above program?
a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 2
class Blue {
public static void main(String args[]) {
long[] l = {1, -1, 3, 4};
for (int i = 0; i<l.length;i++) {
switch(l[i]) {
case 2-1: System.out.print("v ");
break;
case 'd'-'a': System.out.print("w ");
break;
case 0: System.out.print("x ");
break;
case ~0: System.out.print("y ");
break;
case 4&5: System.out.print("z ");
break;
default: System.out.print("Default ");
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 3
class Black {
public static void main (String args[]) {
int i = 0;
int j = 0;
label1:
while (i++<5) {
label2:
for (;;) {
label3:
do {
System.out.print(i + j);
switch (i+j++) {
case 0: continue label3;
case 1: continue label2;
case 2: continue label1;
case 3: break;
case 4: break label3;
case 5: break label2;
case 6: break label1;
default: break label1;
}
} while (++j<5);
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 12457
b. Prints: 02357
c. Prints: 02356
d. Prints: 1357
e. Prints: 1356
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 4
class Brown {
public static void main (String args[]) {
Certified Java Programmer Mock Exam 59
int i = 0;
int j = 0;
label1:
while (1) {
label2:
for (;;i++) {
label3:
do {
System.out.print(i + j);
switch (i+j++) {
case 0: continue label3;
case 1: continue label2;
case 2: continue label1;
case 3: break;
case 4: break label3;
case 5: break label2;
case 6: break label1;
default: break label1;
}
} while (++j<5);
}
}
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1357
b. Prints: 02
c. Prints: 02356
d. Prints: 01346
e. Prints: 0236
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 5
class A {
public static void main (String[] args) {
int i = 0,j = 0,m = 0,n = 0,p;
label1:
try {
do {
m++;
try {
p = i + j + m + n;
if (p>=5) break label1;
n++;
} finally {i++;}
} while (m++ < 2);
} finally {j++;}
System.out.print(i + "," + j + "," + m + "," + n);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1, 1, 2, 3
b. Prints: 1, 1, 2, 2
c. Prints: 1, 1, 3, 1
d. Prints: 1, 0, 3, 1
e. Prints: 2, 1, 3, 1
f. Prints: 2, 1, 4, 1
g. Prints: 0, 0, 3, 1
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 6
class Gray {
public static void main (String args[]) {
int i = 0;
int j = 0;
int k = 0;
label1:
do {
k++;
label2:
do {
j++;
label3:
do {
i++;
Certified Java Programmer Mock Exam 60
System.out.print((i + j + k + ", "));
switch ((i+j+k)) {
case 3: continue label3;
case 4: continue label2;
case 5: continue label1;
case 6: break;
case 7: break label3;
case 8: break label2;
default: break label1;
}
} while (i++ < 2);
} while (j++ < 2);
} while (k++ < 2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 3, 4, 6, 8, 12
b. Prints: 3, 4, 7, 10
c. Prints: 3, 5,
d. Prints: 3, 5, 9,
e. Prints: 3, 6, 8, 12
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 7
class Gray {
public static void main (String args[]) {
int i = 0;
int j = 0;
int k = 0;
label1:
do {
k++;
label2:
do {
j++;
label3:
do {
i++;
System.out.print((i + j + k + ", "));
switch ((i+j+k)) {
default: break label1;
case 3: break label3;
case 4: break label3;
case 5: break label2;
case 6: continue label3;
case 7: continue label2;
case 8: continue label2;
case 9: continue label1;
}
} while (i++ < 2);
} while (j++ < 2);
} while (k++ < 2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 3, 4, 6, 12
b. Prints: 3, 4, 7, 10
c. Prints: 3, 5, 8, 12,
d. Prints: 3, 6, 12,
e. Prints: 3, 6, 8, 12,
f. Runtime Exception
g. Compiler Error
h. None of the Above

Capítulo 6 – Object Oriented Programming (A)


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

Certified Java Programmer Mock Exam 61


}
What is the result of attempting to compile and run the above program?
a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 2
class Basics {
private static int x=1;
static void m(int i) {x++;i++;}
public static void main (String[] args) {
int y=3;
m(y);
System.out.println(x + "," + y);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,3
d. Prints: 2,4
e. Run time Exception
f. Compiler Error
g. None of the Above

Question 3
interface A {
final void m6(); // 1
synchronized void m7(); // 2
strictfp void m8(); // 3
native void m9(); // 4
}
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 4

Which of the following are modifiers that can be applied to a method declaration within an interface?
a. abstract
b. final
c. private
d. protected
e. public
f. None of the above.

Question 5
interface A {
int a = 1; // 1
public int b = 2; // 2
public static int c = 3; // 3
public static final int d = 4; // 4
}

Which of the field declarations result in a compile-time error?


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

Question 6
class L {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a = 1;
long b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above program?
Certified Java Programmer Mock Exam 62
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 7
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5.
6. class Orange {
7. public static void main(String args[]) {
8. Base base = new Base();
9. I1 i1 = base;
10. Sub sub = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 9.
b. Runtime error at line 9.
c. Compiler error at line 10.
d. Runtime error at line 10.
e. Compiles and runs without error.

Question 8
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
A c3 = new C();
C c4 = new C();
c1.m1(c4);
c2.m1(c4);
c3.m1(c4);
}
}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 9
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = new Sub[1];
9. Object obj = base;
10. sub = (Sub[])obj;
11. I1 []i1 = (I1[])obj;
12. }
13. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 8.
b. Runtime error at line 8.
c. Compiler error at line 9.
d. Runtime error at line 9.
e. Compiler error at line 10.
f. Runtime error at line 10.
g. Compiler error at line 11.
Certified Java Programmer Mock Exam 63
h. Runtime error at line 11.
i. Compiles and runs without error.

Question 10
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 11
class A {}
class B extends A {}
class C extends B {}
class D {
void m1(A a) {System.out.print("A");}
void m1(B b) {System.out.print("B");}
void m1(C c) {System.out.print("C");}
public static void main(String[] args) {
A c1 = new C();
B c2 = new C();
C c3 = new C();
D d = new D();
d.m1(c1);
d.m1(c2);
d.m1(c3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 12
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a;
B b;
m(null,null);
m(a=null,b=null);
m(b, a);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: BBABAB
b. Prints: BBABBA
c. Prints: BBBBAB
d. Prints: BBBBBA
e. Prints: BBBBBB
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 13
1. interface I1 {}
Certified Java Programmer Mock Exam 64
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5.
6. class Red {
7. public static void main(String args[]) {
8. Sub s1 = new Sub();
9. I2 i2 = s1;
10. I1 i1 = s1;
11. Base base = s1;
12. Sub s2 = (Sub)base;
13. }
14. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 9.
b. Compiler error at line 10.
c. Compiler error at line 11.
d. Compiler error at line 12.
e. Runtime error.
f. Compiles and runs without error.

Question 14
1. interface I10 {
2. String name = "I10";
3. String s10 = "I10.s10";
4. }
5. interface I20 {
6. String name = "I20";
7. String s20 = "I20.s20";
8. }
9. class C20 implements I10, I20 {
10. public static void main(String[] args) {
11. System.out.print(I10.s10+",");
12. System.out.print(I20.s20+",");
13. System.out.print(I20.name);
14. }
15. }
What is the result of attempting to compile and run the above program?
a. Prints: I10.s10,I20.s20,I10
b. Prints: I10.s10,I20.s20,I20
c. Prints: I10.s10,I20.s20,
d. Prints: I10.s10,I20.s20,null
e. Compiler error at line 9.
f. Compiler error at line 11.
g. Compiler error at line 12.
h. Compiler error at line 13.
i. Runtime Error
j. None of the Above

Question 15
class L {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a = 1;
double b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above 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 16
class T {
private int i1, i2;
void printI1I2() {System.out.print("T: i1=" + i1 + ", i2=" + i2);}
T(int i1, int i2) {
this.i1=i1;this.i2=i2;
}
}
class U extends T {
private int i1, i2;
void printI1I2() {System.out.print("U: i1=" + i1 + ", i2=" + i2);}
Certified Java Programmer Mock Exam 65
U(int i1, int i2) {
this.i1=i1;this.i2=i2;
}
public static void main(String[] args) {
T t = new U(1,2);
t.printI1I2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: U: i1=1, i2=2
b. Prints: T: i1=1, i2=2
c. Prints: U: i1=null, i2=null
d. Prints: T: i1=null, i2=null
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 17
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A a1 = new A();
A b1 = new B();
A c1 = new C();
C c4 = new C();
a1.m1(c4);
b1.m1(c4);
c1.m1(c4);
}
}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 18
class A {public void m1() {System.out.print("A1");}}
class B extends A {
public void m1() {System.out.print("B1");}
public void m2() {System.out.print("B2");}
}
class C {
public static void main(String[] args) {
A a1 = new B();
a1.m1();
a1.m2();
((B)a1).m1();
((B)a1).m2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A1B2B1B2
b. Prints: B1B2B1B2
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Capítulo 6 – Object Oriented Programming (B)


Question 1
class A {
void m1() {System.out.print("A.m1");}
}
class B extends A {
void m1() {System.out.print("B.m1");}
static void m1(String s) {System.out.print(s+",");}

Certified Java Programmer Mock Exam 66


}
class C {
public static void main (String[] args) {
B.m1("main");
new B().m1();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: main,B.m1
b. Compiler Error
c. Runtime Error
d. None of the Above

Question 2
interface A {
int i = 1;
int m1();
}
interface B extends A {
int i = 10;
int m1();
}
class C implements B {
public int m1() {return ++i;}
public static void main(String[] args) {
System.out.print(new C().m1());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2
b. Prints: 11
c. Compile-time error.
d. Run-time error.
e. None of the above.

Question 3
Which of the following are modifiers that can be applied to a field declaration within an interface?
a. abstract
b. const
c. final
d. private
e. protected
f. public
g. None of the above.

Question 4
interface A {
String s1 = "A";
String m1();
}
interface B implements A {
String s1 = "B";
String m1();
}
class C implements B {
public String m1() {return s1;}
public static void main(String[] args) {
A a = new C();
System.out.print(a.m1());
}
}
What is the result of attempting to compile and run the program?
a. Prints: A
b. Prints: B
c. Compile-time error.
d. Run-time error.
e. None of the above.

Question 5
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
static void m1(A x) {System.out.print(x.s1);}
static void m1(B x) {System.out.print(x.s1);}
static void m1(C x) {System.out.print(x.s1);}
public static void main(String[] args) {
A a; B b; C c;
a = b = c = new C();
Certified Java Programmer Mock Exam 67
m1(a);m1(b);m1(c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 6
class E {
void printS1(){System.out.print("E.printS1 ");}
static void printS2() {System.out.print("E.printS2");}
}
class F extends E {
void printS1(){System.out.print("F.printS1 ");}
static void printS2() {System.out.print("F.printS2");}
public static void main (String args[]) {
E x = new F();
x.printS1(); x.printS2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: E.printS1 E.printS2
b. Prints: E.printS1 F.printS2
c. Prints: F.printS1 E.printS2
d. Prints: F.printS1 F.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 7
class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
public static void main(String[] args) {
C c = new C();
c.m();
B b = c;
b.m();
A a = b;
a.m();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 8
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
C c1 = new C();
A c2 = new C();
c2.m1(a1);
c2.m1(b1);
c2.m1(c1);
}
}
What is the result of attempting to compile and run the program?
Certified Java Programmer Mock Exam 68
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 9
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Yellow {
6. public static void main(String args[]) {
7. Base base = new Sub();
8. I1 i1 = base;
9. Sub sub = (Sub)base;
10. I2 i2 = (Sub)base;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 10
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 11
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 12
interface Z {void m1();} // 1
class D implements Z {public final void m1() {}} // 2
class E implements Z {public synchronized void m1() {}} // 3
class F implements Z {public strictfp void m1() {}} // 4
class G implements Z {public native void m1();} // 5
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
Certified Java Programmer Mock Exam 69
d. 4
e. 5
f. None of the above.

Question 13
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
B c2 = new C();
C c3 = new C();
C c4 = new C();
c4.m1(c1);
c4.m1(c2);
c4.m1(c3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 14
class Leg{}
class Fur{}
abstract class Pet {
public abstract void eat();
public abstract void sleep();
}
class Dog extends Pet {
Leg leftFront = new Leg();
Leg rightFront = new Leg();
Leg leftRear = new Leg();
Leg rightRear = new Leg();
Fur fur = new Fur();
public Fur shed() {return fur;}
public void eat() {}
public void sleep() {}
}
class Cat extends Dog {
public void ignoreOwner() {}
public void climbTree() {}
}
Which of the following are true statements?
a. A Cat object inherits Fur and four Legs from the Dog super class.
b. A Cat object is able to sleep and eat.
c. A Cat object is able to climb a tree.
d. The relationship between Dog and Pet is an example of an appropriate use of inheritance.
e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.
f. None of the Above.

Question 15
class L {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
char a = 1;
long b = 2;
System.out.print(m(a)+","+ m(b));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
Certified Java Programmer Mock Exam 70
g. None of the Above

Question 16
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Gray {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = {new Sub()};
9. Object obj = sub;
10. base = obj;
11. }
12. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 17
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(A x) {System.out.print("A");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Capítulo 6 – Object Oriented Programming (C)


Question 1
Which of the following are modifiers that can be applied to a method declaration within an interface?
a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. native
g. None of the above.

Question 2
Which of the following are modifiers that can be applied to a field declaration within an interface?
a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 3
Which of the following are modifiers that can be applied to an interface that is not a member of an enclosing class?
a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 4
interface A {
void m1(); // 1
public void m2(); // 2

Certified Java Programmer Mock Exam 71


protected void m3(); // 3
private void m4(); // 4
abstract void m5(); // 5
}
Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above.

Question 5
interface A {void main(String[] args);} // 1
interface B {public void main(String[] args);} // 2
interface C {public static void main(String[] args);} // 3
interface D {protected void main(String[] args);} // 4
interface E {private void main(String[] args);} // 5
Which interface declarations generate a compile-time error?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above.

Question 6
interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
String s1 = "C";
void printIt() {
System.out.print(((A)this).s1 + ((B)this).s1 +
((C)this).s1 + ((I)this).s1);
}
public static void main (String[] args) {
new C().printIt();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: ABCI
b. Runtime Exception
c. Compiler Error
d. None of the Above

Question 7
1. interface I10 {
2. String name = "I10";
3. String s10 = "I10.s10";
4. }
5. interface I20 {
6. String name = "I20";
7. String s20 = "I20.s20";
8. }
9. class C10 implements I10, I20 {
10. public static void main(String[] args) {
11. System.out.print(s10+",");
12. System.out.print(s20+",");
13. System.out.print(name);
14. }
15. }
What is the result of attempting to compile and run the above program?
a. Prints: I10.s10,I20.s20,I10
b. Prints: I10.s10,I20.s20,I20
c. Prints: I10.s10,I20.s20,
d. Prints: I10.s10,I20.s20,null
e. Compiler error at line 9.
f. Compiler error at line 11.
g. Compiler error at line 12.
h. Compiler error at line 13.
i. Runtime Error
j. None of the Above

Question 8
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
Certified Java Programmer Mock Exam 72
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
static void m(A x, C y) {System.out.print("AC");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Prints: AC
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 9
class A {}
class B extends A {}
class C extends B {
static void m1(A x) {System.out.print("m1A");}
static void m2(B x) {System.out.print("m2B"); m1(x);}
static void m2(A x) {System.out.print("m2A"); m1(x);}
static void m3(C x) {System.out.print("m3C"); m2(x);}
static void m3(B x) {System.out.print("m3B"); m2(x);}
static void m3(A x) {System.out.print("m3A"); m2(x);}
public static void main(String[] args) {
m3(new C());
}
}
What is the result of attempting to compile the program?
a. Prints: m3Am2Am1A
b. Prints: m3Bm2Bm1A
c. Prints: m3Cm2Bm1A
d. Prints: m3Cm2Am1A
e. Compiler error
f. Run time error
g. None of the Above

Question 10
class M {
static String s1 = "M.s1";
static void printS1(){System.out.print("M.printS1,"+s1+" ");}
M() { printS1();}
}
class N extends M {
static String s1 = "N.s1";
static void printS1(){System.out.print("N.printS1,"+s1+" ");}
N() { printS1();}
public static void main(String[] args) {
N n = new N();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: M.printS1,M.s1 N.printS1,N.s1
b. Prints: N.printS1,N.s1 N.printS1,N.s1
c. Prints: N.printS1,N.s1 M.printS1,M.s1
d. Prints: N.printS1,M.s1 M.printS1,M.s1
e. Prints: N.printS1,N.s1
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 11
class A {}
class B extends A {}
class C {
void m(A x) {System.out.print("A");}
void m(B x) {System.out.print("B");}
public static void main(String[] args) {
C c = new C();
c.m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: A
Certified Java Programmer Mock Exam 73
b. Prints: B
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 12
abstract class D {
String s1 = "D";
abstract String getS1();
}
class E extends D {
String s1 = "E";
String getS1() {return s1;}
}
class F {
public static void main (String[] s) {
D x = new E();
System.out.print(x.s1 + x.getS1());
}
}
What is the result of attempting to compile and run the above program?
a. Prints: DD
b. Prints: DE
c. Prints: ED
d. Prints: EE
e. Runtime Error
f. Compiler Error
g. None of the Above

Question 13
class G {
String s1 = "G.s1";
void printS1(){System.out.print("G.printS1," + s1);}
G() { printS1();}
}
class H extends G {
String s1 = "H.s1";
void printS1(){System.out.print("H.printS1," + s1);}
public static void main(String[] args) {
H h = new H();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: G.printS1,G.s1
b. Prints: G.printS1,H.s1
c. Prints: G.printS1,null
d. Prints: H.printS1,G.s1
e. Prints: H.printS1,H.s1
f. Prints: H.printS1,null
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 14
class C {
void printS1() {System.out.print("C.printS1 ");}
static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
void printS1(){System.out.print("D.printS1 ");}
void printS2() {System.out.print("D.printS2 ");}
public static void main (String args[]) {
C c = new D();
c.printS1(); c.printS2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Runtime error
f. Compiler error
g. None of the above

Question 15
class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
Certified Java Programmer Mock Exam 74
class D extends C {
void m1() {System.out.print("D");}
void m2() {
m1();
((C)this).m1(); // 1
((B)this).m1(); // 2
((A)this).m1(); // 3
}
public static void main (String[] args) {
new D().m2(); // 4
}
}
What is the result of attempting to compile and run the above program?
a. Prints: DCBA
b. Prints: DDDD
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 16
interface A {
String m1();
class B implements A {public String m1() {return "B";}}
}
class E {
public static void main(String[] args) {
// Insert statement here.
}
}
Which of the following statements could be inserted into the program to print the String ""B"?
a. System.out.print(m1());
b. System.out.print(B.m1());
c. System.out.print(A.B.m1());
d. System.out.print(new B().m1());
e. System.out.print(new A.B().m1());
f. System.out.print(new A().new B().m1());
g. Run"time Error
h. Compile"time Error
i. None of the Above

Question 17
class A {String s1="A";}
class B extends A {String s1="B";}
class C extends B {String s1="C";}
class D extends C {
String s1="D";
void m1() {
System.out.print(s1 + ",");
System.out.print(((C)this).s1 + ","); // 1
System.out.print(((B)this).s1 + ","); // 2
System.out.print(((A)this).s1); // 3
}
public static void main (String[] args) {
new D().m1(); // 4
}
}
What is the result of attempting to compile and run the above program?
a. Prints: D,D,D,D
b. Prints: D,C,B,A
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Capítulo 6 – Object Oriented Programming (D)


Question 1
interface A {
void m1(); // 1
public void m2(); // 2
protected void m3(); // 3
private void m4(); // 4

Certified Java Programmer Mock Exam 75


}
What is the result of attempting to compile the code?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above.

Question 2
interface A {
protected int e = 5; // 1
private int f = 6; // 2
volatile int g = 7; // 3
transient int h = 8; // 4
}
Which of the field declarations result in a compile-time error?
a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 3
Which of the following are modifiers that can be applied to an interface that is not a member of an enclosing class?
a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. None of the above.

Question 4
interface A {
void m1(); // 1
}
class B implements A {
public void m1() {} // 2
}
class C implements A {
protected void m1() {} // 3
}
class D implements A {
private void m1() {} // 4
}
class E implements A {
void m1() {} // 5
}
What is the result of attempting to compile the code?
a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. None of the Above.

Question 5
Which of the following are true statements?
a. The relationship between a class and its super class is an example of a "has-a" relationship.
b. The relationship between a class and its super class is an example of an "is-a" relationship.
c. The relationship between a class and a field within the class is an example of a "has-a" relationship.
d. The relationship between a class and a field within the class is an example of an "is-a" relationship.
e. None of the Above.

Question 6
interface F {abstract void main(String[] args);} // 1
interface G {synchronized void main(String[] args);} // 2
interface H {final void main(String[] args);} // 3
interface I {native void main(String[] args);} // 4
Which interface declarations generate a compile-time error?
a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 7
Which of the following are true statements?
Certified Java Programmer Mock Exam 76
a. An interface that is declared within the body of a class or interface is known as a nested interface.
b. A constant can be a member of an interface.
c. An abstract method declaration can be a member of an interface.
d. A class declaration can be a member of an interface.
e. None of the above.

Question 8
Which of the following modifiers can be applied to a class that is declared within an enclosing interface?
a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 9
Which of the following are modifiers that can be applied to a member interface?
a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 10
1. class A {void m1(String s1) {}}
2. class B extends A {
3. void m1(String s1) {}
4. void m1(boolean b) {}
5. void m1(byte b) throws Exception {}
6. String m1(short s) {return new String();}
7. private void m1(char c) {}
8. protected void m1(int i) {}
9. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Compiler error at line 8.
g. Compiler error at line 9.
h. None of the Above

Question 11
Which of the following are modifiers that can be applied to a member interface?
a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. None of the above.

Question 12
Which of the following are true statements?
a. An interface declaration can be a member of an interface.
b. A method declared within an interface must have a body represented by empty curly braces.
c. An interface can implement another interface.
d. An abstract class that implements an interface must implement all abstract methods declared in the interface.
e. None of the above.

Question 13
class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();
}
}
What is the result of attempting to compile and run the above program?
Certified Java Programmer Mock Exam 77
a. Prints: R.printS1 R.printS2
b. Prints: R.printS1 S.printS2
c. Prints: S.printS1 R.printS2
d. Prints: S.printS1 S.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 14
class A {
String s1 = "A.s1";
String s2 = "A.s2";
}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B();
A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: B.s1 A.s2 B.s1 A.s2
b. Prints: B.s1 A.s2 A.s1 A.s2
c. Prints: A.s1 A.s2 B.s1 A.s2
d. Prints: A.s1 A.s2 A.s1 A.s2
e. Runtime error.
f. Compiler error.
g. None of the Above

Question 15
interface Z {void m1();} // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4

Compile-time errors are generated at which lines?


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

Question 16
interface A {
String m1();
class B implements A {public String m1() {return "B";}}
}
class C implements A {
public String m1() {return "C";}
class D extends B {public String m1() {return "D";}}
}
class E {
public static void main(String[] args) {
// Insert statement here.
}
}
Which of the following statements could be inserted into the program to print the String ""D"?
a. System.out.print(m1());
b. System.out.print(D.m1());
c. System.out.print(C.D.m1());
d. System.out.print(new D().m1());
e. System.out.print(new C.D().m1());
f. System.out.print(new C().new D().m1());
g. Run"time Error
h. Compile"time Error
i. None of the Above

Question 17
class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
String s1="A";
class B extends SuperB { // 1
String s1="B";
void m1() {
System.out.print(this.s1 + ","); // 2
System.out.print(super.s1 + ","); // 3
System.out.print(A.this.s1 + ","); // 4
Certified Java Programmer Mock Exam 78
System.out.print(A.super.s1); // 5
}
}
public static void main (String[] args) {
new A().new B().m1(); // 6
}
}
What is the result of attempting to compile and run the above program?
a. Prints: B,SuperB,B,SuperB
b. Prints: B,SuperB,A,SuperA
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Compiler Error at 5.
h. Compiler Error at 6.
i. Runtime Error
j. None of the Above

Capítulo 7 – Inner Classes (A)


Question 1
abstract class A { // 1
abstract static void m1(); // 2
abstract static class B {} // 3
static class C extends B {} // 4
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 2
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;}
public abstract int math();
}
class B {
static A a1 = new A() {public int math() {return x()+y();}}
static A a2 = new A() {public int math() {return x()-y();}}
static A a3 = new A() {public int math() {return x()*y();}}
static A a4 = new A() {public int math() {return x()/y();}}
public static void main(String[] args) {
System.out.print("" + a1.math() +
a2.math() +
a3.math() +
a4.math());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 18
b. Prints: 6282
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 3
class Z {
void m1() {
abstract class A {} // 1
final class B {} // 2
private class C {} // 3
protected class D {} // 4
public class E {} // 5
static class F {} // 6
synchronized class G {} // 7
transient class H {} // 8
volatile class I {} // 9
strictfp class J {} // 10
}

Certified Java Programmer Mock Exam 79


}
Which of the follow class declarations results in a compiler error?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. 10
k. None of the Above

Question 4
abstract class A {
private int x = 4;
private int y = 2;
public A(int i1) {x=i1;}
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;}
public void incY() {y++;}
public abstract int math();
}
class B {
static A a1 = new A(2) {
{incY();}
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.math());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 5
b. Prints: 6
c. Prints: 7
d. Compile-time error.
e. Run-time error.
f. None of the Above.

Question 5
abstract class A {
private int x = 4;
private int y = 2;
public A(int i1, int i2) {x=i1;y=i2;}
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;}
public abstract int math();
}
class B {
static A a1 = new A(2,1) {public int math() {return x()+y();}};
static A a2 = new A(2,1) {public int math() {return x()-y();}};
static A a3 = new A(2,1) {public int math() {return x()*y();}};
static A a4 = new A(2,1) {public int math() {return x()/y();}};
public static void main(String[] args) {
System.out.print("" + a1.math() +
a2.math() +
a3.math() +
a4.math());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 8
b. Prints: 3122
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 6
abstract class A { // 1
abstract synchronized void m1(); // 2
abstract synchronized class B {} // 3
synchronized class C extends B {} // 4
}
Certified Java Programmer Mock Exam 80
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 7
Which of the follow are true statements.
a. A local class is declared within a block.
b. An anonymous class is always a local class.
c. A local class is a nested class.
d. A local class is a member class.
e. A local class is always a named class.
f. None of the above.

Question 8
class A {
int B; // 1
void B() {} // 2
class B {} // 3
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 9
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 10
class A {
private static int f1 = 1;
private int f2 = 2;
void m1(int p1, final int p2) {
int l1 = 5;
final int l2 = 6;
Object x = new Object() {
int a = f1; // 1
int b = f2; // 2
int c = p1; // 3
int d = p2; // 4
int e = l1; // 5
int f = l2; // 6
};
}
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
Certified Java Programmer Mock Exam 81
d. 4
e. 5
f. 6
g. None of the Above.

Question 11
Which of the following modifiers can be applied to a local class?
a. Public
b. Protected
c. Private
d. Abstract
e. Static
f. Final
g. Strictfp
h. None of the above.

Question 12
class D {
D() {System.out.print("D");}
class Z {Z(){System.out.print("Z");}}
public static void main(String args[]) {
new D.Z();
}
}

What is the result of attempting to compile and run the above program?
a. Prints: D
b. Prints: Z
c. Prints: DZ
d. Prints: ZD
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 13
class E {
E() {System.out.print("E");}
static class Z {Z(){System.out.print("Z");}}
public static void main(String args[]) {
new E.Z();
}
}

What is the result of attempting to compile and run the above program?
a. Prints: E
b. Prints: Z
c. Prints: EZ
d. Prints: ZE
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 14
Which of the follow are true statements.
a. An anonymous class can extend only the Object class.
b. An anonymous class can not implement an interface.
c. An anonymous class can be abstract.
d. An anonymous class is implicitly final.
e. An anonymous class can be static.
f. The class instance creation expression for an anonymous class must never include parameters.
g. An anonymous class must declare at least one constructor.
h. None of the above.

Question 15
1. class F {
2. public void m1() {Z.m1();}
3. private static class Y {
4. private static void m1() {
5. System.out.print("Y.m1 ");
6. }
7. }
8. private static class Z {
9. private static void m1(){
10. System.out.print("Z.m1 ");
11. Y.m1();
12. }
13. }
14. public static void main(String[] args) {
15. new F().m1();
Certified Java Programmer Mock Exam 82
16. }
17. }
What is the result of attempting to compile and run the above program?
a. Compiler error at line 2
b. Compiler error at line 11
c. Runtime error at line 2
d. Runtime error at line 11
e. Prints: Z.m1 Y.m1
f. None of the Above

Question 16
class C {
private static String s1 = "s1";
String s2 = "s2";
C() {m1("s5","s6");}
void m1(final String s5, String s6) {
final String s3 = "s3"; String s4 = "s4";
class Z {
Z() {
System.out.print(???);
}
}
new Z();
}
public static void main(String args[]) {new C();}
}
Which variables can be substituted for ??? without causing a compiler error?
a. s1
b. s2
c. s3
d. s4
e. s5
f. s6
g. Runtime error
h. Compiler error
i. None of the above

Question 17
1. class Red {
2. private static final int a = 10;
3. protected static int b = 20;
4. int c = 30;
5. static class StaticNested {
6. int d = a;
7. int e = b;
8. int f = c;
9. }
10. class NonStaticInner {
11. int g = a;
12. int h = b;
13. int i = c;
14. }
15. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. Compiler error at line 8.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. None of the Above

Capítulo 7 – Inner Classes (B)


Question 1

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;}

Certified Java Programmer Mock Exam 83


public void y(int y) {this.y = y;}
public abstract int math();
}
class B {
static A a1 = new A(2,1) {
public A(int i1, int i2) {x(i1);y(i2);};
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.math());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 8
b. Prints: 3122
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 2
abstract class A { // 1
abstract final void m1(); // 2
abstract final class B {} // 3
class C extends B {} // 4
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 3
Which of the following are class modifiers? Select all that are applicable to a top-level class and all that are applicable to a nested class. It is not
required that a modifier be applicable to both.
a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. static
i. synchronized
j. transient
k. volatile
l. strictfp
m. None of the above.

Question 4
Which of the follow are true statements.
a. A nested class is any class that is declared within the body of another class or interface.
b. A nested class can not be declared within the body of an interface declaration.
c. A top level class is a class this is not a nested class.
d. An inner class is a nested class that is not static.
e. A nested class can not be declared static.
f. A named class is any class that is not anonymous.
g. None of the above.

Question 5
Which of the following modifiers can be applied to a member class?
a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 6
Which of the follow are true statements.
a. An anonymous class can be declared abstract.
b. An incompletely implemented class must be declared abstract.
c. A local class can be declared abstract.
d. An abstract class can be instantiated.
e. An abstact class is implicitly final.
f. An abstact class must declare at least one abstract method.
g. An abstact class can not extend a concrete class.
Certified Java Programmer Mock Exam 84
h. None of the above.

Question 7
abstract class A { // 1
private abstract void m1(); // 2
private abstract class B {} // 3
private class C extends B {} // 4
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above.

Question 8
class A {
A() {} // 1
int A; // 2
void A() {} // 3
class A {} // 4
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 9
If the strictfp modifier is applied to a class, then all float or double expressions in which of the following will also be implicitly strictfp?
a. nested classes
b. methods
c. constructors
d. static initializers
e. instance initializers
f. variable initializers
g. None of the above.

Question 10
abstract class A { // 1
abstract strictfp void m1(); // 2
abstract strictfp class B {} // 3
strictfp class C extends B {} // 4
}
Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 11
class Z {
abstract class A {} // 1
final class B {} // 2
private class C {} // 3
protected class D {} // 4
public class E {} // 5
static class F {} // 6
synchronized class G {} // 7
transient class H {} // 8
volatile class I {} // 9
strictfp class J {} // 10
}
Which of the class declarations results in a compiler error?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. 10
k. None of the Above

Question 12
Certified Java Programmer Mock Exam 85
class A {
private static String s1 = "s1";
final String s2 = "s2";
A () { new Z("s5","s6");}
class Z {
final String s3 = "s3";
String s4 = "s4";
Z (final String s5, String s6) {
System.out.print(???);
}
}
public static void main(String args[]) {new A();}
}
Which variables can be substituted for ??? without causing a compiler error?
a. s1
b. s2
c. s3
d. s4
e. s5
f. s6
g. Runtime error.
h. Compiler error.
i. None of the Above

Question 13
class B {
private static String s1 = "s1";
final String s2 = "s2";
B () { new Z("s5","s6");}
static class Z {
final String s3 = "s3";
static String s4 = "s4";
Z (final String s5, String s6) {
System.out.print(???);
}
}
public static void main(String args[]) {new B();}
}
Which variables can be substituted for ??? without causing a compiler error?
a. s1
b. s2
c. s3
d. s4
e. s5
f. s6
g. Runtime error
h. Compiler error
i. None of the above

Question 14
1. class Outer {
2. static class StaticNested {
3. static final int a = 25;
4. static final int b;
5. static int c;
6. int d;
7. static {b = 42;}
8. }
9. class NonStaticInner {
10. static final int e = 25;
11. static final int f;
12. static int g;
13. int h;
14. static {f = 42;}
15. }
16. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. Compiler error at line 9.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. Compiler error at line 14.
Certified Java Programmer Mock Exam 86
m. None of the Above

Question 15
1. class Red {
2. static class StaticNested {
3. interface ABC {}
4. }
5. class NonStaticInner {
6. interface DEF {}
7. }
8. interface GHI {}
9. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 8.
f. None of the Above

Question 16
class G {
final String s1 = "G.s1";
class Z {
String s1;
void m1() {System.out.println(???);}
}
public static void main(String args[]) {
G g = new G();
g.new Z().m1();
}
}
What field access expression could be used in place of ??? above to cause the program to print "G.s1"?
a. s1
b. G.s1
c. ((G)this).s1
d. G.this.s1
e. G.super.s1
f. None of the Above

Capítulo 8 – Garbage Collection (A)


Question 1
class B {
private String name;
public B(String s) {name = s;}
public void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X");
B y1 = new B("Y");
}
public static void main(String[] args) {
m();
System.gc();
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will run.
g. Compiler Error.
h. Run Time Error.
i. None of the above.

Question 2
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I();

Certified Java Programmer Mock Exam 87


I i2 = new I();
I i3 = new I();
i1.other(i3);
i2.other(i1);
i3.other(i2);
}
public static void main (String[] args) {
new J().m1();
}
}
Which objects are eligible for garbage collection after method m1 returns?
a. i1
b. i2
c. i3
d. None.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 3
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 4
class I {
private String name;
public I(String s) {name = s;}
private I other;
public void other(I i) {other = i;}
}
class J {
private I i1 = new I("A");
private I i2 = new I("B");
private I i3 = new I("C");
private void m1() {
i1.other(i2);
i2.other(i1);
i3.other(i3);
i1 = i3;
i2 = i3;
m2();
}
private void m2() {
// Do amazing things.
}
public static void main (String[] args) {
new J().m1();
}
}
Which of the three objects, A, B, C, are eligible for garbage collection when method m2 begins to execute?
a. A
b. B
Certified Java Programmer Mock Exam 88
c. C
d. None.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 5
void m1() {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(); // 1
m2(q1); // 2
}
System.out.print("All done"); // 3
}
When the processing of line 3 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. Compiler error.
g. Run time error.
h. None of the above.

Question 6
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1 = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 7
void m1() {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(); // 1
}
System.out.print("All done"); // 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. Compiler error.
g. Run time error.
h. None of the above.

Question 8
class I {
private String name;
public String name() {return name;}
public I(String s) {name = s;}
Certified Java Programmer Mock Exam 89
}
class J {
public static void m1(I i) {i = null;}
public static void main (String[] args) {
I i = new I("X"); // 1
m1(i); // 2
System.out.print(i.name()); // 3
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. Prints X.
d. Compiler error.
e. None of the above.

Capítulo 8 – Garbage Collection (Opcionais)


Question 1
import java.util.*;
class A {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private static ArrayList finalizedNames = new ArrayList();
public static Iterator finalizedNames() {
return finalizedNames.iterator();
}
private String name;
public A(String s) {name = s; notFinalized++;}
public void finalize() {
finalizedNames.add(name);
notFinalized--;
}
}
class F {
public static void main(String[] args) {
A x1 = new A("X");
A y1 = new A("Y");
x1= null;
y1 = null;
System.gc();
while (A.notFinalized() > 0) {
try {Thread.sleep(1000);} catch (InterruptedException ie) {}
}
Iterator i = A.finalizedNames();
while (i.hasNext()) {
System.out.print(i.next());
}
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will run.
g. There is no guarantee that F.main will run to completion.
h. Compiler Error.
i. Run Time Error.
j. None of the above.

Question 2
class J {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private K k;
private int name;
public int name() {return name;}
public J(K k, int i) {this.k = k; name = i; notFinalized++;}
public void finalize() {
synchronized (k) {
System.out.print(name);
notFinalized--;
k.notify();
}
}

Certified Java Programmer Mock Exam 90


}

class K {
private void m1() {
J j = null;
for (int i = 0; i < 5; i++) {
j = new J(this, i); // 1
}
Runtime.getRuntime().gc(); // 2
synchronized (this) {
while (J.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
}
public static void main(String[] args) {
new K().m1();
}
}
When the processing of line 2 begins how many objects of type J that were created at line 1 are eligible for garbage collection?
a. 0
b. 1
c. 4
d. 5
e. Can not be determined without more information.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 3
class Arnold {
private H h;
private String quote;
public String quote() {return quote;}
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
public Arnold(H h, String s) {this.h = h; quote = s; notFinalized++;}
public void finalize() {
synchronized (h) {
notFinalized--;
h.arnold(this);
h.notify();
}
}
}
class H {
private Arnold arnold;
public void arnold(Arnold a) {arnold = a;}
public void m1() {
arnold = new Arnold(this, "I'll be back!");
arnold = null;
System.gc();
synchronized (this) {
while (Arnold.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
System.out.print(arnold.quote()); // 1
}
public static void main(String[] args) {new H().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again.
b. NullPointerException is thrown at line 1.
c. Prints: I'll be back!
d. Prints nothing.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 4
class I {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private J observer;
private I other;
public void other(I i) {other = i;}
private String name;
public String name() {return name;}
Certified Java Programmer Mock Exam 91
public I(String s, J j) {this (s, j, null);}
public I(String s, J j, I i) {
name = s;
observer = j;
other = i;
notFinalized++;
}
public void finalize() {
System.out.print(name);
synchronized (observer) {
notFinalized--;
observer.notify();
}
}
}
class J {
private void m1() {
I i1 = new I("X", this);
I i2 = new I("Y", this, i1);
I i3 = new I("Z", this, i2);
i1.other(i3);
i3 = i2 = i1 = null; // 1
Runtime.getRuntime().gc();
synchronized (this) {
while (I.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
}
public static void main (String[] args) {
new J().m1();
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. All three instances of I form a ring where each instance within the ring has a reference to the next instance.
b. All three instances of I are eligible for garbage collection after references i1, i2 and i3 are set to null.
c. There is no guarantee that the garbage collector will run.
d. The order in which the instances in the ring are garbage collected is not guaranteed.
e. The name of all three instances of I might be printed.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 5
class J {
private static int finalized;
public static int finalized() {return finalized;}
private K k;
private String name;
public String name() {return name;}
public J(K k, String s) {this.k = k; name = s;}
public void finalize() {
synchronized (k) {
finalized++;
k.j(this);
k.notify();
}
}
}
class K {
private J j1;
public void j(J j) {j1 = j;}
public void m1() {
j1 = new J(this, "X");
j1.finalize();
j1.finalize();
for (int i = 0; i < 3; i++) {
int finalized = J.finalized();
j1= null;
System.gc();
synchronized (this) {
if (J.finalized() == finalized) {
try {wait(1000);} catch (InterruptedException ie) {}
}
}
if (j1 != null) {
System.out.print(j1.name());
}
}
Certified Java Programmer Mock Exam 92
System.out.print(J.finalized());
}
public static void main(String[] args) {new K().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again.
b. The user code can invoke the finalize method more than once.
c. The garbage collector will never call the finalize method more than once.
d. The output could include the character X.
e. The output could include more than one character X.
f. The output could include the number 3.
g. The output could include a number larger than 3.
h. Compiler error.
i. Run time error.
j. None of the above.

Question 6
import java.util.*;
class C {
private Object observer;
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private static ArrayList finalizedNames = new ArrayList();
public static Iterator finalizedNames() {
return finalizedNames.iterator();
}
private String name;
public C(Object obs, String s) {observer = obs; name = s; notFinalized++;}
public void finalize() {
synchronized (observer) {
finalizedNames.add(name);
notFinalized--;
observer.notify();
}
}
}
class G {
public void m1() {
C x1 = new C(this, "X");
C y1 = new C(this, "Y");
x1= null;
y1 = null;
System.gc();
synchronized (this) {
while (C.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
Iterator i = C.finalizedNames();
while (i.hasNext()) {
System.out.print(i.next());
}
}

public static void main(String[] args) {


new G().m1();
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will run.
g. There is no guarantee that G.main will run to completion.
h. Compiler Error.
i. Run Time Error.
j. None of the above.

Question 7
class Arnold {
private I i;
private String quote;
public String quote() {return quote;}
private static int instances;
public static int instances() {return instances;}
public static void heIsBack() {instances++;}
public Arnold(I i, String s) {this.i = i; quote = s; instances++;}
Certified Java Programmer Mock Exam 93
public void finalize() {
synchronized (i) {
instances--;
i.arnold(this);
i.notify();
}
}
}
class I {
private Arnold arnold;
public void arnold(Arnold a) {arnold = a;}
public void m1() {
arnold = new Arnold(this, "I'll be back!");
for (int i = 0; i < 3; i++) {
arnold = null;
System.gc();
synchronized (this) {
if (Arnold.instances() > 0) {
try {wait(1000);} catch (InterruptedException ie) {}
}
}
if (arnold != null) {
Arnold.heIsBack();
System.out.print(arnold.quote());
}
}
}
public static void main(String[] args) {new I().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again.
b. The garbage collector will never call the finalize method more than once.
c. Could print Arnold's quote more than once.
d. Can not print Arnold's quote more than once.
e. Could print nothing.
f. Compiler error.
g. Run time error.
h. None of the above.

Capítulo 8 – Initializers (A)


Question 1
class Q {
int i = 1;
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
Q(int x) {System.out.print("4");}
}
class R extends Q {
int j = 2;
{System.out.print("5");}
static {System.out.print("6");}
R() {super(j);System.out.print("7");}
}
class S {
public static void main(String[] args) {
new R();
}
}
What is the result of attempting to compile and run the program?
a. Prints: 261357
b. Prints: 261457
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 2
class P {void m1() throws Exception {}}
class Q {
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
}
class R extends Q {

Certified Java Programmer Mock Exam 94


{System.out.print("4"); new P().m1();}
static {System.out.print("5");}
R() {System.out.print("6");}
}
class S {
public static void main(String[] args) {
new R();
}
}
What is the result of attempting to compile and run the program?
a. Prints: 251346
b. Prints: 256314
c. Prints: 123456
d. Prints: 142356
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 3
class C {
{System.out.print("1");}
static {System.out.print("2");}
C() {System.out.print("3");}
String m() {return "4";}
}
class D {
{System.out.print("5");}
static {System.out.print("6");}
D() {System.out.print("7");}
public static void main(String[] args) {
System.out.print("8");
System.out.print(new C().m());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 65782134
b. Prints: 681234
c. Prints: 682134
d. Prints: 68572134
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 4
class A {
static {System.out.print("1");}
{System.out.print("2");}
A() {System.out.print("3");}
static String m() {return "4";}
}
class B {
static {System.out.print("5");}
{System.out.print("6");}
B() {System.out.print("7");}
public static void main(String[] args) {
System.out.print("8");
System.out.print(A.m());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 5814
b. Prints: 15814
c. Prints: 581234
d. Prints: 58123674
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 5
class D {
{System.out.print("1");}
static {System.out.print("2");}
D() {System.out.print("3");}
D(String s) {System.out.print("4");}
}
class E extends D {
{System.out.print("5");}
static {System.out.print("6");}
E(String s) {System.out.print("7");super(s);}
Certified Java Programmer Mock Exam 95
}
class F {
public static void main(String[] args) {
new E("e");
}
}
What is the result of attempting to compile and run the program?
a. Prints: 261457
b. Prints: 261357
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 6
class D {
{System.out.print("1");}
static {System.out.print("2");}
D() {System.out.print("3");}
D(String s) {System.out.print("4");}
}
class E extends D {
{System.out.print("5");}
static {System.out.print("6");}
E() {System.out.print("7");this("e");}
E(String s) {System.out.print("8");}
}
class F {
public static void main(String[] args) {
new E();
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2613587
b. Prints: 2614587
c. Prints: 2671358
d. Prints: 2678315
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 7
class Red {
Red() {System.out.print("Constructor ");}
static {
System.out.print("Static ");
}
{
System.out.print("Instance ");
}
public static void main(String args[]) {
Red red = new Red();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Instance Static Constructor
b. Prints: Static Constructor Instance
c. Prints: Constructor Instance Static
d. Prints: Constructor Static Instance
e. Prints: Static Instance Constructor
f. Prints: Instance Constructor Static
g. Runtime error
h. Compiler error
i. None of the above

Question 8
class Blue {
Blue() {System.out.print("Constructor ");}
static {
System.out.print("Static ");
}
public static void main(String args[]) {
System.out.print("Main ");
Blue blue = new Blue();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Constructor Static Main
Certified Java Programmer Mock Exam 96
b. Prints: Static Main Constructor
c. Prints: Main Constructor Static
d. Prints: Main Static Constructor
e. Prints: Static Constructor Main
f. Prints: Constructor Main Static
g. Runtime error.
h. Compiler error.
i. None of the Above

Question 9
class Orange {
static {
System.out.print("Static1 ");
}
static String string1 = echo("string1 ");
static {
System.out.print("Static2 ");
}
static String echo (String s) {
System.out.print(s);
return s;
}
public static void main(String args[]) {
Orange o = new Orange();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Static1 string1 Static2
b. Prints: string1 Static2 Static1
c. Prints: Static2 Static1 string1
d. Prints: Static2 string1 Static1
e. Prints: string1 Static1 Static2
f. Prints: Static1 Static2 string1
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 10
class Q {
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
}
class R extends Q {
{System.out.print("4");}
static {System.out.print("5");}
R() {System.out.print("6");}
static String m1() {return "7";}
}
class S {
public static void main(String[] args) {
System.out.print(R.m1());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 7
b. Prints: 57
c. Prints: 257
d. Prints: 527
e. Prints: 2135467
f. Prints: 2514367
g. Prints: 2513467
h. Compiler Error
i. Runtime Error
j. None of the Above

Question 11
class A {
{System.out.print("1");}
static {System.out.print("2");}
A(String s) {System.out.print("3");}
}
class B extends A {
{System.out.print("4");}
static {System.out.print("5");}
B(String s) {System.out.print("6");}
}
class C {
public static void main(String[] args) {
Certified Java Programmer Mock Exam 97
new B("b");
}
}
What is the result of attempting to compile and run the program?
a. Prints: 251346
b. Prints: 256314
c. Prints: 142346
d. Prints: 123456
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 12
class Green {
static {
System.out.print("Static ");
}
String s1 = echo("s1 ");
static String s2 = echo("s2 ");
static String echo (String s) {
System.out.print(s);
return s;
}
public static void main(String args[]) {
Green g = new Green();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Static s1 s2
b. Prints: s1 s2 Static
c. Prints: s2 Static s1
d. Prints: s2 s1 Static
e. Prints: s1 Static s2
f. Prints: Static s2 s1
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 13
class E {
{System.out.print("1");}
static {System.out.print("2");}
E() {System.out.print("3");}
String m() {return "4";}
}
class F {
{System.out.print("5");}
static {System.out.print("6");}
F() {System.out.print("7");}
public static void main(String[] args) {
System.out.print("8");
System.out.print(new E().m());
System.out.print(new E().m());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 82134134
b. Prints: 682134134
c. Prints: 6821342134
d. Prints: 657821342134
e. Prints:
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 14
class T {
{System.out.print("1");}
static {System.out.print("2");}
T() {System.out.print("3");}
}
class U extends T {
{System.out.print("4");}
static {System.out.print("5");}
U() {System.out.print("6");}
}
class V {
public static void main(String[] args) {
new U();
Certified Java Programmer Mock Exam 98
}
}
What is the result of attempting to compile and run the program?
a. Prints: 36
b. Prints: 1346
c. Prints: 123456
d. Prints: 213546
e. Prints: 251346
f. Prints: 251436
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 15
class A {
A() {System.out.print("CA ");}
{System.out.print("IA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
{System.out.print("IB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {
B b = new B();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: SA IA CA SB IB CB
b. Prints: SA SB IA CA IB CB
c. Prints: SA SB IA IB CA CB
d. Prints: SB IB SA IA CA CB
e. Prints: SB IB CB SA IA CA
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 16
class A {
{System.out.print("1");}
static {System.out.print("2");}
A(String s) {System.out.print("3");}
}
class B extends A {
{System.out.print("4");}
static {System.out.print("5");}
B(String s) {super(s);System.out.print("6");}
}
class C {
public static void main(String[] args) {
new B("b");
}
}
What is the result of attempting to compile and run the program?
a. Prints: 251346
b. Prints: 256314
c. Prints: 142346
d. Prints: 123456
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 17
class J {
{System.out.print("1");}
static {System.out.print("2");}
J() {System.out.print("3");}
J(String s) {System.out.print("4");}
}
class K extends J {
{System.out.print("5");}
static {System.out.print("6");}
K() {System.out.print("7");}
K(String s) {System.out.print("8");}
}
class L {
public static void main(String[] args) {
new K("!");
}
Certified Java Programmer Mock Exam 99
}
What is the result of attempting to compile and run the program?
a. Prints: 261458
b. Prints: 261358
c. Prints: 261357
d. Prints: 268135
e. Prints: 268145
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 18
class B {
public static void main(String[] args) {
int a = 2;
char b,c,d;
b = (a < 2) ? 'f' : 'g'; // 1
if (a < 2) c = 'h'; else c = 'i'; // 2
if (a < 2) d = 'j'; // 3
if (a > 2) d = 'k'; // 4
if (a == 2) d = 'l'; // 5
System.out.print(b + "," + c + "," + d); // 6
}
}
What is the result of attempting to compile and run the above program?
a. Prints: g,i,l
b. Compiler Error: variable b might not have been initialized.
c. Compiler Error: variable c might not have been initialized.
d. Compiler Error: variable d might not have been initialized.
e. Runtime Exception
f. None of the Above

Question 19
class M {
{System.out.print("1");}
static {System.out.print("2");}
M() {System.out.print("3");}
M(String s) {System.out.print("4");}
}
class N extends M {
{System.out.print("5");}
static {System.out.print("6");}
N() {this("@"); System.out.print("7");}
N(String s) {System.out.print("8");}
}
class P {
public static void main(String[] args) {
new N();
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2613578
b. Prints: 2613587
c. Prints: 261357
d. Prints: 2614587
e. Prints: 2678315
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 20
class G {
{System.out.print("1");}
static {System.out.print("2");}
G() {System.out.print("3");}
String m1() {return "4";}
static String m2() {return "5";}
}
class H extends G {
{System.out.print("6");}
static {System.out.print("7");}
H() {System.out.print("8");}
String m1() {return "9";}
static String m2() {return "A";}
}
class I {
public static void main(String[] args) {
System.out.print(H.m2());
System.out.print(new H().m1());
Certified Java Programmer Mock Exam 100
}
}
What is the result of attempting to compile and run the program?
a. Prints: 7A689
b. Prints: 271368A9
c. Prints: 27A13689
d. Prints: 27A2713689
e. Compiler Error
f. Runtime Error
g. None of the Above

Capítulo 9 – Threads (A)


Question 1
Which of the following methods are deprecated members of the Object or Thread class?
a. countStackFrames
b. join
c. notify
d. notifyAll
e. resume
f. run
g. sleep
h. start
i. stop
j. suspend
k. yield
l. wait
m. None of the above.

Question 2
Which of the following methods are declared or implemented in the Thread class but not inherited from the Object class?
a. Join
b. Notify
c. NotifyAll
d. resume
e. run
f. sleep
g. start
h. stop
i. suspend
j. yield
k. wait
l. None of the above.

Question 3
class A extends Thread {
public void run() {
try{sleep(10000);} catch (InterruptedException ie){}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime);
}
}
What are the possible results of attempting to compile and run the program?
a. Prints a number greater than or equal to 0.
b. The number printed must always be greater than 10000.
c. This program will run for at least ten seconds.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 4
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();

Certified Java Programmer Mock Exam 101


a1.start();
a1.interrupt();
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: true.
b. Prints: false.
c. Compiler error.
d. Run time error.
e. None of the above.

Question 5
class A extends Thread {
String[] sa;
public A(String[] sa) {
this.sa = sa;
}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}
}
System.out.print(sa[1] + sa[2] + sa[3]);
}
}
class B {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A(sa);
t1.start();
synchronized (sa) {
sa[0] = "Done";
sa[1] = "A";
sa[2] = "B";
sa[3] = "C";
sa.notify();
}
}
}
Which of the following are possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 6
class A extends Thread {
private Object obj;
public A(Object obj) {this.obj = obj;}
public void run() {
try {
synchronized (obj) {obj.wait();}
} catch (InterruptedException ie) {}
System.out.print(Thread.currentThread().getName());
}
}
class B {
private void m1() {
for (int i = 0; i < 10; i++) {
A t1 = new A(this);
t1.setName(String.valueOf(i));
t1.setDaemon(true);
t1.start();
}
synchronized (this) {notifyAll();}
}
public static void main(String[] args) {
new B().m1();
}
}
What are the possible results of attempting to compile and run the program?
a. All of the numbers 0 through 9 must always be printed.
b. Some or all of the numbers 0 through 9 could be printed.
c. There is no guarantee that anything will be printed.
d. Compiler error.
Certified Java Programmer Mock Exam 102
e. Run time error.
f. None of the above.

Question 7
Which of the following are true?
a. The Thread.run method is used to start a new thread running.
b. The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler.
c. The Runnable interface declares the start method.
d. The Runnable interface declares the run method.
e. The Thread class implements the Runnable interface.
f. If an Object.notify method call appears in a synchronized block, then it must be the last method call in the block.
g. No restriction is placed on the number of threads that can enter a synchronized method.
h. Some implementations of the Thread.yield method will not yield to a thread of lower priority.
i. None of the above.

Question 8
class A extends Thread {
public A(Runnable r) {super(r);}
public void run() {System.out.print("A");}
}
class B implements Runnable {
public void run() {System.out.print("B");}
}
class C {
public static void main(String[] args) {
new A(new B()).start();
}
}
What is the result of attempting to compile and run the program?
a. Prints: A
b. Prints: B
c. Prints: AB
d. Prints: BA
e. Compiler error
f. Run time error
g. None of the above

Question 9
Which of the following are true?
a. A program will terminate only when all daemon threads stop running.
b. A program will terminate only when all user threads stop running.
c. A daemon thread always runs at Thread.MIN_PRIORITY.
d. A thread inherits its daemon status from the thread that created it.
e. The daemon status of a thread can be changed at any time using the Thread.setDaemon method.
f. The Thread.setDaemon method accepts one of two argument values defined by the constants Thread.DAEMON and Thread.USER.
g. None of the above.

Question 10
class C extends Thread {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public void run() {
synchronized (this) {
while (!sa[0].equals("Done")) {
try {wait();} catch (InterruptedException ie) {}
}
}
System.out.print(sa[1] + sa[2] + sa[3]);
}
void m1() {
start();
synchronized (this) {
sa[0] = "Done";
sa[1] = "A";
sa[2] = "B";
sa[3] = "C";
}
}
public static void main (String[] args) {
new C().m1();
notify();
}
}
Which of the following are possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compile-time error.
f. Run time error.
Certified Java Programmer Mock Exam 103
g. None of the above.

Question 11
A Thread can become the owner of an object's monitor by which three of the following?
a. By invoking the sleep method on the Thread.
b. By entering any synchronized static method of the Thread class.
c. By blocking on IO.
d. By entering a synchronized instance method.
e. By invoking the wait method on the Object.
f. By entering the body of a block that is synchronized on the Object.
g. By entering a synchronized static method of a class of type Class.
h. By invoking the notify method on the Object.
i. None of the above.

Question 12
class A extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000;
while (System.currentTimeMillis() < endTime) {
yield();
}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
sleep(1000);
a1.interrupt();
a1.join();
System.out.print(System.currentTimeMillis() - startTime);
}
}
What are the possible results of attempting to compile and run the program?
a. Prints a number that is less than 1000.
b. Prints a number between 1000 and 9999.
c. Prints a number larger than 10000.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 13
Which statements are true?
a. The process of executing a synchronized method requires the thread to acquire a lock.
b. Any overriding method of a synchronized method is implicitly synchronized.
c. If any method in a class is synchronized, then the class itself must also be declared using the synchronized modifier.
d. A thread must first acquire the lock on the object instance on which a synchronized static method is invoked.
e. None of the above.

Question 14
class A extends Thread {
public void run() {
synchronized (this) {
try{wait(5000);} catch (InterruptedException ie){}
}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
try {a1.join(6000);} catch (InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}
}
What are the possible results of attempting to compile and run the program?
a. The first number printed is greater than or equal to 0.
b. The first number printed must always be greater than 5000.
c. The second number printed must always be greater than 5000.
d. The second number printed must always be greater than 6000.
e. The synchronized block inside the run method is not necessary.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 15
class A extends Thread {
public void run() {
System.out.print("A");
Certified Java Programmer Mock Exam 104
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
try {
a.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
a.start(); // 1
}
}
What is the result of attempting to compile and run the program?
a. The program compiles and runs without error.
b. The second attempt to start thread t1 is successful.
c. The second attempt to start thread t1 is ignored.
d. Compiler error at marker 1.
e. An IllegalThreadStateException is thrown at run-time.
f. None of the above

Question 16
class A implements Runnable{public void run() {}}
class B {
public static void main(String[] args) {
ThreadGroup tg = new ThreadGroup("group1");
Thread t1 = new Thread(tg); // 1
Thread t2 = new Thread(tg, new A()); // 2
Thread t3 = new Thread(tg, new A(), "A"); // 3
Thread t4 = new Thread(tg, "A"); // 4
}
}
A compile-time error is generated at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. None of the above.

Question 17
class A extends Thread {
static long startTime;
public void run() {
for (int i = 0; i < 99999; i++) {
Math.sin(i);
}
String name = Thread.currentThread().getName();
long time = System.currentTimeMillis();
System.out.println(name + " done at " + (time - startTime));
}
public static void main(String[] args) {
A t1 = new A();
A t2 = new A();
t1.setName("T1");
t2.setName("T2");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
startTime = System.currentTimeMillis();
t1.start();
t2.start();
}
}
Which of the following statements are true?
a. The priority assigned to thread T2 is greater than the priority assigned to T1.
b. Java guarantees that Thread T2 will get more CPU time than T1.
c. Java guarantess that Thread T2 will run to completion before T1.
d. None of the above.

Question 18
class A extends Thread {
private static B b = new B();
private String s1;
public void run() {
System.out.print(b.m1(s1));
}
A(String threadName, String s1) {
super(threadName);
this.s1 = s1;
Certified Java Programmer Mock Exam 105
}
public static void main (String[] args) {
A a = new A("T1","A");
A b = new A("T2","B");
a.start();
b.start();
}
}
class B {
private String s1;
public synchronized String m1(String s) {
s1 = s;
Thread.yield();
return "[" +
Thread.currentThread().getName() +
"," + s1 + "]";
}
}
What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: [T1,A][T2,B].
c. Prints: [T1,B][T2,B].
d. Prints: [T2,B][T1,A].
e. Prints: [T2,A][T1,A].
f. Compiler error.
g. Run time error.
h. None of the above.

Question 19
class A implements Runnable {
public void run() {
String lock = "";
synchronized (lock) {
System.out.print(Thread.currentThread().getName() + 1);
System.out.print(Thread.currentThread().getName() + 2);
System.out.print(Thread.currentThread().getName() + 3);
}
}
public static void main (String[] args) {
new Thread(new A(), "A").start();
new Thread(new A(), "B").start();
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: A1A2A3B1B2B3.
b. Prints: B1B2B3A1A2A3.
c. Prints: A1B1A2B2A3B3.
d. Prints: B1A1B2A2B3A3.
e. Compiler error.
f. Run time error.
g. None of the above.

Capítulo 9 – Threads (B)


Question 1
Which of the following methods are members of the Object class?
a. join
b. notify
c. notifyAll
d. resume
e. run
f. sleep
g. start
h. stop
i. suspend
j. yield
k. wait
l. None of the above.

Question 2

Which of the following methods are static members of the Thread class?
a. join
b. notify
c. notifyAll
d. resume
e. run

Certified Java Programmer Mock Exam 106


f. sleep
g. start
h. stop
i. suspend
j. yield
k. wait
l. None of the above.

Question 3
Which of the following are true?
a. Thread.MAX_PRIORITY = 10.
b. Thread.MAX_PRIORITY = 5.
c. Thread.NORM_PRIORITY = 5.
d. Thread.NORM_PRIORITY = 3.
e. Thread.NORM_PRIORITY = 0.
f. Thread.MIN_PRIORITY = 1.
g. Thread.MIN_PRIORITY = 0.
h. Thread.MIN_PRIORITY = -5.
i. Thread.MIN_PRIORITY = -10.
j. None of the above.

Question 4
class AnException extends Exception {}
class A extends Thread {
public void run() throws AnException {
System.out.print("A");
throw new AnException();
}
}

class B {
public static void main (String[] args) {
A a = new A();
a.start();
System.out.print("B");
}
}
What is the result of attempting to compile and run the program?
a. Prints: A
b. Prints: B
c. Prints: AB
d. Prints: BA
e. Compiler error
f. Run time error
g. None of the above

Question 5
class A extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000;
while (System.currentTimeMillis() < endTime) {
yield();
if (isInterrupted()) {
return;
}
}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
try {sleep(1000);} catch(InterruptedException ie) {}
a1.interrupt();
try {a1.join();} catch(InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}
}
What are the possible results of attempting to compile and run the program?
a. Prints a number that is less than 1000.
b. Prints a number greater than 1000.
c. The printed number must always be greater than 10000.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 6
The synchronized statement has the following form.

Certified Java Programmer Mock Exam 107


synchronized (expression) block

Which statements are true?


a. A compile time error occurs if the expression is a reference type.
b. A compile time error occurs if the expression is a primitive type.
c. A compile time error does not occur if the expression is of type boolean.
d. The sychronized block may be processed normally if the expression is null.
e. If execution of the block completes normally, then the lock is unlocked.
f. If execution of the block completes abruptly, then the lock is unlocked.
g. A thread can hold more than one lock at a time.
h. Synchronized statements can be nested.
i. Synchronized statements with identical expressions can be nested.
j. None of the above.

Question 7
Which of the following is true?
a. The Thread.yield method might cause the thread to move to the Not-Runnable state.
b. The Thread.yield method might cause the thread to move to the ready state.
c. The same thread might continue to run after calling the Thread.yield method.
d. The Thread.yield method is a static method.
e. The behavior of the Thread.yield method is consistent from one platform to the next.
f. The Thread.sleep method causes the thread to move to the Not-Runnable state.
g. The Thread.sleep method causes the thread to move to the ready state.
h. None of the above.

Question 8
class A extends Thread {
private C c1;
public A(String s1, C c1) {super(s1); this.c1 = c1;}
public void run() {
for (int i = 0; i < 3; i++) {
System.out.print(currentThread().getName() + c1.getI());
}
}
}

class C {
private int i;
public synchronized int getI() {
int oldValue = i;
Thread.yield();
i += 1;
return oldValue;
}
public static void main (String[] args) {
C c = new C();
Thread t1 = new A("X", c);
Thread t2 = new A("Y", c);
t1.start();
t2.start();
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: X0Y1X2Y3X4Y5.
b. Prints: X0X1X2Y3Y4Y5.
c. Prints: X0Y0X1Y1X2Y2.
d. Prints: X0X1X2Y0Y1Y2.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 9
class A extends Thread {
String[] sa;
public A(String[] sa) {
this.sa = sa;
}
public void run() {
synchronized (sa) {
System.out.print(sa[0] + sa[1] + sa[2]);
}
}
}
class B {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new A(sa);
t1.start();
Certified Java Programmer Mock Exam 108
sa[0] = "A";
sa[1] = "B";
sa[2] = "C";
}
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 10
Suppose that you would like to stop a thread gracefully and release any locks that the thread might be holding. Which of the following three
techniques is preferred over the other two?
a. Invoke the Thread.stop method.
b. Invoke the Thread.destroy method.
c. Return from the run method based on the state of a boolean flag.

Question 11
class A implements Runnable{public void run() {}}
class B {
public static void main(String[] args) {
Thread t1 = new Thread(); // 1
Thread t2 = new Thread(new A()); // 2
Thread t3 = new Thread(new A(), "A"); // 3
Thread t4 = new Thread("A"); // 4
}
}
A compile-time error is generated at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. None of the above.

Question 12
After invoking the wait method on an Object, Obj1, a Thread, T1, will remain in the wait set of Obj1 until which of the following occurs?
a. Another Thread invokes the notify method on the Object, Obj1, and T1 is selected to move out of the wait set.
b. Another Thread invokes the notifyAll method on the Object.
c. Another Thread invokes the resume method on Thread T1.
d. Another Thread interrupts Thread T1.
e. The priority of Thread T1 is increased.
f. A specified timeout period has elapsed.
g. Another Thread invokes the join method on Thread T1.
h. None of the above.

Question 13
class A implements Runnable{public void run() {}}
class B {
public static void main(String[] args) {
Thread t1 = new Thread(); // 1
Thread t2 = new Thread(new A()); // 2
Thread t3 = new Thread("A", new A()); // 3
Thread t4 = new Thread("A"); // 4
}
}
A compile-time error is generated at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. None of the above.

Question 14
Which of the following will force a thread to move into the Not-Runnable state?
a. Thread.yield method.
b. Thread.sleep method.
c. Thread.join method.
d. Object.wait method.
e. By blocking on IO.
f. Unsuccessfully attempting to acquire a lock on an object.
g. None of the above.

Question 15
Which of the following are true statements?
Certified Java Programmer Mock Exam 109
a. The Thread.join method is static.
b. The Thread.join method is always invoked on an instance of a Thread.
c. The Thread.join method causes the current thread to wait for the referenced thread to die.
d. The Thread.join method throws an InterruptedException.
e. The Thread.join method accepts a timeout value as an argument.
f. The timeout value sets the minimum time that the current thread will wait for the death of the referenced thread.
g. Thread.join will return immediately if the timeout value is zero.
h. A timeout of zero will allow Thread.join to wait forever if necessary.
i. None of the above.

Question 16
Which of the following will cause a dead thread to restart?
a. Thread.yield method.
b. Thread.join method.
c. Thread.start method.
d. Thread.resume method.
e. None of the above.

Question 17
class A extends Thread {
public void run() {
System.out.print("A");
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
a.start(); // 1
}
}
What is the result of attempting to compile and run the program?
a. The program compiles and runs without error.
b. The second attempt to start thread t1 is successful.
c. The second attempt to start thread t1 is ignored.
d. Compiler error at marker 1.
e. An IllegalThreadStateException is thrown at run-time.
f. None of the above

Question 18
class A extends Thread {
private static B b = new B();
private String s1;
public void run() {
System.out.print(b.m1(s1));
}
A(String threadName, String s1) {
super(threadName);
this.s1 = s1;
}
public static void main (String[] args) {
A a = new A("T1","A");
A b = new A("T2","B");
a.start();
b.start();
}
}
class B {
private String s1;
public String m1(String s) {
s1 = s;
Thread.yield();
return "[" +
Thread.currentThread().getName() +
"," + s1 + "]";
}
}
What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: [T1,A][T2,B].
c. Prints: [T1,B][T2,B].
d. Prints: [T2,B][T1,A].
e. Prints: [T2,A][T1,A].
f. Compiler error.
g. Run time error.
h. None of the above.

Question 19

Certified Java Programmer Mock Exam 110


class B {}
class A implements Runnable {
public void run() {
B lock = new B();
synchronized (lock) {
System.out.print(Thread.currentThread().getName() + 1);
System.out.print(Thread.currentThread().getName() + 2);
System.out.print(Thread.currentThread().getName() + 3);
}
}
public static void main (String[] args) {
new Thread(new A(), "A").start();
new Thread(new A(), "B").start();
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: A1A2A3B1B2B3.
b. Prints: B1B2B3A1A2A3.
c. Prints: A1B1A2B2A3B3.
d. Prints: B1A1B2A2B3A3.
e. Compiler error.
f. Run time error.
g. None of the above.

Capítulo 9 – Threads (C)


Question 1
class A extends Thread {
private int i;
public void run() {
i = 1;
}
public static void main(String[] args) {
A a = new A();
a.start();
System.out.print(a.i);
}
}
What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: 0.
c. Prints: 1.
d. Prints: 01.
e. Prints: 10.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 2
Which of the following methods can throw an InterruptedException?
a. join
b. notifyAll
c. resume
d. run
e. sleep
f. start
g. stop
h. suspend
i. yield
j. wait
k. None of the above.

Question 3
A timeout can be specified for which of the following methods?
a. join
b. notifyAll
c. resume
d. run
e. sleep
f. start
g. stop
h. suspend
i. yield
j. wait
k. None of the above.

Question 4

Certified Java Programmer Mock Exam 111


class A extends Thread {
private int i;
public void run() {
i = 1;
}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}
}
What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: 0.
c. Prints: 1.
d. Prints: 01.
e. Prints: 10.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 5
Which of the following is a checked exception?
a. IllegalMonitorStateException
b. IllegalThreadStateException
c. IllegalArgumentException
d. InterruptedException
e. None of the above.

Question 6
class A extends Thread {
String[] sa;
public A(String[] sa) {
this.sa = sa;
}
public void run() {
System.out.print(sa[0] + sa[1] + sa[2]);
}
}
class B {
public static void main (String[] args) {
String[] sa = new String[]{"X","Y","Z"};
Thread t1 = new A(sa);
t1.start();
sa[0] = "A";
sa[1] = "B";
sa[2] = "C";
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 7
Which of the following instance methods should only be called by a thread that is the owner of the monitor for the instance on which the method
is invoked?
a. notify
b. notifyAll
c. resume
d. run
e. start
f. stop
g. suspend
h. wait
i. None of the above.

Question 8
class A extends Thread {
public void run() {
synchronized (this) {
try{wait();} catch (InterruptedException ie){}
}
}
public static void main(String[] args) {
Certified Java Programmer Mock Exam 112
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
}
}
What are the possible results of attempting to compile and run the program?
a. The number printed is greater than or equal to 0.
b. The synchronized block inside the run method is not necessary.
c. This program runs to completion after the elapsed time is printed.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 9
Which of the following occurs after invoking the interrupt method on a thread that is not blocking due to an earlier invocation of the join, sleep, or
wait methods?
a. The thread immediately throws an InterruptedException.
b. The thread's interrupt status is set.
c. The thread moves into the Not-Runnable state.
d. The thread moves out of the Not-Runnable state.
e. An InterruptedException is thrown when the thread moves into the running state.
f. None of the above.

Question 10
Which of the following thread state transitions are supported?
a. The dead state to the ready state.
b. The ready state to the Not-Runnable state.
c. The ready state to the running state.
d. The running state to the Not-Runnable state.
e. The running state to the ready state.
f. The Not-Runnable state to the ready state.
g. The Not-Runnable state to the running state.
h. None of the above.

Question 11
class A extends Thread {
public void run() {
synchronized (this) {
try{wait();} catch (InterruptedException ie){}
}
}
public static void main(String[] args) {
A a1 = new A();
a1.setDaemon(true);
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
}
}
What are the possible results of attempting to compile and run the program?
a. The number printed is greater than or equal to 0.
b. The synchronized block inside the run method is not necessary.
c. Thread a1 waits forever and the program runs forever.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 12
class C extends Thread {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}
}
System.out.print(sa[1] + sa[2] + sa[3]);
}
public static void main (String[] args) {
start();
synchronized (sa) {
sa[0] = "Done";
sa[1] = "A";
sa[2] = "B";
sa[3] = "C";
sa.notify();
}
}
Certified Java Programmer Mock Exam 113
}
Which of the following are possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compile-time error.
f. Run time error.
g. None of the above.

Question 13
class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}
What is the result of attempting to compile and run the program?
a. Prints: T1T1T1
b. Prints: T1T1T2
c. Prints: T1T2T2
d. Prints: T1T2T3
e. Prints: T1T1T3
f. Prints: T1T3T3
g. Compiler error
h. Run time error
i. None of the above

Question 14
Which of these statements are true?
a. Thread.interrupted is a static method.
b. Thread.isInterrupted is an instance method.
c. Thread.interrupted clears the interrupt status flag but Thread.isInterrupted does not.
d. The boolean value false will be returned if Thread.interrupted or Thread.isInterrupted is invoked after the InterruptedException has been
thrown.
e. None of the above.

Question 15
When a thread is created and started, what is its initial state?
a. new
b. ready
c. Not-Runnable
d. runnning
e. dead
f. None of the above.

Question 16
It is either not possible or not advisable to synchronize on which of the following?
a. primitives
b. A member variable that is an object reference.
c. A local variable that is an object reference.
d. None of the above.

Question 17
class A extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000;
while (System.currentTimeMillis() < endTime) {
yield();
}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
try {sleep(1000);} catch(InterruptedException ie) {}
a1.interrupt();
try {a1.join();} catch(InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
Certified Java Programmer Mock Exam 114
}
}
What are the possible results of attempting to compile and run the program?
a. Prints a number that is less than 1000.
b. Prints a number between 1000 and 9999.
c. The printed number must always be greater than 10000.
d. Compiler error.
e. Run time error.
f. None of the above.

Question 18
Which of the following occurs after invoking the interrupt method on a thread that is blocking due to a previous invocation of the join, sleep or
wait method?
a. The thread moves into the Not-Runnable state.
b. The thread moves out of the Not-Runnable state.
c. An InterruptedException is thrown when the thread moves into the running state.
d. Calling Thread.interrupted after the InterruptedException is thrown will return true.
e. None of the above.

Question 19
class A implements Runnable {
private boolean started;
public boolean started() {return started;}
Object obj;
public A(Object obj) {this.obj = obj;}
public void run() {
synchronized (obj) {
started=true;
obj.notify();
try {obj.wait();} catch(InterruptedException ie) {}
}
}
}
class B {
private void m1() {
A a = new A(this);
Thread t1 = new Thread(a);
t1.start();
synchronized (this) {
while (!a.started()) {
try {wait();} catch (InterruptedException ie) {}
}
}
t1.start(); // 1
synchronized (this) {
this.notify();
}
}
public static void main (String[] args) {
new B().m1();
}
}
Which of the following is always a result of attempting to compile and run the program?
a. The program compiles and runs without error.
b. The second attempt to start thread t1 is successful.
c. The second attempt to start thread t1 is ignored.
d. Compiler error at marker 1.
e. An IllegalThreadStateException is thrown at run-time.
f. None of the above

Capítulo 10 – The java.lang Package (A)


Question 1
class F {
static String m(long i) {return "long primitive";}
static String m(Long i) {return "Long Object";}
static String m(double i) {return "double primitive";}
static String m(Double i) {return "Double object";}
static String m(String i) {return "String object";}
public static void main (String[] args) {
System.out.print(m(Long.parseLong("1")));
}
}
What is the result of attempting to compile and run the program?
a. Prints: long primitive
b. Prints: Long Object
c. Prints: double primitive

Certified Java Programmer Mock Exam 115


d. Prints: Double Object
e. Prints: String object
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 2
class J {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0;
short s = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(s,s))+",");
System.out.print(m(Math.min(b,1)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: byte,byte,byte
b. Prints: short,short,short
c. Prints: int,int,int
d. Prints: byte,short,int
e. Prints: short,short,int
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 3
Which of the following methods of the java.lang.Double class declare a number format exception in the throws clause?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 4
Which of the following statements will generate a compile-time error?
a. Short s1 = new Short("1");
b. Short s1 = new Short("-1");
c. Short s1 = new Short("1.0");
d. Short s1 = new Short("0x1");
e. Short s1 = new Short("011");
f. None of the above.

Question 5
class F {
static String m(long i) {return "long primitive";}
static String m(Long i) {return "Long Object";}
static String m(double i) {return "double primitive";}
static String m(Double i) {return "Double object";}
static String m(String i) {return "String object";}
public static void main (String[] args) {
System.out.print(m(Long.parseLong("1L")));
}
}
What is the result of attempting to compile and run the program?
a. Prints: long primitive
b. Prints: Long Object
c. Prints: double primitive
d. Prints: Double Object
e. Prints: String object
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 6
class D {
public static void main (String args[]) {
Byte b1 = new Byte("0x10"); // 1
byte b2 = Byte.parseByte("0x10"); // 2
Byte b3 = Byte.valueOf("0x10"); // 3
System.out.print(b1+","+b2+","+b3);
Certified Java Programmer Mock Exam 116
}
}
What is the result of attempting to compile and run the program?
a. Prints: 10,10,10
b. Prints: 16,16,16
c. Compile-time error at line 1.
d. Compile-time error at line 2.
e. Compile-time error at line 3.
f. Run-time Error
g. None of the Above

Question 7
class A {
public static void main (String args[]) {
int primitiveInt = 1;
long primitiveLong = 1L;
float primitiveFloat = 1.0f;
String s = "1";
Integer i1 = new Integer(primitiveInt);
Integer i2 = new Integer(primitiveLong);
Integer i3 = new Integer(primitiveFloat);
Integer i4 = new Integer(s);
int i5 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue();

System.out.print(i5);
}
}
}
What is the result of attempting to compile and run the program?
a. Prints: 4
b. Prints: 4.0
c. Compile-time Error
d. Run-time Error
e. None of the Above

Question 8
class E {
public static void main (String[] args) {
Byte b1 = new Byte("1");
Byte b2 = new Byte("1");
int a = b1.hashCode();
int b = b2.hashCode();
System.out.print((b1.equals(b2))+","+(a==b));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 9
class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.MAX_VALUE;
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}
}
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. Compiler Error
j. Runtime Error
k. None of the Above
Certified Java Programmer Mock Exam 117
Question 10
class A {
public static void main(String[] args) {
Boolean b1 = new Boolean(true); // 1
Boolean b2 = new Boolean(false); // 2
Boolean b3 = new Boolean(TRUE); // 3
Boolean b4 = new Boolean(FALSE); // 4
Boolean b5 = new Boolean("TrUe"); // 5
Boolean b6 = new Boolean("fAlSe"); // 6
}
}
A compile-time error is generated at which lines?
a. 1.
b. 2.
c. 3.
d. 4.
e. 5.
f. 6.
g. None of the above.

Question 11
class B {
public static void main(String[] args) {
String s1 = "ABCDE";
System.out.print(s1.indexOf("B")+",");
System.out.print(s1.indexOf("F")+",");
System.out.print(s1.indexOf(""));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 1,0,0
b. Prints: 1,0,-1
c. Prints: 1,-1,0
d. Prints: 1,-1,-1
e. Prints: 2,0,0
f. Prints: 2,0,-1
g. Prints: 2,-1,0
h. Prints: 2,1,-1
i. Compiler error
j. Run time error
k. None of the Above

Question 12
class L {
public static void main (String[] args) {
System.out.print((Math.random() >= 0.0) + ",");
System.out.print(Math.random() < 1.0);
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 13
class A {
public static void main (String[] args) {
Float f1 = new Float("1.0"); // 1
Float f2 = new Float("1.0f"); // 2
Float f3 = new Float("Infinity"); // 3
Float f4 = new Float("-Infinity"); // 4
Float f5 = new Float("NaN"); // 5
System.out.print(f1+","+f2+","+f3+","+f4+","+f5);
}
}
What is the result of attempting to compile and run the program?
a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. Compile-time error at 4.
e. Compile-time error at 5.
f. Run-time error at 1.
g. Run-time error at 2.
h. Run-time error at 3.
Certified Java Programmer Mock Exam 118
i. Run-time error at 4.
j. Run-time error at 5.
k. None of the above.

Question 14
class G {
public static void main (String[] args) {
// Insert statement here.
}
}
Which of the following statements will result in either a compile-time error or a run-time error if inserted in the program?
a. Float f1 = new Float('A');
b. Float f2 = new Float("A");
c. Float f3 = new Float(1L);
d. Float f4 = new Float("1L");
e. Float f5 = new Float(0x10);
f. Float f6 = new Float("0x10");
g. None of the above.

Question 15
class B {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Boolean b3 = new Boolean("TrUe");
Boolean b4 = new Boolean("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. None of the above.

Question 16
class A {
public static void main(String s[]){
System.out.print((Integer.MIN_VALUE < 0) + ",");
System.out.print((Float.MIN_VALUE < 0) + ",");
System.out.print(Float.MIN_VALUE < Integer.MIN_VALUE);
}
}
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 17
class J {
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer(32);
System.out.print(s1.length() + "," + s1.capacity());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0
b. Prints: 0,32
c. Prints: 32,0
d. Prints: 32,32
e. Compiler Error
f. Run time Error
g. None of the Above

Certified Java Programmer Mock Exam 119


Question 18
class Black {
public static void main(String args[]) {
String a = "A";
String b = "B";
String c = a+b;
String d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}
}
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

Question 19
class R{
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer("ABCDEFG");
s1.setCharAt(7,'H');
System.out.print(s1 + "," + s1.length());
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABCDEFGH
b. Prints: ABCDEFH
c. Compiler Error
d. Run time Error
e. None of the Above

Capítulo 10 – The java.lang Package (B)


Question 1
class E {
public static void main (String[] args) {
System.out.print(new Integer("11", 2)+", ");
System.out.print(new Integer("11", 8)+", ");
System.out.print(new Integer("11", 10)+", ");
System.out.print(new Integer("11", 16));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16
b. Prints: 3,9,11,17
c. Prints: 11,11,11,11
d. Prints: 13,19,21,27
e. Compile-time Error
f. Run-time Errorr
g. None of the Above

Question 2
Some of these methods of the java.lang.Math class return only one of the following types :int, long, float, or double. Some are overloaded so
alternate versions of the method can return more than one of the previously named types. Which of the following methods are able to return all
four of the previously named primitive types?
a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the Above

Question 3
class D {
public static void main (String args[]) {

Certified Java Programmer Mock Exam 120


Byte b1 = Byte.valueOf("10",Character.MIN_RADIX);
Byte b2 = Byte.valueOf("10",8);
Byte b3 = Byte.valueOf("10",10);
Byte b4 = Byte.valueOf("10",16);
Byte b5 = Byte.valueOf("10",Character.MAX_RADIX);
System.out.print(b1+","+b2+","+b3+","+b4+","+b5);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16,36
b. Compiler Error
c. Run time Error
d. None of the Above

Question 4
class B {
public static void main (String args[]) {
Long i1 = new Long(1);
Long i2 = new Long(i1);
System.out.print(i1.longValue() + i2.longValue());
}
}
What is the result of attempting to compile and run the program?
a. Prints: false
b. Prints: true
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 5
class F {
public static void main (String args[]) {
Float f1 = new Float("Infinity"); // 1
Float f2 = new Float("NaN"); // 2
Double d1 = new Double("Infinity"); // 3
Double d2 = new Double("NaN"); // 4
System.out.print(f1+","+f2+","+d1+","+d2); // 5
}
}
What is the result of attempting to compile and run the program?
a. Prints: Infinity,NaN,Infinity,NaN.
b. Compile-time error at line 1.
c. Compile-time error at line 2.
d. Compile-time error at line 3.
e. Compile-time error at line 4.
f. Run-time error at line 1.
g. Run-time error at line 2.
h. Run-time error at line 3.
i. Run-time error at line 4.
j. None of the Above

Question 6
Which of the following statements will generate a compile-time error?
a. Short s1 = new Short(1);
b. Short s1 = new Short('1');
c. Short s1 = new Short(1L);
d. Short s1 = new Short('b' - 'a');
e. Short s1 = new Short(1.0);
f. Short s1 = new Short((short)1 - (short)2);
g. Short s1 = new Short((byte)1);
h. Short s1 = new Short((short)1);
i. None of the above.

Question 7
class A {
public static void main (String args[]) {
byte primitiveByte = 1; // 1
Byte b1 = new Byte(primitiveByte); // 2
Byte b2 = new Byte(1); // 3
System.out.print(b1.byteValue() + b2.byteValue());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2
b. Prints: 11
c. Compiler Error at 1
d. Compiler Error at 2
e. Compiler Error at 3
f. Runtime Error
Certified Java Programmer Mock Exam 121
g. None of the Above

Question 8
class G {
public static void main (String[] args) {
System.out.print(Long.parseLong("11", 2)+", ");
System.out.print(Long.parseLong("11", 8)+", ");
System.out.print(Long.parseLong("11", 10)+", ");
System.out.print(Long.parseLong("11", 16));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16
b. Prints: 3,9,11,17
c. Prints: 11,11,11,11
d. Prints: 13,19,21,27
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 9
class A {
public static void main(String[] s) {
String s1 = "A";
String s2 = " B ";
String s3 = "C";
s2.trim();
s3.append("D");
System.out.print(s1 + s2 + s3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compiler error
h. Run time error
i. None of the Above

Question 10
class E {
public static void main (String args[]) {
String s1 = Float.toString(1.0); // 1
String s2 = Float.toString(1.0f); // 2
String s3 = Float.toString(0xf); // 3
String s4 = Float.toString(010); // 4
String s5 = Float.toString('A'); // 5
}
}
What is the result of attempting to compile and run the program?
a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. Compile-time error at 4.
e. Compile-time error at 5.
f. Compile-time error at 6.
g. Run-time error at 1.
h. Run-time error at 2.
i. Run-time error at 3.
j. Run-time error at 4.
k. Run-time error at 5.
l. Run-time error at 6.
m. None of the above.

Question 11
class D {
public static void main (String[] args) {
Boolean b1 = Boolean.valueOf("trUE"); // 1
Boolean b2 = Boolean.valueOf("Very True"); // 2
Boolean b3 = Boolean.valueOf(null); // 3
System.out.print((b1==b2) + ",");
System.out.print((b2==b3) + ",");
System.out.println(b3==b1);
}
}
What is the result of attempting to compile and run the program?
Certified Java Programmer Mock Exam 122
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 12
Which of the following statements are true in terms of the java.lang.Math.floor method?
a. Four overloaded versions of floor exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of an integral primitive type.
e. Returns the largest whole number that is less than or equal to the argument value.
f. Returns the smallest whole number that is greater than or equal to the argument value.
g. None of the Above

Question 13
Which of the following methods of the java.lang.Double class return a primitive value?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 14
class D {
public static void main (String[] args) {
Boolean b1 = new Boolean("trUE"); // 1
Boolean b2 = new Boolean("What's This?"); // 2
Boolean b3 = new Boolean(null); // 3
System.out.print(b1 + "," + b2 + "," + b3);
}
}
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 15
class C {
public static void main (String[] args) {
Float f1 = new Float(1.0); // 1
Float f2 = new Float("1.0"); // 2
Float f3 = new Float("1.0f"); // 3
Float f4 = new Float("1e1f"); // 4
Float f5 = new Float(".1e1d"); // 5
}
}
What is the result of attempting to compile and run the program?
a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. Compile-time error at 4.
e. Compile-time error at 5.
f. Run-time error at 1.
g. Run-time error at 2.
h. Run-time error at 3.
i. Run-time error at 4.
j. Run-time error at 5.
k. None of the above.

Question 16
Certified Java Programmer Mock Exam 123
class B {
public static void main (String args[]) {
Integer a = new Integer(0xFFFF);
byte b = a.byteValue();
short c = a.shortValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+","+c+","+ (e+f+g+h == 4 * 0xFFFF));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0xFF,0xFFFF,false
b. Prints: 0xFF,0xFFFF,true
c. Prints: 0xFFFF,0xFFFF,false
d. Prints: 0xFFFF,0xFFFF,true
e. Prints: -1,-1,false
f. Prints: -1,-1,true
g. Compile-time Error
h. Run-time Error
i. None of the Above

Question 17
class E {
public static void main (String[] args) {
String s1 = "ABCDE";
System.out.print(s1.substring(1,2)+s1.substring(3));
}
}
What is the result of attempting to compile and run the program?
a. Prints: AABC
b. Prints: ACDE
c. Prints: ABABC
d. Prints: ABCDE
e. Prints: BABCD
f. Prints: BDE
g. Prints: BCABCD
h. Prints: BCDE
i. Compiler Error
j. Runtime Error
k. None of the Above

Question 18
class H {
public static void main (String[] args) {
String s1 = "ABC";
StringBuffer s2 = new StringBuffer(s1);
System.out.print(s2.equals(s1) + "," + s1.equals(s2));
}
}
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. Compiler Error
f. Run time Error
g. None of the Above

Question 19
class K {
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer("ABCDEFG");
System.out.print(s1.capacity());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 7
b. Prints: 8
c. Prints: 16
d. Prints: 32
e. Compiler Error
f. Run time Error
g. None of the Above

Certified Java Programmer Mock Exam 124


Capítulo 10 – The java.lang Package (C)
Question 1
1. class A {
2. public static void main(String[] args) {
3. byte[] b = {'a', 'b', 'c'};
4. char[] c = {'a', 'b', 'c'};
5. String s = "abc";
6. StringBuffer sb = new StringBuffer("abc");
7. byte b2 = 'a';
8. char c2 = 'a';
9. String s1 = new String(b);
10. String s2 = new String(c);
11. String s3 = new String(s);
12. String s4 = new String(sb);
13. String s5 = new String(b2);
14. String s6 = new String(c2);
15. }
16. }
What is the result of attempting to compile the program?
a. Compiler error at line 9.
b. Compiler error at line 10.
c. Compiler error at line 11.
d. Compiler error at line 12.
e. Compiler error at line 13.
f. Compiler error at line 14.
g. None of the Above

Question 2
class F {
public static void main (String[] args) {
Float f1 = new Float('B' - 'A'); // 1
Float f2 = new Float(010); // 2
Float f3 = new Float(0x10); // 3
Float f4 = new Float(.1e1); // 4
Float f5 = new Float(1.0); // 5
Float f6 = new Float(1.0d); // 6
System.out.print(f1+","+f2+","+f3+","+f4+","+f5+","+f6);
}
}
What is the result of attempting to compile and run the program?
a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. Compile-time error at 4.
e. Compile-time error at 5.
f. Compile-time error at 6.
g. Run-time error at 1.
h. Run-time error at 2.
i. Run-time error at 3.
j. Run-time error at 4.
k. Run-time error at 5.
l. Run-time error at 6.
m. Prints: 1.0,10.0,10.0,1.0,1.0,1.0
n. Prints: 1.0,8.0,16.0,1.0,1.0,1.0
o. None of the above.

Question 3
class F {
public static void main (String args[]) {
Double b1 = Double.valueOf("10",Character.MIN_RADIX);
Double b2 = Double.valueOf("10",8);
Double b3 = Double.valueOf("10",10);
Double b4 = Double.valueOf("10",16);
Double b5 = Double.valueOf("10",Character.MAX_RADIX);
System.out.print(b1+","+b2+","+b3+","+b4+","+b5);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16,36
b. Prints: 2.0,8.0,10.0,16.0,36.0
c. Compile-time error.
d. Run-time error.
e. None of the above.

Question 4
class E {
static String m1(boolean b) {return "Primitive";}

Certified Java Programmer Mock Exam 125


static String m1(Boolean b) {return "Wrapper";}
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
System.out.print(m1(Boolean.valueOf(null)) + ",");
System.out.print(m1(b1.booleanValue()) + ",");
System.out.println(m1(Boolean.TRUE));
}
}
What is the result of attempting to compile and run the program?
a. Prints: Primitive,Primitive,Primitive
b. Prints: Primitive,Primitive,Wrapper
c. Prints: Primitive,Wrapper,Primitive
d. Prints: Primitive,Wrapper,Wrapper
e. Prints: Wrapper,Primitive,Primitive
f. Prints: Wrapper,Primitive,Wrapper
g. Prints: Wrapper,Wrapper,Primitive
h. Prints: Wrapper,Wrapper,Wrapper
i. Compile-time error.
j. Run-time error.
k. None of the above.

Question 5
1. class B {
2. public static void main (String args[]) {
3. Byte b1 = new Byte(1);
4. Byte b2 = new Byte('2');
5. Byte b3 = new Byte("3");
6. byte i1 = b1.byteValue() + b2.byteValue() + b3.byteValue();
7. System.out.print(i1);
8. }
9. }
What is the result of attempting to compile and run the program?
a. Prints: 6
b. Compiler Error at 3
c. Compiler Error at 4
d. Compiler Error at 5
e. Compiler Error at 6
f. Runtime Error
g. None of the Above

Question 6
class D {
public static void main (String args[]) {
Byte a = new Byte("1");
byte b = a.byteValue();
short c = a.shortValue();
char d = a.charValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+c+d+e+f+g+h);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 7
b. Prints: 7.0
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 7
Which of the following methods are static members of the java.lang.Double class?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 8
class A {
public static void main(String s[]){
System.out.print((Integer.MIN_VALUE < 0) + ",");
System.out.print((Double.MIN_VALUE < 0) + ",");
System.out.print(Double.MIN_VALUE < Integer.MIN_VALUE);
}
Certified Java Programmer Mock Exam 126
}
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 9
class J {
public static void main (String[] args) {
int a = Math.min(-0,+0);
long b = Math.min(-0L,+0L);
float c = Math.min(-0.0f,+0.0f);
double d = Math.min(-0.0,+0.0);
System.out.print(a+","+b+","+c+","+d);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0.0,0.0
b. Prints: -0,-0,-0.0,-0.0
c. Prints: 0,0,-0.0,-0.0
d. Compiler Error
e. Runtime Error
f. None of the Above

Question 10
class A {
public static void main (String args[]) {
Integer i1 = new Integer(1);
Integer i2 = new Integer(i1);
System.out.print(i1.equals(i2));
}
}
What is the result of attempting to compile and run the program?
a. Prints: false
b. Prints: true
c. Compile-time Error
d. Run-time Error
e. None of the Above

Question 11
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 12
class C {
public static void main (String args[]) {
Long a = new Long(1);
byte b = a.byteValue();
short c = a.shortValue();
int d = a.intValue();
Certified Java Programmer Mock Exam 127
long e = a.longValue();
float f = a.floatValue();
double g = a.doubleValue();
System.out.print(b+c+d+e+f+g);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 6
b. Prints: 6L
c. Prints: 6.0
d. Compiler Error
e. Runtime Error
f. None of the Above

Question 13
class H {
public static void main (String args[]) {
System.out.print(Long.toHexString(Long.MIN_VALUE));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 8000000000000000
b. Prints: 7FFFFFFFFFFFFFFF
c. Prints: FFFFFFFFFFFFFFFF
d. Prints: 80000000
e. Prints: 7FFFFFFF
f. Prints: FFFFFFFF
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 14
Which of the following statements are true in terms of the java.lang.Math.ceil method?
a. Four overloaded versions of ceil exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of an integral primitive type.
e. Returns the largest whole number that is less than or equal to the argument value.
f. Returns the smallest whole number that is greater than or equal to the argument value.
g. None of the Above

Question 15
class F {
public static void main (String[] args) {
System.out.print(String.valueOf(1) + String.valueOf(2));
String s1 = "S1";
String s2 = s1.toString();
System.out.print("," + (s1==s2));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 3,false
b. Prints: 3,true
c. Prints: 12,false
d. Prints: 12,true
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 16
class Q{
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer();
s1.append(new Boolean(true));
s1.insert(1, new Boolean(false));
System.out.print(s1);
}
}
What is the result of attempting to compile and run the program?
a. Prints: falsefalse
b. Prints: falsetrue
c. Prints: truefalse
d. Prints: truetrue
e. Compiler Error
f. Run time Error
g. None of the Above

Question 17
class P{
Certified Java Programmer Mock Exam 128
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer(null);
System.out.print(s1);
}
}
What is the result of attempting to compile and run the program?
a. Prints nothing
b. Prints: null
c. Compiler Error
d. Run time Error
e. None of the Above

Question 18
class Blue {
public static void main(String args[]) {
String a = "A";
String b = "B";
final String c = a+b;
final String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}
}
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. Compiler Error
j. Runtime Error
k. None of the Above

Capítulo 10 – The java.lang Package (D)


Question 1
class C {
public static void main (String args[]) {
String s1 = "15";
String s2 = "0xf";
long i1 = Long.parseLong(s1);
long i2 = Long.parseLong(s2);
System.out.print(i1==i2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: true
b. Prints: false
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 2
class F {
public static void main (String[] args) {
System.out.print(Byte.MIN_VALUE+","+Byte.MAX_VALUE);
}
}
What is the result of attempting to compile and run the program?
a. Prints: -128,127
b. Prints: -127,128
c. Prints: 0,255
d. Compiler Error
e. Runtime Error
f. None of the Above

Question 3
class A {
public static void main (String[] args) {
String s = "11";
int i1 = Integer.parseInt(s);
System.out.print(new Integer(i1).equals(new Integer(i1)) + ",");
System.out.print(new Integer(i1).equals(new Integer(s)) + ",");

Certified Java Programmer Mock Exam 129


System.out.print(new Integer(i1) == new Integer(i1));
}
}
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
class E {
public static void main (String[] args) {
Byte b1 = new Byte("1");
Byte b2 = new Byte("1");
System.out.print((b1==b2)+","+(b1.equals(b2)));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 5
class D {
public static void main (String args[]) {
byte b1 = Byte.valueOf("10",Character.MIN_RADIX);
byte b2 = Byte.valueOf("10",8);
byte b3 = Byte.valueOf("10",10);
byte b4 = Byte.valueOf("10",16);
byte b5 = Byte.valueOf("10",Character.MAX_RADIX);
System.out.print(b1+","+b2+","+b3+","+b4+","+b5);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16,36
b. Compiler Error
c. Run time Error
d. None of the Above

Question 6
class M {
public static void main(String s[]){
System.out.print((Long.MIN_VALUE < 0) + ",");
System.out.print((Double.MIN_VALUE < 0) + ",");
System.out.print(Double.MIN_VALUE < Long.MIN_VALUE);
}
}
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. Compiler Error
j. Runtime Error
k. None of the Above

Question 7
class B {
public static void main(String[] args) {
String s1 = "ABCDE";
System.out.print(s1.indexOf("F")+",");
System.out.print(s1.indexOf(""));
System.out.print(s1.indexOf(''));
}
Certified Java Programmer Mock Exam 130
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0
b. Prints: 0,0,-1
c. Prints: 0,-1,0
d. Prints: 0,-1,-1
e. Prints: -1,0,0
f. Prints: -1,0,-1
g. Prints: -1,-1,0
h. Prints: -1,1,-1
i. Compiler error
j. Run time error
k. None of the Above

Question 8
class B {
public static void main (String[] args) {
byte b1 = 11;
System.out.print(new Integer(b1).equals(new Integer(b1)) + ",");
System.out.print(new Integer(b1).equals(new Short(b1)) + ",");
System.out.print(new Integer(b1).equals(new Byte(b1)));
}
}
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 9
class D {
static String m(int i) {return "int primitive";}
static String m(double i) {return "double primitive";}
static String m(Double i) {return "Double object";}
static String m(String i) {return "String object";}
public static void main (String[] args) {
System.out.print(m(Double.parseDouble("1")));
}
}
What is the result of attempting to compile and run the program?
a. Prints: int primitive
b. Prints: double primitive
c. Prints: Double object
d. Prints: String object
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 10
class J {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
long seed = 1L;
System.out.print(m(Math.random(seed)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: int
b. Prints: long
c. Prints: float
d. Prints: double
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 11
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
Certified Java Programmer Mock Exam 131
char primitiveChar = 1;
double primitiveDouble = 1;
String s = "1";
Double d1 = new Double(primitiveByte);
Double d2 = new Double(primitiveChar);
Double d3 = new Double(primitiveDouble);
Double d4 = new Double(s);
double d5 = d1.doubleValue() + d2.doubleValue() +
d3.doubleValue() + d4.doubleValue();

System.out.print(d5);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 4
b. Prints: 4.0
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 12
class K {
public static void main (String[] args) {
System.out.print(Math.round(-3.6)+ ",");
System.out.print(Math.round(-3.4)+ ",");
System.out.print(Math.round(3.4)+ ",");
System.out.print(Math.round(3.6));
}
}
What is the result of attempting to compile and run the program?
a. Prints: -3.0,-4.0,3.0,4.0
b. Prints: -3.0,-4.0,4.0,3.0
c. Prints: -4.0,-3.0,3.0,4.0
d. Prints: -4.0,-3.0,4.0,3.0
e. Prints: -4.0,-4.0,3.0,3.0
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 13
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
char primitiveChar = 'b'-'a';
int primitiveInt = 1;
short primitiveShort = 1;
String s = "1";
Integer i1 = new Integer(primitiveByte);
Integer i2 = new Integer(primitiveChar);
Integer i3 = new Integer(primitiveShort);
Integer i4 = new Integer(primitiveInt);
Integer i5 = new Integer(s);
int i6 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();
System.out.print(i6);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 5
b. Prints: 5.0
c. Compile-time Error
d. Run-time Error
e. None of the Above

Question 14
class C {
public static void main (String[] args) {
System.out.print(Math.round(Float.NaN));
}
}
What is the result of attempting to compile and run the program?
a. Prints: NaN
b. Prints: 0.0
c. Prints: 0
d. Compiler Error
e. Runtime Error
f. None of the Above

Certified Java Programmer Mock Exam 132


Question 15
class L{
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer();
s1.ensureCapacity(16);
s1.append("ABCDEFG");
s1.setLength(5);
System.out.print(s1 + "," + s1.capacity());
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABCDEFG,16
b. Prints: ABCDEFG,5
c. Prints: ABCDE,16
d. Prints: ABCDE,5
e. Prints: 32
f. Compiler Error
g. Run time Error
h. None of the Above

Question 16
class B {
public static void main(String[] args) {
String s1 = " B C D E ";
System.out.print('A' + s1.trim() + 'F');
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABCDEF
b. Prints: A B C D E F
c. Prints: A BCDEF
d. Prints: ABCDE F
e. Prints: AB C D EF
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 17
class I {
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer();
System.out.print(s1.length() + "," + s1.capacity());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0
b. Prints: 0,8
c. Prints: 8,0
d. Prints: 8,8
e. Prints: 0,16
f. Prints: 16,0
g. Prints: 16,16
h. Compiler Error
i. Run time Error
j. None of the Above

Question 18
class N{
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer();
System.out.print(s1.append(null));
}
}
What is the result of attempting to compile and run the program?
a. Prints nothing
b. Prints: null
c. Compiler Error
d. Run time Error
e. None of the Above

Capítulo 10 – The java.lang Package (F)


Question 1
Which of the following methods of the java.lang.Integer class returns a primitive int type?
a. intValue
b. parseInt
c. valueOf

Certified Java Programmer Mock Exam 133


d. None of the Above

Question 2
class B {
public static void main (String args[]) {
Double a = new Double(0xFFFF);
byte b = a.byteValue();
short c = a.shortValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+","+c+","+ (e+f+g+h == 4 * 0xFFFF));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0xFFFF,0xFFFF,false
b. Prints: 0xFFFF,0xFFFF,true
c. Prints: -1,-1,false
d. Prints: -1,-1,true
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 3
class C {
public static void main (String[] args) {
int i1 = Math.round(0.5f);
int i2 = Math.round(1.5f);
System.out.print(i1 + "," + i2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,1
b. Prints: 0,2
c. Prints: 1,1
d. Prints: 1,2
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 4
class Orange {
public static void main(String args[]) {
String a = "A";
String b = "B";
String c = "AB";
System.out.print((("A"+"B")=="AB") + ",");
System.out.print(("A"+"B")==c);
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 5
class K {
public static void main (String[] args) {
System.out.print(Math.round(-3.6)+ ",");
System.out.print(Math.round(-3.4)+ ",");
System.out.print(Math.round(3.4)+ ",");
System.out.print(Math.round(3.6));
}
}
What is the result of attempting to compile and run the program?
a. Prints: -3.0,-4.0,3.0,4.0
b. Prints: -3.0,-4.0,4.0,3.0
c. Prints: -4.0,-3.0,3.0,4.0
d. Prints: -4.0,-3.0,4.0,3.0
e. Prints: -4.0,-4.0,3.0,3.0
f. Compiler Error
g. Runtime Error
h. None of the Above

Certified Java Programmer Mock Exam 134


Question 6
Which of the following methods of the java.lang.Math class are static?
a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the Above

Question 7
class D {
public static void main (String args[]) {
int[] literals = {010, 0xf};
String[] strings = {"010", "0xf"};
for (int i = 0; i < literals.length; i++) {
try {
int i1 = new Integer(literals[i]).intValue();
int i2 = Integer.parseInt(strings[i]);
System.out.print(i1==i2);
} catch (NumberFormatException ie) {
System.out.print("NFE");
} finally {
if (i != literals.length - 1) {System.out.print(",");}
}
}
}
}
What is the result of attempting to compile and run the program?
a. Prints: NFE,NFE
b. Prints: NFE,false
c. Prints: NFE,true
d. Prints: false,NFE
e. Prints: false,false
f. Prints: false,true
g. Prints: true,NFE
h. Prints: true,false
i. Prints: true,true
j. Compile-time Error
k. None of the Above

Question 8
class E {
public static void main (String[] args) {
System.out.print(Integer.parseInt("11", 2)+", ");
System.out.print(Integer.parseInt("11", 8)+", ");
System.out.print(Integer.parseInt("11", 10)+", ");
System.out.print(Integer.parseInt("11", 16));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 2,8,10,16
b. Prints: 3,9,11,17
c. Prints: 11,11,11,11
d. Prints: 13,19,21,27
e. Compile-time Error
f. Run-time Errorr
g. None of the Above

Question 9
class J {
public static void main (String[] args) {
double d1 = -0.0;
double d2 = +0.0;
System.out.print(Math.min(d1,d2)+","+Math.max(d1,d2));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0.0,0.0
b. Prints: 0.0,+0.0
c. Prints: -0.0,0.0
d. Prints: -0.0,+0.0
e. Compiler Error
f. Runtime Error
Certified Java Programmer Mock Exam 135
g. None of the Above

Question 10
class A {
public static void main (String args[]) {
Double d1 = new Double(1.0);
Double d2 = new Double(d1);
System.out.print(d1.equals(d2));
}
}
What is the result of attempting to compile and run the program?
a. Prints: false
b. Prints: true
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 11
Which of the following methods of the java.lang.Math class have a return type of a primitive integral type but an argument of a floating-point
primitive type?
a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the Above

Question 12
Which of these methods can be invoked on a java.lang.String object to modify the contents of that String object?
a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. trim
h. valueOf
i. None of the above.

Question 13
class C {
public static void main (String args[]) {
String s1 = "011";
String s2 = "0xf";
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
System.out.print(i1 + i2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 24
b. Prints: 25
c. Prints: 26
d. Compile-time Error
e. Run-time Error
f. None of the Above

Question 14
Which of the following methods are members of the java.lang.Double class?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. getDouble
g. toString(double)
h. toHexString
i. valueOf
j. None of the Above

Question 15
class F {
public static void main (String args[]) {
Certified Java Programmer Mock Exam 136
Double d1 = new Double("0x10"); // 1
double d2 = Double.parseDouble("0x10"); // 2
Double d3 = Double.valueOf("0x10"); // 3
System.out.print(d1+","+d2+","+d3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 10,10,10
b. Prints: 16,16,16
c. Compile-time error at line 1.
d. Compile-time error at line 2.
e. Compile-time error at line 3.
f. Run-time Error
g. None of the Above

Question 16
class A {
static void m1(String s) {
s = s.trim();
s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A";
String s2 = " B ";
String s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compiler error
h. Run time error
i. None of the Above

Question 17
class Red {
public static void main(String args[]) {
String a = "A";
String b = "B";
System.out.print((("A"+"B")=="AB") + ",");
System.out.print(("A"+"B")==(a+b));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 18
class Green {
public static void main(String args[]) {
final String a = "A";
final String b = "B";
String c = a+b;
String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}
}
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
Certified Java Programmer Mock Exam 137
i. Compiler Error
j. Runtime Error
k. None of the Above

Capítulo 10 – The java.lang Package (E)


Question 1
Which of the following statements are true in terms of the java.lang.Math.random method?
a. The random method is overloaded.
b. The argument type is a double that represents a seed value.
c. Throws an ArithmeticException if the seed value is negative.
d. The returned value is always greater than zero and less than or equal to one.
e. None of the Above

Question 2
class C {
public static void main (String args[]) {
String s1 = "1";
String s2 = "2";
Byte b1 = Byte.parseByte(s1);
Byte b2 = Byte.parseByte(s2);
System.out.print(b1.byteValue() + b2.byteValue());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 3
b. Prints: 12
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 3
class D {
public static void main (String args[]) {
Byte b1 = new Byte("10", 8); // 1
byte b2 = Byte.parseByte("10",8); // 2
Byte b3 = Byte.valueOf("10",8); // 3
System.out.print(b1+","+b2+","+b3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 8,8,8
b. Prints: 10,10,10
c. Compile-time error at line 1.
d. Compile-time error at line 2.
e. Compile-time error at line 3.
f. Run time Error
g. None of the Above

Question 4
class E {
static String m(int i) {return "int primitive";}
static String m(Integer i) {return "Integer Object";}
static String m(double i) {return "double primitive";}
static String m(Double i) {return "Double object";}
static String m(String i) {return "String object";}
public static void main (String[] args) {
System.out.print(m(Integer.parseInt("1")));
}
}
What is the result of attempting to compile and run the program?
a. Prints: int primitive
b. Prints: double primitive
c. Prints: Double object
d. Prints: String object
e. Compile-time Error
f. Run-time Error
g. None of the Above

Question 5
Which of the following statements are true in terms of the java.lang.Math.abs method?
a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of a floating-point primitive type.
e. If the argument is greater than or equal to zero then the returned value is equal to the argument.
f. If the argument, arg, is less than zero then the returned value is -arg.

Certified Java Programmer Mock Exam 138


g. None of the Above

Question 6
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
int primitiveInt = 1;
long primitiveLong = 1L;
float primitiveFloat = 1f;
String s = "1";
Long i1 = new Long(primitiveByte);
Long i2 = new Long(primitiveInt);
Long i3 = new Long(primitiveLong);
Long i4 = new Long(primitiveFloat);
Long i5 = new Long(s);
int i6 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();

System.out.print(i6);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 5
b. Prints: 5.0
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 7
Which of the following methods of the java.lang.Double class declares a parameter of type String?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 8
class D {
public static void main (String args[]) {
boolean b1 = Integer.MIN_VALUE == 0x80000000;
boolean b2 = Integer.MAX_VALUE == 0x7FFFFFFF;
System.out.print(b1 + "," + b2);
}
}
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. Compile-time Error
f. Run-time Error
g. None of the Above

Question 9
Which of the following methods of the java.lang.Double class return an object instance of type Double?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 10
Which of these methods are static member of the java.lang.String class?
a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. trim
h. valueOf
i. None of the above.
Certified Java Programmer Mock Exam 139
Question 11
Which of the following methods are implementations of abstract methods declared in the java.lang.Number class?
a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the Above

Question 12
class K {
public static void main (String[] args) {
double radians = Math.PI/2;
double degrees = 90;
System.out.print((Math.sin(radians)==1.0)+",");
System.out.print((Math.sin(degrees)==1.0));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 13
class A {
public static void main(String[] s) {
String s1 = "A";
String s2 = " B ";
String s3 = "C";
s2.trim();
s3.concat("D");
System.out.print(s1 + s2 + s3);
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compiler error
h. Run time error
i. None of the Above

Question 14
class C {
public static void main (String args[]) {
String s1 = "#f";
String s2 = "0xf";
byte b1 = Byte.parseByte(s1);
byte b2 = Byte.parseByte(s2);
System.out.print(b1 + b2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0x1d
b. Prints: 0x1f
c. Prints: 0xff
d. Prints: 30
e. Prints: 32
f. Compiler Error
g. Run time Error
h. None of the Above

Question 15
class H {
public static void main (String args[]) {
System.out.print(Long.toHexString(Long.MAX_VALUE));
}
}
What is the result of attempting to compile and run the program?
Certified Java Programmer Mock Exam 140
a. Prints: 8000000000000000
b. Prints: 7FFFFFFFFFFFFFFF
c. Prints: FFFFFFFFFFFFFFFF
d. Prints: 80000000
e. Prints: 7FFFFFFF
f. Prints: FFFFFFFF
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 16
class N{
public static void main (String[] args) {
String s1 = null;
StringBuffer s2 = new StringBuffer();
System.out.print(s2.append(s1));
}
}
What is the result of attempting to compile and run the program?
a. Prints nothing
b. Prints: null
c. Compiler Error
d. Run time Error
e. None of the Above

Question 17
class A {
public static void main(String[] args) {
String s1 = "A";
String s2 = "a";
String s3 = "b";
s1.toLowerCase();
s3.replace('b','a');
System.out.print((s1.equals(s2)) + "," + (s2.equals(s3)));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 18
class M {
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer();
s1.ensureCapacity(16);
s1.append("ABCDEFG");
s1.setLength(100);
System.out.print(s1.length() + "," + s1.capacity());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 7,16
b. Prints: 16,16
c. Prints: 100,100
d. Compiler Error
e. Run time Error
f. None of the Above

Capítulo 10 – The java.lang Package (G)


Question 1
class C {
public static void main (String[] args) {
double d = Long.MAX_VALUE;
long l = Math.round(d + 1.0);
System.out.print((l == 0) + ",");
System.out.print((l == Long.MIN_VALUE) + ",");
System.out.print(l == Long.MAX_VALUE);
}
}
What is the result of attempting to compile and run the program?
a. Prints: false,false,false

Certified Java Programmer Mock Exam 141


b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: true,false,false
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 2
class A {
public static void main(String[] s) {
String s1 = "ABC";
String s2 = new String(s1);
String s3 = new String(s1);
System.out.print((s2 == s3) + ",");
System.out.print((s2.trim() == s2.toUpperCase()) + ",");
System.out.print(s3.concat("") == s3.replace('D','E'));
}
}
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. Compiler error
j. Run time error
k. None of the Above

Question 3
class B {
public static void main(String[] args) {
String s1 = "ABCDE";
System.out.print(s1.indexOf('A')+",");
System.out.print(s1.indexOf("A")+",");
System.out.print(s1.indexOf("A",1));
}
}
What is the result of attempting to compile and run the program?
a. Prints: -1,-1,-1
b. Prints: 0,0,0
c. Prints: 0,0,-1
d. Prints: 1,1,0
e. Prints: 1,1,-1
f. Prints: 1,1,1
g. Compiler error
h. Run time error
i. None of the Above

Question 4
class J {
public static void main (String[] args) {
float f1 = Math.min(-0.0,+0.0);
float f2 = Math.max(-0.0,+0.0);
System.out.print(f1+","+f2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0.0,0.0
b. Prints: 0.0,+0.0
c. Prints: -0.0,0.0
d. Prints: -0.0,+0.0
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 5
class E {
public static void main (String[] args) {
System.out.print(Math.sqrt(Math.exp(2)) == Math.E);
}
}
What is the result of attempting to compile and run the program?
a. Prints: true
b. Prints: false
c. Compiler Error
d. Runtime Error
Certified Java Programmer Mock Exam 142
e. None of the Above

Question 6
class C {
public static void main (String[] args) {
float f = (float)(Integer.MIN_VALUE - 1.0);
int i = Math.round(f);
System.out.print((i == 0) + ",");
System.out.print((i == Integer.MIN_VALUE) + ",");
System.out.print(i == Integer.MAX_VALUE);
}
}
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: true,false,false
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 7
class J {
public static void main (String[] args) {
byte x1 = 0;
byte x2 = 1;
byte a = Math.min(x1,x2); // 1
short x3 = 0;
short x4 = 1;
short b = Math.min(x3,x4); // 2
int c = Math.min(0,1); // 3
long d = Math.min(0L,+1L); // 4
System.out.print(a+","+b+","+c+","+d);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0,0,0
b. Compiler Error at 1
c. Compiler Error at 2
d. Compiler Error at 3
e. Compiler Error at 4
f. Runtime Error
g. None of the Above

Question 8
Which of the following statements are true in terms of the java.lang.Math.sqrt method?
a. It is not overloaded.
b. The argument type is a primitive int.
c. If the argument is negative then the square root of the absolute value is calculated.
d. Throws an ArithmeticException if the argument is negative.
e. None of the Above

Question 9
class E {
public static void main (String[] args) {
int a = -1;
long b = -2;
float c = -3.0f;
double d = -4.0;
a = Math.abs(a);
b = Math.abs(b);
c = Math.abs(c);
d = Math.abs(d);
System.out.print(a+b+c+d);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 10.0
b. Compiler Error
c. Runtime Error
d. None of the Above

Question 10
Which of the following methods of the java.lang.Math class declares a non-primitive argument type?
a. abs
b. ceil
c. floor
d. max
e. min
Certified Java Programmer Mock Exam 143
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the Above

Question 11
class D {
public static void main(String[] args) {
double d1 = Math.ceil(0.5);
double d2 = Math.ceil(1.5);
System.out.print(d1 + "," + d2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0.0,1.0
b. Prints: 0.0,2.0
c. Prints: 1.0,1.0
d. Prints: 1.0,2.0
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 12
Which of the following are not methods of the java.lang.String class?
a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. trim
h. valueOf
i. None of the above.

Question 13
class J {
static String m(byte i) {return "byte";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(b,1))+",");
System.out.print(m(Math.min(b,1L)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: byte,byte,byte
b. Prints: byte,int,long
c. Prints: int,int,int
d. Prints: int,int,long
e. Prints: long,long,long
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 14
class Yellow {
public static void main(String args[]) {
String a = "A";
String b = "B";
String c = a+b;
String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}
}
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
Certified Java Programmer Mock Exam 144
g. Prints: true,true,false
h. Prints: true,true,true
i. Compiler Error
j. Runtime Error
k. None of the Above

Question 15
class G {
public static void main (String[] args) {
char[] c = {'a','b','c','d'};
String s1 = String.copyValueOf(c);
boolean b = false;
for (int i = 0; i < s1.length; i++) {
b |= c[i] == s1.charAt(i);
}
System.out.print(b);
}
}
What is the result of attempting to compile and run the program?
a. Prints: false
b. Prints: true
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 16
class D {
public static void main (String[] args) {
System.out.print("CC".compareTo("C") + ",");
System.out.print("CC".compareTo("CC") + ",");
System.out.print("CC".compareTo("CCC"));
}
}
What is the result of attempting to compile and run the program?
a. Prints: false,true,false
b. Prints: 1,0,-1
c. Prints: -1,0,1
d. Compiler Error
e. Runtime Error
f. None of the Above

Question 17
class White {
public static void main(String args[]) {
String a = "A";
String b = "B";
String c = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c.intern()==("A"+"B"));
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above

Question 18
class Violet {
public static void main(String args[]) {
String a = "A";
String b = "B";
String c = a+b;
String d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c.intern()==d.intern());
}
}
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. Compiler Error
f. Runtime Error
g. None of the Above
Certified Java Programmer Mock Exam 145
Capítulo 10 – The java.lang Package (H)
Question 1
Which of the following statements are true in terms of the java.lang.Math.round method?
a. The returned value is of a floating-point primitive type.
b. The returned value is always of type int.
c. The returned value is always of type long.
d. The returned value is of type long only if the argument is of type long.
e. The returned value is of type long only if the argument is of type double.
f. None of the Above

Question 2
class K {
public static void main (String[] args) {
System.out.print(Math.round(-3.6)+ Math.round(3.6) + ",");
System.out.print(Math.round(-3.4)+ Math.round(3.4));
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,0
b. Prints: 0,-1
c. Prints: -1,0
d. Prints: -1,-1
e. Prints: 0,1
f. Prints: 1,0
g. Prints: 1,1
h. Compiler Error
i. Runtime Error
j. None of the Above

Question 3
class K {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
System.out.print(m(Math.sin(0.0))+",");
System.out.print(m(Math.cos(0.0))+",");
System.out.print(m(Math.tan(0.0)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: int,int,int
b. Prints: long,long,long
c. Prints: float,float,float
d. Prints: double,double,double
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 4
class B {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0f)));
System.out.print(m1(Math.abs(1.0d)));
System.out.print(m1(Math.sqrt(1.0f)));
System.out.print(m1(Math.sqrt(1.0d)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: IIII
b. Prints: ILIL
c. Prints: LLLL
d. Prints: FDFD
e. Prints: FDDD
f. Prints: DDFD
g. Prints: DDDD
h. None of the Above

Question 5

Certified Java Programmer Mock Exam 146


Which of the following statements are true in terms of the java.lang.Math.sin method?
a. It is not overloaded.
b. The argument type is a primitive int.
c. The argument is an angle measured in radians.
d. Throws an ArithmeticException if the argument is greater than 360.
e. None of the Above

Question 6
class A {
public static void main (String[] args) {
System.out.print(Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE));
}
}
What is the result of attempting to compile and run the program?
a. Prints: false
b. Prints: true
c. Compiler Error
d. Runtime Error
e. None of the Above

Question 7
Which of the following statements are true in terms of the value returned by the java.lang.Math.round method?
a. Rounds the argument to the nearest whole number and if the result is equally close to two whole numbers then returns the even one.
b. Rounds the argument to the nearest whole number and if the result is equally close to two whole numbers then returns the odd one.
c. Adds 0.5 to the argument and takes the floor of the result.
d. Subtracts 0.5 from the argument and takes the ceil of the result.
e. None of the Above

Question 8
class C {
public static void main (String[] args) {
int i1 = Math.round(0.5f);
int i2 = Math.round(1.5d);
System.out.print(i1 + "," + i2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0,1
b. Prints: 0,2
c. Prints: 1,1
d. Prints: 1,2
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 9
Which of the following methods of the java.lang.Math class declares a non-primitive return type?
a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the Above

Question 10
class K {
public static void main (String[] args) {
System.out.print(Math.floor(-3.6)+ ",");
System.out.print(Math.ceil(-3.6)+ ",");
System.out.print(Math.floor(3.6)+ ",");
System.out.print(Math.ceil(3.6));
}
}
What is the result of attempting to compile and run the program?
a. Prints: -3.0,-4.0,3.0,4.0
b. Prints: -3.0,-4.0,4.0,3.0
c. Prints: -4.0,-3.0,3.0,4.0
d. Prints: -4.0,-3.0,4.0,3.0
e. Prints: -4.0,-4.0,3.0,3.0
f. Compiler Error
g. Runtime Error
h. None of the Above

Certified Java Programmer Mock Exam 147


Question 11
class C {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0)));
System.out.print(m1(Math.ceil(1.0)));
System.out.print(m1(Math.max(1.0,1.0)));
System.out.print(m1(Math.round(1.0)));
System.out.print(m1(Math.sin(1.0)));
System.out.print(m1(Math.sqrt(1.0)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: DDDDDD
b. Prints: LLLLLL
c. Prints: DLLLDD
d. Prints: DLDLDD
e. Prints: DDDLDD
f. Prints: DIDIDD
g. None of the Above

Question 12
class J {
public static void main (String[] args) {
float f1 = -0.0f;
float f2 = +0.0f;
float f3 = Math.min(f1,f2);
float f4 = Math.max(f1,f2);
System.out.print(f3+","+f4);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0.0,0.0
b. Prints: 0.0,+0.0
c. Prints: -0.0,0.0
d. Prints: -0.0,+0.0
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 13
class A {
static String m1(byte i) {return "B";}
static String m1(char i) {return "C";}
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.min((byte)1,(byte)2)));
System.out.print(m1(Math.min('A','B')));
System.out.print(m1(Math.min(1,2)));
System.out.print(m1(Math.min((long)1,2)));
System.out.print(m1(Math.min(1.0f,1)));
System.out.print(m1(Math.min(1.0,1)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: DDDDDD
b. Prints: FFFFFD
c. Prints: FFFDFD
d. Prints: IIILFD
e. Prints: IILLDD
f. Prints: IIILDD
g. Prints: BCILFD
h. Prints: CCILFD
i. None of the Above

Question 14
class J {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
System.out.print(m(Math.random()));
Certified Java Programmer Mock Exam 148
}
}
What is the result of attempting to compile and run the program?
a. Prints: int
b. Prints: long
c. Prints: float
d. Prints: double
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 15
class A {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0f)));
System.out.print(m1(Math.abs(1.0)));
System.out.print(m1(Math.round(1.0f)));
System.out.print(m1(Math.round(1.0)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: IIII
b. Prints: LLLL
c. Prints: FFFF
d. Prints: DDDD
e. Prints: FDFD
f. Prints: ILIL
g. Prints: FDIL
h. Prints: ILFD
i. None of the Above

Question 16
Suppose that the java.lang.Math.round method is invoked with a primitive float type and the result exceeds the range of type int. Which of the
following occurs?
a. The result is implicitly widened to type long.
b. The returned value is Float.NaN.
c. A run-time exception is thrown.
d. The bits that overflow are ignored and no exception is thrown.
e. The returned value is zero but no exception is thrown.
f. None of the Above

Question 17
class A {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.floor(1.0f)));
System.out.print(m1(Math.floor(1.0d)));
System.out.print(m1(Math.ceil(1.0f)));
System.out.print(m1(Math.ceil(1.0d)));
}
}
What is the result of attempting to compile and run the program?
a. Prints: IIII
b. Prints: ILIL
c. Prints: LLLL
d. Prints: FDFD
e. Prints: DDDD
f. None of the Above

Question 18
class D {
public static void main(String[] args) {
double d1 = Math.floor(0.5);
double d2 = Math.floor(1.5);
System.out.print(d1 + "," + d2);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0.0,1.0
b. Prints: 0.0,2.0
c. Prints: 1.0,1.0
d. Prints: 1.0,2.0
Certified Java Programmer Mock Exam 149
e. Compiler Error
f. Runtime Error
g. None of the Above

Capítulo 11 – Collections (A)


Question 1
Which of the following classes allow unsynchronized read operations by multiple threads?
a. Vector
b. Hashtable
c. TreeMap
d. TreeSet
e. HashMap
f. HashSet
g. WeakHashMap
h. None of the above

Question 2
import java.util.*;
class D {
public static void main (String[] args) {
Object m = new LinkedHashSet();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof AbstractSet)+",");
System.out.print((m instanceof HashSet)+",");
}
}
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. None of the above

Question 3
A class C has legal implementations of the equals and hashCode methods. On Monday, an instance of the class is created and the hashCode
method is invoked. On Tuesday the program is loaded again and an instance of class C is created containing the same data that was loaded on
Monday. If the hashCode method is invoked after restarting the program on Tuesday then the hashCode method must return the same integer
value that was returned on Monday.
a. false
b. true

Question 4
Suppose that class C has legal implementations of the hashCode and equals methods. Within any one execution of the Java application the
hashCode contract requires that each invocation of the hashCode method of class C must consistently return the same result as long as the
fields used for the equals comparison remain unchanged.
a. false
b. true

Question 5
import java.util.*;
class G {
public static void main (String args[]) {
Object a = new HashSet();
Object b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof AbstractCollection)+",");
System.out.print((b instanceof AbstractCollection)+",");
System.out.print(c instanceof AbstractCollection);
}
}
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. None of the above

Question 6

Certified Java Programmer Mock Exam 150


A class C contains a field of type int and a large array of primitives of type double. Programmer A suggests writting a hashCode method that
saves time by calculating only the hashCode of the int field. Programmer B suggests writing a hashCode method that includes every element of
the array in the calculation. As the manager of the software engineering department you must now decide which programmer receives the
higher pay raise at the end of the year? Which is it going to be?
a. Programmer A
b. Programmer B

Question 7
import java.util.*;
class E {
public static void main (String args[]) {
AbstractSet a = new TreeSet();
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
An immutable class C contains a field of type int and a large array of primitives of type double. You must consider developing a hashCode
method based one of these three options. Which of the three is most likely to optimize the performance of a Hashtable without violating any of
the rules for coding a hashCode method?
a. Calculate the hashCode using only the int field.
b. Calculate the hashCode using both the int field and the array.
c. Calculate the hashCode using both the int field and the array, but only calculate the hashCode once and store the value for future use in an
instance variable.

Question 9
Which implementation of the List interface provides for the slowest access to an element in the middle of the list by means of an index?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above

Question 10
import java.util.*;
class H {
public static void main (String[] args) {
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}
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. None of the above

Question 11
a. Stores key/value pairs.
b. Allows null elements, keys, and values.
c. Duplicate entries replace old entries.
d. Entries are sorted using a comparator or the
Comparable interface.

Which of these classes provides the specified features?


a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. Hashtable
g. None of the above

Question 12
import java.util.*;
class F {
Certified Java Programmer Mock Exam 151
public static void main (String args[]) {
LinkedList a = new LinkedList();
ArrayList b = new ArrayList();
Vector c = new Vector();
System.out.print((a instanceof List)+",");
System.out.print((b instanceof List)+",");
System.out.print(c instanceof List);
}
}
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. None of the above

Question 13
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.
a. false
b. true

Question 14
If two instances of a class type are equal according to the equals method, then the same integer value must be returned by the hashCode
method of the two objects.
a. false
b. true

Question 15
a. Each element must be unique.
b. Duplicate elements must not replace old elements.
c. Elements are not key/value pairs.
d. Accessing an element is almost as fast as performing the same operation on an array.
Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. LinkedHashMap
g. LinkedHashSet
h. Hashtable
i. None of the above

Question 16
a. Entries are organized as key/value pairs.
b. Duplicate entries replace old entries.
c. Entries are sorted using a comparator or the
Comparable interface.
Which interface of the java.util package offers the specified behavior?
a. List
b. Map
c. Set
d. SortedSet
e. SortedMap
f. None of the above

Question 17
import java.util.*;
class E {
public static void main (String[] args) {
Object a = new ArrayList();
System.out.print((a instanceof Collections)+",");
System.out.print((a instanceof Arrays)+",");
System.out.print((a instanceof List)+",");
}
}
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
Certified Java Programmer Mock Exam 152
h. Prints: true,true,true
i. None of the above

Question 18
a. Each element must be unique.
b. Duplicate elements must not replace old elements.
c. Elements are not key/value pairs.
d. Entries are not sorted using a comparator or the
Comparable interface.
e. The iteration order is determined by the insertion order.
Which of these classes provides the specified features?
a. HashMap
b. HashSet
c. Hashtable
d. LinkedHashMap
e. LinkedHashSet
f. LinkedList
g. TreeMap
h. TreeSet
i. None of the above

Question 19
Which of the following classes would provide the most efficient implementation of a First In First Out queue?
a. ArrayList
b. LinkedHashMap
c. LinkedHashSet
d. LinkedList
e. HashMap
f. HashSet
g. Hashtable
h. TreeMap
i. TreeSet
j. Vector
k. WeakHashMap
l. None of the above

Capítulo 11 – Collections (B)


Question 1
a. Entries are not organized as key/value pairs.
b. Duplicate entries are rejected.
c. Entries are sorted using a comparator or the
Comparable interface.
Which interface of the java.util package offers the specified behavior?
a. List
b. Map
c. Set
d. SortedSet
e. SortedMap
f. None of the above

Question 2
import java.util.*;
class A {
public static void main (String args[]) {
AbstractList a = new LinkedList();
AbstractSet b = new TreeSet();
AbstractMap c = new TreeMap();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}
}
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. None of the above

Question 3
import java.util.*;
class J {

Certified Java Programmer Mock Exam 153


public static void main (String[] args) {
Object i = new ArrayList().listIterator();
System.out.print((i instanceof List)+",");
System.out.print((i instanceof Iterator)+",");
System.out.print(i instanceof ListIterator);
}
}
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. None of the above

Question 4
import java.util.*;
import java.io.Serializable;
class J {
public static void main (String args[]) {
HashMap a = new HashMap();
boolean b1, b2, b3;
b1 = (a instanceof Cloneable) & (a instanceof Serializable);
b2 = (a instanceof Map) & (a instanceof AbstractMap);
b3 = a instanceof Collection;
System.out.print(b1 + "," + b2 + "," + b3);
}
}
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. None of the above

Question 5
import java.util.*;
class I {
public static void main (String args[]) {
Object a = new HashSet();
Object b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Cloneable)+",");
System.out.print((b instanceof Cloneable)+",");
System.out.print(c instanceof Cloneable);
}
}
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. None of the above

Question 6
Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above

Question 7
import java.util.*;
class H {
public static void main (String args[]) {
Object a = new HashSet();
Object b = new HashMap();
Object c = new Hashtable();
Certified Java Programmer Mock Exam 154
System.out.print((a instanceof Map)+",");
System.out.print((b instanceof Map)+",");
System.out.print(c instanceof Map);
}
}
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. None of the above

Question 8
a. Entries are not organized as key/value pairs.
b. Generally accepts duplicate elements.
c. Entries may be accessed by means of an index.
Which interface of the java.util package offers the specified behavior?
a. List
b. Map
c. Set
d. None of the above

Question 9
a. Entries are organized as key/value pairs.
b. 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 10
import java.util.*;
class A {
public static void main (String[] args) {
Object m = new LinkedHashMap();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Map)+",");
System.out.print(m instanceof List);
}
}
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. None of the above

Question 11
import java.util.*;
class I {
public static void main (String[] args) {
Object i = new ArrayList().iterator();
System.out.print((i instanceof List)+",");
System.out.print((i instanceof Iterator)+",");
System.out.print(i instanceof ListIterator);
}
}
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. None of the above

Question 12
class A {
Certified Java Programmer Mock Exam 155
private int[] val;
private int hash;
public int hashCode() {
int h = hash;
if (h == 0) {
int off = 0;
int len = val.length;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
// The equals method has been omitted for clarity.
A (int[] val) {this.val = val;}
public static void main (String[] args) {
A a = new A(new int[]{1,2,3});
System.out.print(a.hashCode());
}
}
What is the result of attempting to compile and run the program?
a. Prints: 1026
b. Prints: 1091
c. Prints: 31806
d. Compiler error
e. Run time error
f. None of the above

Question 13
Which of the following are true statements?
a. The Iterator interface declares only two methods: hasMoreElement and nextElement.
b. The ListIterator interface extends both the List and Iterator interfaces.
c. The ListIterator interface was introduced with Java 1.2 to replace the older Iterator interface that was released with Java 1.0.
d. The ListIterator interface declares only three methods: hasNext, next, and remove.
e. None of the above.

Question 14
import java.util.*;
class C {
public static void main (String[] args) {
Object m = new LinkedHashSet();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Set)+",");
System.out.print(m instanceof List);
}
}
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. None of the above

Question 15
In addition to implementing the List interface, which of the following also provides methods to get, insert, and remove elements from the head
and tail of the list?
a. Collection
b. ArrayList
c. LinkedList
d. List
e. Vector
f. None of the above

Question 16
a. Stores key/value pairs.
b. Allows null elements, keys, and values.
c. Duplicate entries replace old entries.
d. Entries are not sorted using a comparator or the
Comparable interface.
e. The iteration order is unspecified.
Which of these classes provides the specified features?
a. LinkedList
b. LinkedHashMap
c. LinkedHashSet
Certified Java Programmer Mock Exam 156
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 17
Which of the following are true statements?
a. All implementations of the List interface provide fast random access.
b. A LinkedList provides faster random access than an ArrayList.
c. The LinkedList implements the RandomAccess interface.
d. Each collection that implements the List interface must also implement the RandomAccess interface.
e. The RandomAccess interface declares method that allow fast access to elements by use of an index.
f. The RandomAccess interface declares the next and hasNext methods.
g. None of the above.

Question 18
import java.util.*;
class F {
public static void main (String[] args) {
Object v = new Vector();
System.out.print((v instanceof Collections)+",");
System.out.print((v instanceof Arrays)+",");
System.out.print(v instanceof List);
}
}
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. None of the above

Capítulo 11 – Collections (C)


Question 1
import java.util.*;
class D {
public static void main (String args[]) {
AbstractSet 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 2
Which of the following classes allow elements to be accessed in the order that they were added?
a. ArrayList
b. LinkedHashMap
c. LinkedHashSet
d. LinkedList
e. HashMap
f. HashSet
g. Hashtable
h. TreeMap
i. TreeSet
j. Vector
k. WeakHashMap
l. None of the above

Question 3
Which of the following are true statements?
a. The Enumeration interface was introduced with the collections framework with Java 1.2.
b. The Enumeration interface declares only two methods: hasMoreElements and nextElement.
c. The Iterator interface extends the Enumeration interface.
d. The Iterator interface declares a total of three methods.

Certified Java Programmer Mock Exam 157


e. None of the above.

Question 4
import java.util.*;
class C {
public static void main (String args[]) {
Object a = new Vector();
Object b = new WeakHashMap();
Object c = new HashSet();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}
}
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. None of the above

Question 5
The presence of a mapping for a given key within
this collection instance will not prevent the key
from being recycled by the garbage collector.
Which concrete class provides the specified features?
a. Vector
b. Hashtable
c. TreeMap
d. TreeSet
e. HashMap
f. HashSet
g. WeakHashMap
h. None of the above

Question 6
a. Duplicate elements are not accepted.
b. Entries are sorted.
Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. Hashtable
g. None of the above

Question 7
import java.util.*;
class B {
public static void main (String args[]) {
AbstractMap a = new HashMap();
AbstractList b = new ArrayList();
AbstractSet c = new HashSet();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}
}
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. None of the above

Question 8
import java.util.*;
class B {
public static void main (String[] args) {
Object m = new LinkedHashMap();
Certified Java Programmer Mock Exam 158
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof AbstractMap)+",");
System.out.print((m instanceof HashMap)+",");
}
}
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. None of the above

Question 9
a. Entries are not organized as key/value pairs.
b. Duplicate entries are rejected.
Which interface of the java.util package offers the specified behavior?
a. List
b. Map
c. Set
d. None of the above

Question 10
a. Stores key/value pairs.
b. Allows null elements, keys, and values.
c. Duplicate entries replace old entries.
d. Entries are not sorted.
Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. Hashtable
g. None of the above

Question 11
Suppose that you would like to create an instance of a new Set that has an iteration order that is the same as the iteration order of an existing
instance of a Set. Which concrete implementation of the Set interface should be used for the new instance?
a. The answer depends on the implementation of the existing instance.
b. HashSet
c. LinkedHashSet
d. TreeSet
e. None of the above.

Question 12
a. Stores key/value pairs.
b. Duplicate entries replace old entries.
c. Provides constant-time performance for the add, contains and remove operations.
Which of these classes provides the specified features?
a. LinkedHashMap
b. LinkedHashSet
c. LinkedList
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 13
Which of the following are true statements?
a. The Iterator interface declares only three methods: hasNext, next and remove.
b. The ListIterator interface extends both the List and Iterator interfaces.
c. The ListIterator interface provides forward and backward iteration capabilities.
d. The ListIterator interface provides the ability to modify the List during iteration.
e. The ListIterator interface provides the ability to determine its position in the List.
f. None of the above.

Question 14
a. Stores key/value pairs.
b. Does not allow null elements, keys, and values.
Which of these classes provides the specified features?
a. LinkedList
b. LinkedHashMap
c. LinkedHashSet
Certified Java Programmer Mock Exam 159
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 15
import java.util.*;
class G {
public static void main (String[] args) {
Object a = new ArrayList();
Object l = new LinkedList();
Object v = new Vector();
System.out.print((a instanceof RandomAccess)+",");
System.out.print((l instanceof RandomAccess)+",");
System.out.print(v instanceof RandomAccess);
}
}
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. None of the above

Question 16
a. Stores key/value pairs.
b. Allows null elements, keys, and values.
c. Duplicate entries replace old entries.
d. The least recently used element can be removed automatically when a new element is added.
Which of these classes provides the specified features?
a. LinkedHashMap
b. LinkedHashSet
c. LinkedList
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 17
Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing
instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
a. TreeMap
b. HashMap
c. LinkedHashMap
d. The answer depends on the implementation of the existing instance.
e. None of the above.

Question 18
Which of these classes declares the removeEldestEntry method?
a. LinkedHashSet
b. LinkedHashMap
c. LinkedList
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Certified Java Programmer Mock Exam 160


Respostas

Capítulo 2 – Language Fundamentals (A)


No.-Answer-Remark
1-g -None of the Above -Section 12.1.4 of the Java Language Specification states the following, "The method main must be declared public,
static, and void. It must accept a single argument that is an array of Strings." In each of the three class declarations, the single argument is
indeed an array of Strings.
2-f -Compiler Error -Variables declared inside of a block or method are called local variables and they are not automatically initialized. (Locally
declared arrays are an exception to the rule, but none are used in this question.) The compiler will generate an error as a result of the attempt to
access the local variables before a value has been assigned.
3-b d f i j k -catch instanceof const goto import transient -Both "const" and "goto" are java keywords, but neither is used.
4-b -Prints: null -Null is converted to a string and is printed as the word null.
5-c e g i -const continue extends break -Both "const" and "goto" are java keywords, but neither is used. All of the letters of all java
keywords are lower case. In this question, the letter O of the word "instanceof" was upper case.
6-d e f -Run time error at line 2. Run time error at line 5. Run time error at line 8. -Section 12.1.4 of the Java Language Specification states
the following, "The method main must be declared public, static, and void. It must accept a single argument that is an array of Strings." The rule
is not enforced by the compiler, but should instead be enforced by the Java Virtual Machine. However, not every JVM enforces the rule. Even
so, for the purposed of the SCJP exam, the main method should be declared as stated in the Java Language Specification. In this example,
each of the three main methods generates a run-time exception because they are not declared static.
7-b d e f g h -goto package synchronized default interface volatile -Both "const" and "goto" are java keywords, but neither is used.
8-e -None of the Above -Unicode literals are declared using single quotes but none of the declarations here use single quotes. The declaration
of char, b, is also problematic because it contains more than one char.
9-b g i -goto implements const -Both "const" and "goto" are java keywords, but neither is used.
10-b -Prints: BCD -The index for the first element of an array is zero.
11-d -Prints: -128,127 -A byte is an 8 bit signed value.
12-c -Prints: 7f,ffff,7fff -A byte is an 8 bit signed value. A char is a 16 bit unsigned value. A short is a 16 bit signed value. The left most bit of a
signed value is the sign bit. The sign bit is zero for positive numbers and one for negative numbers. The hexadecimal value 7f is equal to the
decimal value 127. The hexadecimal value ff is equal to the decimal value negative 128.
13-a -boolean b1 = true; -There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C
programming language accepts zero as a boolean value, the Java programming language does not.
14-b -Prints: -32768,32767 -A short is a 16 bit signed value.
15-b -Prints: 8000000000000000,7fffffffffffffff -A long is a 64 bit signed value. The left most bit is the sign bit. The sign bit is zero for positive
numbers and one for negative numbers.
16-e f -Run time error at line 5. Run time error at line 8. -Section 12.1.4 of the Java Language Specification states the following, "The method
main must be declared public, static, and void. It must accept a single argument that is an array of Strings." The rule is not enforced by the
compiler, but should instead be enforced by the Java Virtual Machine. However, not every JVM enforces the rule. Even so, for the purposed of
the SCJP exam, the main method should be declared as stated in the Java Language Specification. Note: the Java Language Specification
provides the option to disallow the declaration of more than one class in a single source code file if more than one of the classes in the file is
referred to by code that exists outside of the file. The restriction is stated as an option and not a requirement. This option is not enforced by any
compiler that I have tested.
17-e -Compiler Error. -Teal.b1 is always initialized because it is a class member. Arrays are always initialized even if declared locally. Local
variable b2 is not initialized because it is local. Consequentially, a compiler error is generated when the attempt is made to print the value of b2.

18-c -Prints 0,0.0,0.0,false,null -Generally speaking, numberic type variables are initialized to zero. Booleans are initialized to false. Reference
type variables are initialized to null.
19-a -Prints: 0,0,0,0,0,null -

Capítulo 2 – Language Fundamentals (B)


No.-Answer-Remark
1-j -None of the Above -
2-b d g h -strictfp super goto native -Both "const" and "goto" are java keywords, but neither is used.
3-b c e g i -boolean short const instanceof goto -Both "const" and "goto" are java keywords, but neither is used.
4-e -None of the Above -String literals are declared using double quotes but all of the declarations here use single quotes.
5-d -Prints: 0 -Member variables are initialized automatically. Type int variables are initialized to zero.
6-b c e -Compiler error at line 4. Compiler error at line 5. Compiler error at line 7. -The escape sequences are defined in section 3.10.6 of
the Java Language Specification.
7-a e -transient volatile -Both "const" and "goto" are java keywords, but neither is used. Serializable, Runnable, Externalizable, and Cloneable
are all interfaces. Thread.run is a method. The keywords transient and volatile are field modifiers.
8-a b c d g i j k -byte short int long float double boolean char -
9-d -4 -The literal, 1.0, is a double and can not be used to initialize a float without an explicit cast.
10-f -None of the Above -All of the declarations are legal. String b is a single quote followed by the letter A followed by another single quote.
String C is the letter A. String D is the unicode character that represents the hexidecimal value abcd.
11-a -Prints: 1111111,177,127,7f -A byte is an 8 bit signed value. The left most bit is the sign bit. The sign bit is set to zero for positive
numbers and is set to one for negative numbers. The most positive byte value is represented as a sign bit that is set to zero and all of the other
bits set to one. The Integer.toBinaryString method does not print leading zeros. An eight bit binary value is represented as three octal digits. The
first of the three digits represents the left most two bits of the binary value. In this case, the left most two bits are zero and one. The second octal
digit represents the next three bits of the binary value. The last of the octal digits represents the right most three bits of binary value. Note that
the Integer.toOctalString method does not print a leading zero as is required for an octal literal value. An eight bit binary value is represented as
two hexadecimal digits. The left hex digit represents the left most bits of the binary value. The right hex digit represents the right most bits of the
binary value.
12-b -Prints: 1010101,125,85,55 -A byte is an 8 bit signed value. The left most bit is the sign bit. The sign bit is set to zero for positive
numbers. An eight bit binary value is represented as three octal digits. The first of the three digits represents the left most two bits of the binary
value including any leading zeros. Please note that Integer.toBinaryString does not include the leading zeros. The second octal digit represents
the next three bits of the binary value. The last of the octal digits represents the right most three bits of binary value. An eight bit binary value is
represented as two hexadecimal digits. The left hex digit represents the left most bits of the binary value. The right hex digit represents the right
most bits of the binary value. The decimal value of 85 can be converted to binary as follows. 85=64+16+4+1=01010101

Certified Java Programmer Mock Exam 1


13-f -Compiler error. -A compiler error is generated by the attempt to assign the line feed character to char variable l. A unicode escape
character should not be used to represent a linefeed or a carriage return. Instead, the escape sequence '\r' should be used to represent a
carriage return, and '\n' should be used to represent a linefeed.
14-a b c e -char a = 0x0041; char b = '\u0041'; char c = 0101; char e = (char)-1; -The primitive char type is unsigned so a negative int can
not be assigned to a char without an explicit cast. The result of the explicit cast is not a negative value. Instead, it is interpreted as a positive
value that is very large. To be precise, it is the maximum value of the primitive char type.
15-f -Prints: 80000000,7fffffff -An int is a 32 bit signed value. The left most bit is the sign bit. The sign bit is zero for positive numbers and one
for negative numbers.
16-e -Run-time exception -The index is incremented before the array access expression is evaluated so the first element accessed is at index
one instead of zero. An ArrayIndexOutOfBoundsException is thrown when an attempt is made to access an element past the end of the array.
17-e f -Compiler error at line 5. Compiler error at line 6. -The non-static field x can not be referenced from within a static method. The local
variable y has not been initialized.
18-c -Prints: 0null -The numeric sum of variables a, b, c, d, and e is zero. The zero is converted to a string and concatenated with s.
19-d -Prints: 127 -128 -1 0 -Bytes are stored as 8 bit two's complement signed integers. When an int primitive is cast to a byte, the three most
significant bytes are discarded and only the least significant byte remains. The most significant bit of the remaining byte becomes the new sign
bit.

Capítulo 3 – Operators and Assignments (A)


No.-Answer-Remark
1-f -Compiler Error -The static main method can not access the non-static method m.
2-e -Prints: true,false,false -The right operand of the conditional "or" operator is not evaluated if the left hand operand is true. In this case the
right hand operand of the conditional "or" operator is the result of the conditional "and" expression. Therefore, the conditional "and" expression is
not evaluated.
3-b -Prints: false,false,true -The result of dividing two float values is a float. When the float is converted to a double a rounding error is
introduced. For that reason, the value of variable a is different from the value of variables b and c.
4-d -Prints: false,true,true -The right operand of the conditional "or" operator is evaluated only if the left hand operand is false. In this case, the
left operand of the conditional "or" operator is false so the right operand must also be evaluated. The right operand of the conditional "and"
operator is only evaluated if the left hand operand is true. In this case, the left hand operand is true so the right hand expression is evaluated.
5-j -Compiler error -Both operands of the conditional "and" operator and conditional "or" operator must be of type boolean.
6-b -Prints: 2 -Java evaluates expressions from left to right while respecting operator precedence. The multiplication operator has higher
precedence than the logical and bitwise operators. The "and" operator has higher precedence than the "exclusive or" operator. The "or" operator
has lower precedence. Although complete memorization of the operator precedence chart is not necessary, the level required for this question is
required. (1 | 2 ^ 3 * 2 & 13 | 2), (1 | (2 ^ ((3 * 2) & 13)) | 2), (1 | (2 ^ ((6) & 13)) | 2), (1 | (2 ^ (4)) | 2), (1 | 6 | 2 = 7), (7%5=2)
7-b -Prints: 2,3 -Primitive type variables are passed to methods by value. In other words, only the value of the variable is passed to the method
so the method has no access to the variable itself. If the method modifies a primitive value that has been passed to the method as a parameter,
then the modification does not impact the variable outside of the method. Instead, only the value of the local copy is modified. For that reason,
the method m does not change the value of the variable y in the main method. However, method m does indeed have direct access to the
member variable x and its value is modified by method m and the modification is seen within the main method.
8-a -Prints: 78 ABC*$ -When char variables a and b are promoted to String types they are printed as *$. When not promoted to String types
they are printed as the numeric sum of 0x2a and 0x24. Note: 0x2a is decimal 42 and 0x24 is decimal 36.
9-g -Compiler error at line 10. -The reference "base" is of type "Base[]", therefore an explicit cast to type "Base[]" is required.
10-j -Compiler error. -The result of the addition of a and b is an int. The attempt to assign the int result to a byte variable e generates a possible
loss of precision error. The precedence of the cast operator is higher than the precedence of the addition operator. Therefore, the cast applies
only to variable a and not to the result of the addition.
11-c -Prints: 1,2,3,4,5,3 -Java evaluates expressions from left to right while respecting operator precedence. j = 1 + (((2 * 3) % 4) + 5)
12-g -Compiler error at line 5. -Although the referenced object is indeed an array of type int, an explicit cast is necessary to cast the obj
reference to an int array.
13-b -Prints: BBABBA -The first method call, m(null,null), calls method m with two null literals that have type null. Both null literals are promoted
to type B because B is more specific than type A. As a result, m(B x, B y) is invoked. The second method call, m(a=null,b=null), calls m with a
first parameter of type A and a second parameter of type B. As a result, m(A x, B y) is invoked because the parameter types are matched
exactly. The third method call invokes m(B x, A y) because the parameter types match exactly.
14-d -Prints: -1,127 -The initial value of b is -1. The two's compliment representation of -1 is the binary value 11111111. If the left hand
operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. When a byte equal to minus 1 is promoted
to an int the resulting int representation of minus one has all 32 bits set to one. When an int value of minus one is shifted right one bit the result
is a sign bit of zero and the remaining 31 bits set to one. If that value were printed as an int it would be the maximum positive value of an int.
However, if it is cast to a byte then it is reduced to eight bits that are all set to one. As stated earlier, that is the two's compliment representation
of negative one. In the line marked with the number three the byte value of negative one is shifted to the right 25 bits. First, the byte is promoted
to an int value of minus one. The result is all 32 bits of the int are set to one. When the int is shifted right 25 bits the result is 25 bits set to zero
and the right most seven bits set to one. That is the binary representation of 127. When the int value of 127 is cast to a byte the result is also the
value 127.
15-g -Compiles and runs without error. -All array types implement the Serializable interface and may be assigned to a reference of type
Serializable.
16-f -Compiles and runs without error. -Although line 12 appears to be the source of a compiler error it is not. In line 12, the reference named
base actually refers to an object of type Sub. Therefore, the reference to the object may be cast to type Sub.
17-a -Prints: float,float -An argument of type long can be widened to a float or a double. Since the float type is more specific than a double the
float type is selected before double.
18-c -Prints: false,true,false -When Byte.MAX_VALUE is multiplied by two, the result is a byte with the most significant seven bits set to one
and the least significant bit set to zero. The two's compliment representation of negative two is the same.

Capítulo 3 – Operators and Assignments (B)


No.-Answer-Remark
1-c -Prints: BFCTAT -The right operand of the conditional "or" operator is evaluated only if the left hand operand is false. In this case, the left
operand of the first conditional "or" operator is false so the right hand operand is evaluated. No further evaluation of the expression is necessary
so the right hand operand of the second conditional "or" operator is not evaluated.
2-f g h -Compiler error at 6 Compiler error at 7 Compiler error at 8 -The compiler will implicitly do a narrowing conversion for an assignment
statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable
on the left and if the variable is of type byte, short, or char.
3-f -Prints: true,false,true -Positive infinity is equal to positive infinity. NaN is not equal to anything including itself.
Certified Java Programmer Mock Exam 2
4-b -Prints: NaN,NaN,2.0 -If the dividend is infinity then the result is NaN. If the dividend is finite and the divisor is infinity, then the result is the
dividend.
5-f -Runtime error at line 10. -The object referenced by obj is of type "Base[]", and can not be cast to type "Sub[]".
6-d -Prints: 2, 2, -3, 3, 4, -
7-d -Runtime error at line 10. -The compiler accepts the explicit cast at line 10. However, Base is not a subclass of Sub; therefore, a runtime
exception is thrown.
8-e -Compiler error at line 6. -An explicit cast to type int is required.
9-a -Prints: true -A char type literal or variable can be cast to a numberic value.
10-d -Prints: BB -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, B is more specific than A because B extends A. The method m(B b, B b) is more specific than the others because both
parameters are the most specific.
11-c -Prints: 5 -A compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1.
Therefore, the statements int a=1; a+=++a+a++; can be rewritten as a=(int)((1)+(++a + a++));. With further evaluation we have a=(int)((1)+(2 +
2)).
12-b -Prints: 2, 2, -3, -4, 12, -Integral primitives are stored in two's compliment format. To change a positive value into a negative value, invert
each bit and add one to the result. The "~" operator is the bitwise compliment operator. It inverts the value of each bit. For example, ~0=-1.
13-i -Compiles and runs without error. -Although the reference named base is of type Base, the object that it refers to is of type Sub. Therefore,
the object may be cast to type Sub. Since objects of type Sub implement both interfaces, I1 and I2, the Sub type objects can be assigned to
references of type I1 and I2 without an explicit cast.
14-b -Prints: String -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, the String type is more specific than the Object type because String extends Object.
15-g -Compiler error at line 6. -An array of primitive types can not be cast to an array of a different primitive type.
16-e -Compiler error at line 5. -The length attribute of the array is accessed as though it were a method.
17-c -Compiler error. -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, both the String class and class A are more specific than type Object because both extend Object. However, String and A are
equally specific because one does not extend the other. The ambiguity generates a compiler error.
18-g -None of the above. -The program compiles and runs without error and prints 112. It is necessary to remember that all arrays are objects
and therefore are clonable. Furthermore, a two dimensional array is also a single dimensional array of single dimensional arrays.

Capítulo 3 – Operators and Assignments (C)


No.-Answer-Remark
1-c -Prints: 195ab -The operands of the first addition operator are both char literals and are evaluated as integral numeric values. The right
hand operand of the second addition operator is a String so the result of the first addition operator is promoted to a String type. The rest of the
operands within the expression will later be promoted to String values and the addition operators will be evaluated as String concatenation
operators.
2-e -Prints: -0.0,0.0,0.0 -A positive value multiplied by a negative value is negative. A negative value multiplied by a negative value is positive.
A positive value added to a negative value of equal magnitude is zero.
3-c d -Compiler error at 3 Compiler error at 4 -This question demonstrates a variety of assignment conversions. The compiler will implicitly do
a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the
value falls within the range of the variable on the left and if the variable on the left is of type byte, short, or char. In this case, variables s1 and c1
are not compile time constants so the compiler will not do an implicit narrowing conversion. However, variabls s2 and c2 are compile time
constants that fall within the range of the left hand operand.
4-d -Prints: 5 -If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. If the
promoted type of the left hand operand is of type int, then the shift operator only uses the least significant five bits of the right hand operand and
the actual shift distance is always less than 32 bits. Therefore, if the right hand operand is 32 then the shift distance is zero because all five of
the least significant bits are zero. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are
used.
5-c -Prints: -1 -If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all
four bytes are shifted. The result is of type int. The compound assignment operator includes an implicit cast to the type of the left hand operand.
Therefore, the result of the shift operation, an int, is cast to a byte before the assignment to variable b. The result of the cast is the contents of
the least significant byte. In this case, all eight bits are set to one so the result is a negative one in two's compliment format.
6-a -Prints: float,float -An argument of type char can be widened to an int, long, float or double. An argument of type long can be widened to a
float or a double. Since the float type is the most specific type available here it selected before double.
7-d -Prints: true,false,false -When two is added to Integer.MAX_VALUE the result is an integer with the most significant bit set to one and the
least significant bit set to one and all the bits in between set to zero. The same is true for the two's compliment representation of the most
negative integer plus one.
8-d -Prints: true, true, true -Negative infinity is rounded to the most negative value of type int or long. Positive infinity is rounded to the most
positive value of type int or long. NaN is converted to zero.
9-g -Compiles and runs without error. -The null literal is converted to an int array type. All array types implement the Cloneable interface and
may therefore be assigned to a reference of type Cloneable. The int array object referenced by the Cloneable reference c can be assigned to a
reference of the int array type.
10-d -Prints: 0, -1, 0, 65535 -The most negative value of type int is represented as 0x80000000 and becomes 0x0000 when converted to a
short or char. The most positive value of type int is represented as 0x7FFFFFFF and becomes 0xFFFF when converted to a short or char. Since
type short is a signed type the value 0xFFFF is interpreted as the two's compliment representation of negative one. Type char is unsigned so
0xFFFF is interpreted as the maximum positive value of type char. Please note that the difference between a signed value and an unsigned
value is the fact that the left most bit of a signed value is the sign bit. If the sign bit is set to one then the value is interpreted as a negative value
in two's compliment format.
11-b -Prints: 1,0 -The statement contains an addition operation. The left operand is a postfix operation and it is evaluated first. The result of the
postfix operation is zero, but variable i is incremented as a side effect. The right hand operand of the addition operation is the result of method

Certified Java Programmer Mock Exam 3


m. As a side effect, method m prints the current value of variable i which is one. Method m then returns the value zero. The result of the addition
operation is zero and that is the value that is assigned to variable i.
12-a -Prints: 1,2,3,4,1 -Java evaluates expressions from left to right while respecting operator precedence. j = 1 + (2 * 3) + 4 == 11
13-c -Prints: m3Cm2Bm1A -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is
both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is
more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
End of quote. In this case, the most specific version of method m3 is the one that declares a parameter of type C. The most specific version of
m2 is the one that declares a parameter of type B.
14-b -Prints: B -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, type B is more specific than type A because B extends A.
15-e -Compile-time error. -A method argument will not be implicitly narrowed from type double to type float.
16-g -Compiles and runs without error. -The compound assignment operators include an implicit cast to the type of the left hand operand.
Therefore, an explicit cast is not necessary.
17-c -Prints: -2, 3, 0, 2, 2, 5 -

Capítulo 3 – Operators and Assignments (D)


No.-Answer-Remark
1-f -Prints: fffffffe,ffffffff,7fffffff -The left shift operator << shifts each bit of the left operand to the left a distance specified by the right operand.
The right operand specifies the shift distance. For each bit position of the shift distance a bit that is set to zero is shifted into the least significant
bit. The least significant bit is the right most bit. The right shift operator >> shifts each bit of the left operand to the right a distance specified by
the right operand. The right operand specifies the shift distance. For each bit position of the shift distance a copy of the sign bit is shifted to the
right as each bit of the left operand is shifted to the right. As a result, the sign of the left operand remains unchanged by the shift operation. The
sign bit is the left most bit of the left operand. The unsigned right shift operator >>> shifts each bit of the left operand to the right a distance
specified by the right operand. The right operand specifies the shift distance. For each bit position of the shift distance a bit that is equal to zero
is shifted into the sign bit as each bit of the left operand is shifted to the right. Therefore, the sign of the result is always positive.
2-f -Prints: fffffffe,ffffffff,fffffffe -
3-d -Prints: 0.0,-0.0,-0.0 -If the result is not NaN, then the sign of the result is equal to the sign of the dividend.
4-j -Compiler error -The expression used to set the value of variable b3, "r1 instanceof Blue" causes a compiler error because the type of
variable r1, Red, can not be converted to type Blue. The expressions used to set variables b1 and b2 are legal. The first of the two would
produce a value of true and the second would produce a value of false.
5-d -Prints: true,true -The sign of a number is changed by inverting all of the bits and by adding one.
6-f -Prints: 3,0,3 -Java evaluates expressions from left to right while respecting operator precedence. The order of operator precedence
starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, the level
required for this question is required.
7-a -Prints: NaN,NaN -If either operand is NaN then the result is NaN.
8-a -Prints: 1 -Java evaluates expressions from left to right while respecting operator precedence. The "and" operator has higher precedence
than the "exclusive or" operator. The "or" operator has lower precedence. Although complete memorization of the operator precedence chart is
not necessary, the level required for this question is required. (1 | ((2 ^ (15 & 7)) ^ 13) | 2), (1 | ((2 ^ (7)) ^ 13) | 2), (1 | ((5) ^ 13) | 2), ((1 | 8 |
2)==11), (11%5=1)
9-b -Prints: false, false, true -Float values are converted to an integer before being converted to a short. The most negative value of type int is
represented as 0x80000000 and becomes 0x0000 when converted to a short. The most positive value of type int is represented as
0x7FFFFFFF and becomes 0xFFFF when converted to a short. NaN is converted to zero.
10-b -Prints: false true false -The equality operator has higher precedence than the logical operators; therefore, the left side, (a | b == c & d), is
evaluated as (a | (b == c) & d). Substituting the actual boolean values for the variable names produces (false | (false == true) & false) and
evaluates to false.
11-f -Compiler error. -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, no method is clearly more specific than all of the others.
12-f -Prints: true,false,true -The left operand of the "instanceof" operator must be a reference to an instance of an Object or subclass of Object.
The right operand of the "instanceof" operator must be an Object type. If the left operand is a reference to an instance of the type specified by
the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof
returns true.
13-e -Compiler error at line 7. -Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and
vice versa.
14-e -Prints: 10 -If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. If the
promoted type of the left hand operand is of type int, then the shift operator only uses the least significant five bits of the right hand operand and
the actual shift distance is always less than 32 bits. Therefore, if the right hand operand is 33 then the shift distance is one bit because the five
least significant bits are 00001. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are
used.
15-b -Prints: 1, 2, 3, 4, 11, -Java evaluates expressions from left to right. The first operator encountered is the subtraction operator. First, the
left operand is evaluated as one and then the right operand is evaluated as two and then the subtraction is performed resulting in a value of
negative one. The next operand encountered is the addition operator. The value of the left operand is the result of the previous operation, -1.
The value of the right operand will be the result of the expression m(3) * m(4) which is evaluated as 12. The addition operator adds the left hand
operand, -1, to the right hand operand, 12, to prodduce a value of 11.
16-a -Prints: -4 -If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int.
When a variable of type int with a value of 127 is shifted to the left two bits, the result is 508. However, the compound assignment operator
includes an implicit cast to the type of the left hand operand. E1 op= E2 is equivalent to E1=(T)((E1) op (E2)), where T is the type of the left
hand operand. Therefore, when 508 is cast to an eight bit byte, the result is negative four.
17-c -Prints: 1, 2, 3, 4, 9, -Java evaluates expressions from left to right. The first operator encountered is the addition operator. First, the left
operand is evaluated as 1. The value of the right operand will be the result of the expression m(2) % m(3) * m(4). When Java encounters the
remainder operator, "%", it first evaluates the left hand operand, 2, and then the right, 3. The result is 2. The next operator is the multiplication

Certified Java Programmer Mock Exam 4


operator. The left operand is the result of the previous operation, 2. The right operand is 4. The result is 8. Java then adds the left operand, 1, of
the addition operator to the value of the right operand, 8. The result is 9.

Capítulo 3 – Operators and Assignments (Opcionais)


No.-Answer-Remark
1-b -Prints: 2 -Java evaluates expressions from left to right while respecting operator precedence. (1 + 2 | 1 << 2 ^ 1 | 1), ((1 + 2) | ((1 << 2) ^
1) | 1), ((3) | ((1 << 2) ^ 1) | 1), ((3) | ((4) ^ 1) | 1), ((3) | (5) | 1 == 7), (7%5=2)
2-c -Prints: 3 -Java evaluates expressions from left to right while respecting operator precedence. (1 | 2 ^ 3 >> 2 & 14 + 2), (1 | (2 ^ ((3 >> 2) &
(14 + 2))), (1 | (2 ^ ((0) & (16))), (1 | (2 ^ (0)), (1 | 2), (3),
3-a -Prints: [a,0],[b,1],1,0,0,2 -The array index expression is evaluated before the right hand operand of the simple assignment operator.
Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the
operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a
String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero. As
a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand
operand of the simple assignment operator is the return value of method m. The input parameter to method m is the postfix increment
expression with variable i as the operand. The value of variable i is now one and that is the value that is passed into method m and that is the
value returned by method m and assigned to element zero of the array a. As a side effect of the postfix increment expression, the value of
variable i is two after method m is invoked.
4-c -Prints: 162 -The compound assignment operator is right associative so the expression is evaluated from right to left. The result is a String
value that can be calculated using the following expression. "1"+(2*(3+(4*(5+2))))
5-c -Prints: 1,2,3,4,5,3 -Java evaluates expressions from left to right while respecting operator precedence. (1 | (2 & (3 + (4 % 5)))), (1 | (2 & (3
+ (4)))), (1 | (2 & (7)))), (1 | 2)=3
6-b -Prints: [A,1],[B,1],[C,1],[D,2],2 -The statement contains a series of addition operations. The left operand of each addition operator is
evaluated completely before the right hand operand. The left hand operand of the first addition operator is the return value of method m1. A
prefix increment expression is passed to m1 as an input parameter. The result of the pretfix expression is the original value of i plus one. The
right hand operand of the addition operator is the return value of method m2. Method m2 prints the current value of i which is one and then m2
always returns zero. The result of the first addition operation becomes the left operand of the second addition operator. The right operand is the
return value of method m1. The input parameter of m1 is a postfix increment expression. The result of the postfix increment expression is the
existing value of i which is one and that is the value that is passed to m1 as the input parameter. The side effect of the postfix increment
expression is the addition of one to the value of i. Therefore, parameter i has the value of two after method m1 is invoked. The result of the
second addition operation is the value 2. The result of the second addition operation is the left hand operand of the third addition operation.
Since method m2 always returns zero, the third addition operation does not change the final result which is two.
7-b -Prints: 1,1,1,0,3 -The statement contains a series of addition operations. The left operand of each addition operator is evaluated
completely before the right hand operand. The left hand operand of the first addition operator is a postfix increment expression that increments
variable i. The result of the postfix expression is the original value of i. The side effect of the postfix operation is the addition of one to the value
of variable i. The right hand operand of the addition operator is the return value of method m. Method m prints the current value of i which is
one. Method m then adds the value of i to the value of x. The result is x equals one. Then m returns zero. The result of the addition operation is
the sum of the original value of i which is zero and the return value of m which is also zero. The left hand operand of the next addition operator
is the result of the first addition. The value of the right operand is the result of the postfix expression that increments the value of variable j. The
result of the addition operation is then the sum of zero and the original value of j which is also zero. The rest of the expression is evaluated
similarly. The final result of the statement is that the value of zero is assigned to variables i, j, and k. The value of x is sum of the side effects of
each postfix expression.
8-a -Prints: [a,0],[b,1],[c,2],2,2,2 -The question contains a series of simple assignment operators where all of the operands are array access
expressions. Starting from the left, the array index expression is evaluated before the right hand operand of the simple assignment operator.
Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the
operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a
String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero
and method m prints [a,0]. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is
evaluated. The right hand operand of the first simple assignment operator is evaluated next. The right hand operand is another array access
expression similar to the first. The only difference is that the value of variable i is now one greater than it was at the time when the previous
array access expression was evaluated. This time, method m returns one and prints [b,1]. The final array access expression is evaluated next.
Method m returns 2 and prints [c,2].
9-b -Prints: 1,2,3,1,2,3,1 -Java evaluates expressions from left to right while respecting operator precedence. (1 | (2 & (3 >> ((1 / 2) + 3)))), (1 |
(2 & (3 >> ((0) + 3)))), (1 | (2 & (3 >> (3)))), (1 | (2 & (0))), (1 | (0))=1
10-a -Prints: 2, 2, -3, -4, 8, -Integral primitives are stored in two's compliment format. To change a positive value into a negative value, invert
each bit and add one to the result. The "~" operator is the bitwise compliment operator. It inverts the value of each bit. For example, ~0=-1.
11-c -Prints: 1 -The following statements are all equivalent. i += ~i - -i * ++i + i-- % ++i * i++; Change the compound assignment operator to a
simple assignment operator. i = (int) ((i) + (~i - -i * ++i + i-- % ++i * i++)); Evaluate the unary operators. i = (int) ((1) + (-2 - (-1) * 2 + 2 % 2 * 2));
Evaluate the binary operators. i = (int) ((1) + ((-2 - ((-1) * 2)) + (2 % 2 * 2))); i = (int) ((1) + ((0) + (0)));

Capítulo 4 – Declarations and Access Control (A)


No.-Answer-Remark
1-d -Compiler error at line 15. -Both class A and B are declared in the same package. Therefore, class B has access to the public, protected,
and package access methods of class A.
2-e -Compiler Error -A static method can not access a non-static variable.
3-b c d -Compiler error at line 2. Compiler error at line 3. Compiler error at line 4. -Only one class in a source code file can be declared
public. The other classes may not be public. Therefore, the declarations for classes Basics2, Basics3, and Basics4 generate errors.
4-a b e f g h -final private static synchronized native strictfp -A final or private method can not be overridden and therefore can not be
abstract. An abstract method declaration provides no implementation of the method an all implementation details are left to the overriding
method in the subclass. Since the synchronized modifier specifies an implementation detail that can be ignored by an overriding subclass
method, it makes no sense to allow the use of the sychronized modifier in an abstract method declaration. The strictfp modifier in a superclass
declaration has no impact on an overriding method in a subclass. For that reason, it makes no sense to include the strictfp modifier in an
abstract method declaration.
5-e -Prints: 2,5,8 -Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an index of 0 so and index of one refers to the
second element of each array.
6-d e -Compiler error at line 18. Compiler error at line 19. -Class A and C are not declared in the same package; therefore, class C does not
have access to package access method m4. Since class C extends class A, class C does have access to the protected method, m2, of class A.

Certified Java Programmer Mock Exam 5


7-e -Prints: 2,5,9 -The line marked 1 declares three arrays, a2, a3 and a4 where the elements of the arrays are primitives of type int. The line
marked 2 declares an array, a1, where each element of a1 is an array of primitives. The elements of a1 are initialized with references to the
previously declared arrays.
8-i -volatile -A field might be shared between two or more threads. Each thread is allowed to maintain a working copy of the field. If the threads
do not reconcile the working copies then each might be working with a different value. The volatile modifier is used to force each thread to
reconcile its working copy of the field with the master copy in main memory. For more information, please see section 8.3.1.4 of the Java
Language Specification.
9-e f -Compiler Error at 3. Compiler Error at 4. -There are no compiler errors at 1 and 2 because the variables are compile time constants that
are assignable to type byte. There are compiler errors at 3 and 4 because the variables are not compile time constants and are not assignable
to type byte without an explicit cast. For more information, please see section 5.2 of the Java Language Specification for more information on
assignment conversions.
10-d e f -Compiler Error at 2. Compiler Error at 3. Compiler Error at 4. -There is a compiler error at 2 because the method is declared with a
void return type so the return statement is not permitted to return a value. There is a compiler error at 3 because the method is declared with an
int return type but the return statement does not return a value. There is a compiler error at 4 because method m2 is declared with a void return
type. Please see section 14.16 of the Java Language Specification for more information on the return statement.
11-e -None of the above. -An abstract method can override an abstract method. The overridding abstract method can be useful as a place for
comments. An abstract method can override a method that is not abstract. An abstract method declaration does provide a throws clause that
can be empty. The body of an abstract method is represented by a semicolon.
12-a d -class A extends Object. Compiler error at 3. -The constructor for class B and class C both invoke the constructor for A. The
constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B
anc C must declare Exception in their throws clause. A compile time error is generated at marker 3 because the constructor does not declare
Exception in the throws clause.
13-c d e -private protected public -Constructors are not inherited and can not be overridden. For that reason, there is no need to apply the
"final" modifier to a constructor declaration. For the same reason, an abstract constructor would be useless since it could never be implemented.
A constructor is used to create an instance of an object so it would make no sense to apply the "static" modifier in an attempt to disassociate the
constructor from an object instance. An object is not available to multiple threads during the construction process so the "synchronized" modifier
would not provide additional protection. Native constructors are not permitted because the JVM could not easily monitor the operation of a
native constructor. A constructor is made strictfp by declaring the class strictfp.
14-h i -Compiler Error at 4. Compiler Error at 5. -There are no compiler errors at 1, 2 or 3 because a widening conversion from type byte,
char, or short to type int is legal. There are compiler errors at 4 and 5 because a narrowing conversion is not a legal assignment conversion
without an explicit cast. For more information, please see section 5.2 of the Java Language Specification for more information on assignment
conversions.
15-a b c e -A static method is also known as a class method. A class method is not associated with a particular instance of the class. The
keyword "this" can not be used inside the body of a static method. A method that is not static is known as an instance method. -The keyword
"this" refers to the current instance of the class. Since a static method is not associated with an instance, the keyword "this" is not permitted in
the body of a static method.
16-b d -The compiler attempts to create a default constructor for class B. Compiler error at 2. -If no constructor is declared explicitly, then the
compiler will implicitly create a default constructor that accepts no parameters, has no throws clause, and invokes its superclass constructor.
Since class A has an explicitly declared constructor, the compiler will not create an implicit default constructor. Class B does not have an explicit
constructor declaration so the compiler attempts to create a default constructor. Since class A does not have a no parameter constructor, the
attempt by class B to invoke the no parameter constructor of A would fail. As a result, a compiler error is generated at marker 2.
17-a b c d e f -1 2 3 4 5 6 -A variable that is local to a method can not possibly be access from outside of the class so the access
modifiers are not useful and not legal. A variable that is local to a method can not possibly be part of the persistent state of an object so the
transient modifier is not useful and not legal. Local variables can not possible be shared between threads so the volatile modifier is not useful
and not legal. A local variable can be declared final to prevent its value from being assigned more than once. If the value of the variable needs
to be accessed from a local class or an anonymous class, then the local variable or method parameter must be declared final.
18-a c e f g -The value of a final field can not be assigned more than once. A final field that is not assigned a value at compile time is called
a blank final variable. A blank final variable that is static must be definitely assigned in a static initializer. At the end of the instance construction
process all blank final variables must be definitely assigned. A field can not be declared both final and volatile. -Static and non--static field
variables may be declared final. All final fields must be definitely assigned a value once and only once. If the final variable is not assigned a
value at compile time then it is called a blank final variable. All blank final static variables must be assigned in a static initializer. All blank non-
static variables must be assigned by the end of the instance construction process. A field is sometimes shared between threads. The volatile
modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final
then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in
main memory.
19-a -Prints: 2,3,4,0, -The array, a, is an array containing four arrays. The size of the first is 2, the second is 3, the third is four, and the fourth is
zero.

Capítulo 4 – Declarations and Access Control (B)


No.-Answer-Remark
1-d -Prints: 1, 1, 0, 1 -Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1
reference, the change is visible in the r2 instance of class Red.
2-c d e -Compiler error at line 3. Compiler error at line 4. Compiler error at line 5. -If a class C is declared as a member of an enclosing class
then C may be declared using no access modifier or any of the three access modifiers, private, protected, or public. However, if class C is not
declared as a member of an enclosing class then C may be declared with no access modifier or using the access modifier "public". The other
two access modifiers, private and protected, are not applicable to any class that is not a member class. The class declaration "Class Basics4"
generates a compiler error because all of the letters of the reserved word "class" must be lower case.
3-e -None of the Above -If a class is declare public, then the compiler will generate an error if the class is not stored in a file that has the same
name as the class plus the extension .java. In this case, none of the classes is declared with the public modifier. Therefore, all four classes may
be stored in the same file.
4-e -Compiler Error -The access modifiers public, protected, and private can not be applied to variables declared inside methods.
5-a b c d e f g j k -abstract final private protected public static synchronized native strictfp -An abstract method declaration provides
no method body. If one method is declared abstract then the entire class must be declared abstract and can not be instantiated. A final method
can not be overridden. The access modifiers, private, protected and public, can be applied to a method. A static method is associated with a
class but not a particular instance of the class. A thread can not enter a synchronized method without first acquiring a lock. The transient and
volatile modifiers are not applicable to methods. A native method is implemented in platform-dependent code.
6-a d f -If the superclass method is static, then any method with the same signature in a subclass must also be static. If the superclass
method is public, then the overriding method must also be public. If the superclass method is protected, then the overriding method must be

Certified Java Programmer Mock Exam 6


protected or public. -The modifiers synchronized, native and strictfp specify implementation details that a subclass is free to change. Similarly, a
subclass can override a concrete implementation of a method with an abstract declaration. A subclass method may not have weaker access
than a superclass method. For example, a public method may not be overridden by a private method. However, a subclass method can provide
greater access than a superclass method. For example, a protected method can be overridden by a public method.
7-b -Prints: 0,0,0.0,null -Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as
0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null.
8-c d e f g h -C D E F G H -The following modifiers can not be applied to a top level class: private, protected, static, synchronized,
transient, volatile. The synchronized modifier can not be applied to any class because it is a method modifier. The modifiers, transient and
volatile, can not be applied to any class; because they are field modifiers.
9-c d e -Compiler error at line 17. Compiler error at line 18. Compiler error at line 19. -Classes A and D are not declared in the same
package; therefore, class D does not have access to package access method m4. Since class D does not extend class A, class D does not
have access to the protected method, m2, of class A.
10-a b -The super class must have an accessible constructor that takes no arguments. The no-argument super class constructor must not
have a throws clause. -The default constructor takes no parameters and invokes the superclass constructor with no parameters. If the
superclass does not have an accessible no parameter constructor then a compile time error is generated. The default construct does not have a
throws clause. Consequently, a compile time error is generated if the no parameter constructor of the super class has a throws clause.
11-c f -3 6 -The strictfp modifier can be applied to a class or method but not to a field. The synchronized modifier is a method modifier.
12-b c d e f h i -final private protected public static transient volatile -The access modifiers, private, protected and public, can be applied
to a field. A final field can not have its value assigned more than once. A transient field is not part of the persistent state of an object. Transient
fields are not serialized. Fields that are shared between threads may be marked volatile to force each thread to reconcile its own working copy
of the field with the master copy stored in the main memory. The synchronized modifier and abstract modifier may be applied to methods but not
to fields.
13-b d e f -Instance variables declared in this class or any superclass. Instance methods declared in this class or any superclass. The
keyword "this". The keyword "super". -The superclass constructor invocation statement, super, is processed before the new instance of the
class has been created. For that reason, the arguments to the superclass constructor invocation statement can not legally refer to members that
have not been created and initialized.
14-a d -A constructor can invoke another constructor of the same class using the constructor invocation statement "this". A constructor can
invoke the constructor of the direct superclass using the superclass constructor invocation statement "super". -If a constructor invocation
statement appears in the body of the constructor then it must be the first statement. The same is true for a superclass constructor invocation
statement. A compile-time error is generated if a constructor attempts to invoke itself either directly or indirectly.
15-a b e -A final method can not be overridden. All methods declared in a final class are implicitly final. A machine-code generator can inline
the body of a final method. -All methods declared in a final class are implicitly final. It is permissible but not required that all such methods be
explicitly declared final. All private methods are implicitly final. It is permissible but not required that all private methods be explicitly declared
final. The body of an inline method is inserted directly into the code at the point where the method is invoked. If the method is invoked at 10
different points in the code then the body can be copied to all 10 points. Inline code runs very quickly because it removes the need to jump to
and from the method. If the method is excuted repeatedly in a loop then inlining can improve performance significantly. The machine-code
generator has the option to inline a final method.
16-b -2. -An array is declared by placing brackets after the identifer or after the type name. A compile-time error occurs at the line marked 2
because the brackets appear before the identifier for array a4.
17-a -Prints: 3,4,8 -The array, a, is an array containing 3 array objects. The first index references one of the three arrays. The second index
references one of the primitive int values.
18-b -Prints: A -This question demonstrates that a Java array is really an array where each element is another array.

Capítulo 4 – Declarations and Access Control (C)


No.-Answer-Remark
1-l m -Compiler error at line 12. Compiler error at line 13. -The following three rules are copied from the Java Language Specification. 1.
Comments do not nest. 2. /* and */ have no special meaning in comments that begin with //. 3. // has no special meaning in comments that begin
with /* or /**. The three rules have the following implications. Once a comment is started the compiler will ignore additional comment start
markers such as those associated with comments 2, 6, 7, 8, and 9. However, the compiler will not accept repeated comment end markers such
as those associated with comments 10, 11, and 12.
2-c -Compiler error at 3. -The compiler creates a constructor for class C implicitly. The implicitly created constructor accepts no parameters
and has no throws clause. The constructor for class B and class C both invoke the constructor for A. The constructor for class A declares
Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B anc C must declare Exception
in their throws clause. A compile time error is generated at marker 3 because the default constructor does not declare Exception in the throws
clause.
3-f -Compile-time Error. -A compile-error is the result of replacing the commas with semicolons in the declaration of the array.
4-f -Compile-time Error. -A compile-error is the result of replacing the curly braces with square braces in the declaration of the arrays.
5-a d f g -public abstract final strictfp -The access modifiers, protected and private, can be applied to a member class but not to a local
class or a top level class. The static modifier can be applied to a member class but not a local class or a top level class. The public modifier can
be applied to a top level class to allow the class to be access from outside of the package. The abstract modifier prevents the class from being
instantiated. An abstract class may include zero, one or more abstract methods. The final modifier prevents a class from being extended. The
strictfp modifier can be applied to a class, interface, or method to guarantee that all float or double operations are processed according to the
IEEE 754 standard. Without the strictfp modifier the float or double operations can be processed using the greatest precision available. For
example, a 64 bit CPU could process intermediate float operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or
double expressions within all nested classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be
strictfp.
6-e -Prints: 2,5,9 -Arrays a1, a2 and a3 are all arrays where each element is an array of primitives of type int. The size of each of the 3
primitive int arrays are different. The size of the first is 2, the second is 3, and the third is 4. The declarations of a1, a2, and a3 are different in
terms of the position of the square brackets, but the result of each is the same.
7-a e -1. 5. -An array creation expression must have either a dimension expression or an initializer. If both are present then a compile-error is
generated. Similarly, if neither is present then a compile-error is generated. If only the dimension expression is present then an array with the
specified dimension is created with all elements set to the default values. If only the initializer is present then an array will be created that has
the required dimensions to accommodate the values specified in the initializer. Java avoids the possibility of an incompatible dimension
expression and initializer by not allowing both to appear in the same array creation expression. A compile-time error is generated by the array
creation expression for a1 because it needs either a dimension expression or an initializer. A compile-time error is generated at 5 because either
the dimension expression or the initializer must be removed.
8-h -transient -A transient field is not part of the persistent state of an object and therefore is not serialized. Please see section 8.3.1.3 of the
Java Language Specification for more information.

Certified Java Programmer Mock Exam 7


9-a d e h -class A extends Object. The compiler will insert a default constructor implicitly. The implicit constructor declaration has no throws
clause. The implicitly declared constructor invokes the no-parameter constructor of the superclass. -Field i1 and m1 both have package
access. When no constructor is declared explicitly the compiler will insert one explicitly. The implicitly declared default construct will have the
same access privileges as the class. In this case, the class is public so the default constructor is also public. The defaul constructor accepts no
parameters and throws no exceptions.
10-c g -3 7 -The abstract modifier can be applied to a class and a method but not a field. A field is sometimes shared between threads. The
volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared
final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy
in main memory.
11-a -1. -A compile-time error occurs at the line marked 1 because it is not possible to use the type name to specify the dimensions of the
array.
12-d -Declare all constructors using the private access modifier. -If no constructors are declared explicitly then the compiler will create one
implicitly and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a default
constructor. If the explicit constructors are declared private then no code outside of the class will have access to the constructors and will not
have the ability to create an instance of the class. Constructor declarations do not include a return type so the void type can not be specified.
13-f -Compile-time Error. -A compile-error is the result of replacing the curly braces with parenthesis in the declaration of the array.
14-a b c -The compiler will create a default constructor if no other constructor is declared. The default constructor takes no parameters. The
default constructor invokes the superclass constructor with no arguments. -If no constructor is explicitly declared in a class, then the compiler
will implicitly insert a default constructor. The default constructor takes no parameters and invokes the superclass constructor with no
parameters. If the superclass does not have an accessible no parameter constructor then a compile time error is generated. The access
modifier implicitly assigned to the default constructor is the same as that assigned to the class. The default construct does not have a throws
clause. Consequently, a compile time error is generated if the no parameter constructor of the super class has a throws clause.
15-e -Prints: 2,3,5 -Arrays a1, a2 and a3 are all arrays where each element is an array of primitives of type int. The size of each of the 3
primitive int arrays are different. The size of the first is 2, the second is 3, and the third is 4. The declarations of a1, a2, and a3 are different in
terms of the position of the square brackets, but the result of each is the same.
16-c -Prints: 4 -The array, a, is an array containing four arrays so the length is 4.
17-k -Run-time Exception -Line 3 creates a three dimensional array where each element is a null reference to an object of type A. Array a3 is
actually a single dimensional array with three elements. Each of the three elements is a two dimensional array and the dimensions of each is
3X3. Line 7 replaces each 3X3 array with a 2X1 array, a2. Line eight attempts to access an element that is not available in the new 2X1 array.
18-b -Prints: 147258369 -If array index i had been the first index of the array, a1, then the output would have been 123456789. Since i is the
second index of the array the output is 147258369.

Capítulo 5 – Flow Control and Exception Handling (A)


No.-Answer-Remark
1-f -Compiler Error -The loop variable, h, is local to the "for" statement. The print statement causes a compiler error because it refers to the
local variable h. Always check for compiler errors due to the use of out-of-scope variables.
2-d -Prints: ABCBCC -There are no break statements for the first case falls through to the second case and the second case falls through to
the third. There is no default case so no case statement is processed when the switch expression is 3.
3-b -Prints: 1,0,1,0,0,1 -No exception is thrown so a is incremented. Both of the finally blocks are then executed.
4-f -Compiler Error -A compile-time error is generated because the second catch clause attempts to catch an exception that is never thrown in
the try block.
5-a e f -Compiler error at line 2. Compiler error at line 6. Compiler error at line 7. -All of the exceptions in this question are a subclass of
Exception and three are a subclass of RuntimeException. Any exception that is a subclass of RuntimeException should not be caught. Instead,
it should be allowed to bubble to the top of the program where the runtime system will display a stack trace. RuntimeExceptions are a result of
program bugs. A RuntimeException should not be listed in the throws clause of a program. Methods m1, m5, and m6 throw subclasses of
Exception that are not subclasses of RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be
declared in the throws clause of the method.
6-b c e -Compiler error at line 3. Compiler error at line 4. Compiler error at line 6. -All of the exceptions in this question are a subclass of
Exception and three are a subclass of RuntimeException. Any exception that is a subclass of RuntimeException should not be caught. Instead,
it should be allowed to bubble to the top of the program where the runtime system will display a stack trace. RuntimeExceptions are a result of
program bugs. A RuntimeException should not be listed in the throws clause of a program. Methods m2, m3, and m5 throw subclasses of
Exception that are not subclasses of RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be
declared in the throws clause of the method.
7-a -Prints: 0,0,0,1,1 -The first catch block is able to catch a Level3Exception or any subclass of Level3Exception. The second catch block is
able to catch a Level2Exception or any subclass of Level2Exception. The third catch block is the only one that is able to catch a
Level1Exception. The finally block is also executed.
8-b -Prints: 0,0,1,1,0,1 -The nested catch block is able to catch a Level2Exception or any subclass of it. The Level1Exception is not a subclass
of Level2Exception so it is caught by the second of the two outer catch blocks. Both finally blocks are executed.
9-a b c d e f -Compiler error at line 2. Compiler error at line 3. Compiler error at line 4. Compiler error at line 5. Compiler error at line 6.
Compiler error at line 7. -Methods m1(), m2(), m3(), m4(), m5(), and m6() throw subclasses of Exception that are not subclasses of
RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be declared in the throws clause of the
method.
10-b -Prints: false,true -Error is a direct subclass of Throwable. RuntimeException is a direct subclass of Exception.
11-c -Prints: v w x x y z z -Cases one and three have no break statement, so the next case is also executed and x and z are printed twice.
12-d -Prints: true,true -Both Error and Exception are subclasses of Throwable.
13-f -Compiler error -The Java Language Specification states that "Every case constant expression associated with a switch statement must
be assignable to the type of the switch expression." The constant expression 1000 is beyond the range of type byte.
14-c -Prints: 1433 -The initial value of X is -5. On the first pass through the loop the default case of the switch statement is invoked and 2 is
added to the value of x. The new value is -3. On the second pass, the default case of the switch statement is invoked again and two is added to
the value of x. The new value is negative 1. On the third pass 2 is added again and the new value of x is positive 1. After that, the value of x is
printed on each pass through the loop. On the last two passes through the loop the value of x is 3.
15-c -Prints: i1k1i2j1i3j2 -A while loop is nested inside of a do loop. The while loop iterates twice during the first iteration of the do loop. The
body of the while loop does not excute during the final two iterations of the do loop.
16-f -Prints: 8,4 -While j is greater than or equal to seven the variable, i, is incremented twice with each pass through the loop. When j is less
than seven the continue statement causes the first increment of variable i to be skipped. The continue statement causes control to go directly to
the loop expression that causes i to be incremented after checking to see that i is less than seven. When i reaches eight the loop expression is
false and the loop is terminated.

Certified Java Programmer Mock Exam 8


17-h -Prints: 121212 -Although a program such as this will probably never be found in the real world, something similar is likely to be found on
the real exam. This trick question has a for-loop nested inside of a do-loop. The body of each loop is a single statement rather than a block. The
best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables
involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows:
(1,0)(2,0)(1,1)(2,1)(1,2)(2,2).
18-c -Prints: 6,6 -The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass.
The loop terminates when i reaches six.
19-f -Prints: 1112 -Although a program such as this will probably never be found in the real world, something similar is likely to be found on the
real exam. This trick question has a do-loop nested inside of a for-loop. The body of each loop is a single statement rather than a block. The
best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables
involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows:
(1,0)(1,1)(1,2)(2,3).

Capítulo 5 – Flow Control and Exception Handling (B)


No.-Answer-Remark
1-g -None of the Above -Methods m1(), m2(), m3(), m4(), m5(), and m6() throw subclasses of RuntimeException. Any exception that is a direct
subclass of RuntimeException should not be caught and should not be declared in the throws clause of a method.
2-b -Prints: 0,1,1,0,0,1 -The nested catch block is able to catch a Level2Exception or any subclass of it causing b to be incremented. Both of
the finally blocks are then executed.
3-d -Prints: D -This is a trick question. The expression (b = false) appears to be testing the value of b, but it is really setting the value of b.
Notice that the equality operator has been replaced by the simple assignment operator. Anytime you think you see the equality operator on the
exam make sure that it has not been replaced by the assignment operator.
4-f -Compiler Error -The throws clause of White.m1 declares a ColorException, but the catch clause in the main method catches only a
subclass of ColorException. The result is a compiler error.
5-d -Prints: 0,1,1 -An exception is thrown so variable a is not incremented. Although Color.m1 declares a ColorException in the throws clause,
a subclass of Color is free to declare only a subclass of Color in the throws clause of the overriding method.
6-d -Prints: 0,0,1,0,1,1 -The nested catch block is able to catch a Level2Exception or any subclass of it, but Exception is not a subclass of
Level2Exception. The Exception is caught by the second of the two outer catch blocks causing f to be incremented. Both of the finally blocks are
then executed.
7-c -Prints: C -This is a trick question. The boolean b is initialized to true, but the first if statement sets b to false. Notice that the equality
operator has been replaced by the simple assignment operator. Anytime you think you see the equality operator on the exam make sure that it
has not been replaced by the assignment operator.
8-f -Compiler Error -The throws clause of White.m2 declares a WhiteException, so the body of m2 may throw a WhiteException or any
subclass of WhiteException. Instead, the body of m2 throws a superclass of WhiteException. The result is a compiler error.
9-e -Prints: 0,0,1,0,1 -The first catch block is able to catch a Level3Exception or any subclass of Level3Exception. The second catch block is
able to catch a Level2Exception or any subclass of Level2Exception, so variable c is incremented. The finally block is also executed.
10-c -Prints: 0,1,1,0,0,1 -The nested catch block is able to catch a Level2Exception or any subclass of it causing b to be incremented. Both of
the finally blocks are then executed.
11-g -Prints: 1,0,0,0,1 -No exception is thrown so variable a is incremented. The finally block is also executed.
12-c -Prints: C -This is a trick question. It appears that a compiler error would be generated as a result of attempting to use the value of
variable b before it is initialized. In reality, the first if statement does the initialization of b. The expression (b = false) appears to be testing the
value of b, but it is really setting the value of b. Notice that the equality operator has been replaced by the simple assignment operator. Anytime
you think you see the equality operator on the exam make sure that it has not been replaced by the assignment operator.
13-d -Prints: 61433 -On the first pass through the loop the value of x is 6 so 5 is subtracted from x. On the second pass through the loop the
value of x is 1 so 3 is added to the value of x. On the third pass through the loop the value of x is 4 so 1 is subtracted from the value of x. On the
fourth pass through the loop the value of x is 3 so the variable, success, is incremented from zero to one. On the final pass the value of x is 3
and the variable, success, is incremented again to the value of 2. The boolean expression of the do while loop is now false so control flows out
of the loop.
14-f -Compiler Error -A throw statement is the first statement in the outer try block. The switch statement that appears next is unreachable and
generates a compile-time error.
15-d -Prints: 012345 -Although a program such as this will probably never be found in the real world, something similar is likely to be found on
the real exam. This trick question has a do-loop nested inside of a while-loop. The body of each loop is a single statement rather than a block.
The best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables
involved in the loop. The printed results will provide the best possible explanation. The values of i and j for each iteration are as follows:
(1,0)(1,1)(1,2)(1,3)(2,4)(3,5) .
16-e -Prints: 9,4 -The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass.
The if statement causes the loop terminates when i reaches six.
17-b -Prints: 012 -Although a program such as this will probably never be found in the real world, something similar is likely to be found on the
real exam. This trick question has a while-loop nested inside of a do-loop. The body of each loop is a single statement rather than a block. The
best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables
involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows:
(1,0,0)(2,0,1)(3,0,2).
18-c -Prints: 3,1 -On the first iteration case 1 is processed and control goes directly to the top of the for statement. The boolean expression of
the do-loop is not processed. On the second iteration case 2 is processed and the break statement causes the switch statement to complete
and control goes to the boolean expression of the do-loop. On the third iteration case 3 is processed and the break label2 statement cause the
do-loop to complete abruptly without processing the boolean loop expression so j is not incremented. On the fourth iteration case 4 is processed
and the break label 1 statement causes the outer loop to complete abruptly.
19-b -Prints: 1,3,2 -The case 2 statement is processed on the first iteration followed by case 4 and then the default case. Case 2 causes the
switch statement to complete. Case 4 processes the continue label2 statement which causes control to transfer to the boolean expression of the
do-loop. The default case causes control to transfer out of the outer for-loop.

Capítulo 5 – Flow Control and Exception Handling (Opcionais)


No.-Answer-Remark
1-c -Prints: v y w z -The legal types for the switch expression are byte, short, char, and int. The constant case expressions can be any
expression that is assignable to the type of the switch expression. 'd'-'a'=3. ~0=-1. 4&5=4.
2-f -Compiler Error -The switch expression can not be of type long. The legal types for the switch expression are byte, short, char, and int.
3-a -Prints: 12457 -Since each iteration of the loop prints the value of the switch expression, it should be possible to figure out what is going on
here. If more information is needed, then change the print statement so that the values of i and j are printed separately in columns.
Certified Java Programmer Mock Exam 9
4-g -Compiler Error -In the C programming language, the boolean value of false is represented by zero and true is any non zero value. In the
Java programming language, numeric values are not assignable to a boolean value. The use of the numeric value 1 as the boolean expression
in the while loop causes a compiler error.
5-e -Prints: 2, 1, 3, 1 -A break statement with a label will transfer control outside of the enclosing statement with the matching label.
Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not
evaluated as control transfers out of the loop. However, if the break statement transfers control out of a try-catch-finally statement, then the
finally clause is processed.
6-d -Prints: 3, 5, 9, -A continue statement without a label will cause the current iteration of the enclosing loop to end and the next iteration to
begin. As a result, the boolean expression that controls the loop is evaluated as the next iteration begins. Similarly, a continue statement that
has a label transfers control to the next iteration of the enclosing loop that has the same label. If the continue statement causes control to break
out of a nested loop to transfer control to an outer loop with a matching label, then execution of the nested loop is completed abruptly as control
transfers to the outer loop. Furthermore, the boolean expression that controls the nested loop is not evaluated as control is abruptly transferred
to the labeled outer loop.
7-d -Prints: 3, 6, 12, -A break statement with a label will transfer control outside of the enclosing statement with the matching label.
Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not
evaluated as control transfers out of the loop. However, if the break statement transfers control out of a try-catch-finally block, then the finally
clause is processed. The first iteration of this code example executes the case statement with the value 3. As a result, the statement "break
label3" is executed causing the inner most loop to complete abruptly and control to be transferred out of the inner most loop to the second
nested loop. The boolean expression of the second loop is evaluted and variable j is incremented as a side effect. The boolean expression
evalutes as true and a second iteration begins. Variable j is incremented again at the top of the loop and varible i is incremented at the top of the
inner most loop. The switch expression evaluates as six. A continue statement without a label will cause the current iteration of the enclosing
loop to end and the next iteration to begin. As a result, the boolean expression that controls the loop is evaluated as the next iteration begins.
Similarly, a continue statement that has a label transfers control to the next iteration of the enclosing loop that has the same label. If the
continue statement causes control to break out of a nested loop to transfer control to an outer loop with a matching label, then execution of the
nested loop is completed abruptly as control transfers to the outer loop. Furthermore, the boolean expression that controls the nested loop is not
evaluated as control is abruptly transferred to the labeled outer loop.

Capítulo 6 – Object Oriented Programming (A)


No.-Answer-Remark
1-f -Compiler Error -The static main method can not access the non-static method m.
2-b -Prints: 2,3 -Primitive type variables are passed to methods by value. In other words, only the value of the variable is passed to the method
so the method has no access to the variable itself. If the method modifies a primitive value that has been passed to the method as a parameter,
then the modification does not impact the variable outside of the method. Instead, only the value of the local copy is modified. For that reason,
the method m does not change the value of the variable y in the main method. However, method m does indeed have direct access to the
member variable x and its value is modified by method m and the modification is seen within the main method.
3-a b c d -1 2 3 4 -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can
legally be applied to a method declaration in an interface the usage is redundant and is discouraged. The final, synchronized, native, and strictfp
modifiers can not appear in the declaration of an abstract method and can not be applied to an abstract method declared in an interface.
4-a e -abstract public -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers
can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. An abstract method can not also be
declared private, static, final, native, strictfp, or synchronized so the same restriction applies to methods declared within an interface.
5-e -None of the above. -All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but
legal. No other modifiers can be applied to a field declaration within an interface.
6-a -Prints: float,float -An argument of type long can be widened to a float or a double. Since the float type is more specific than a double the
float type is selected before double.
7-d -Runtime error at line 10. -The compiler accepts the explicit cast at line 10. However, Base is not a subclass of Sub; therefore, a runtime
exception is thrown.
8-a -Prints: AAA -Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C
overloads method m1 with a third implementation. Even though c1, c2 and c3 are all instances of class C, the reference type is always A so the
overload methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared in class A is
always invoked.
9-f -Runtime error at line 10. -The object referenced by obj is of type "Base[]", and can not be cast to type "Sub[]".
10-b -Prints: P.printS1 Q.printS2 -Static method Q.printS1 hides the static method P.printS1 in the super class P. Instance method Q.printS2
overrides the instance method P.printS2. Due the the differences between the hiding of static methods and the overridding of instance methods
the invocation of the two methods in P.printS1S2 produces different results. The method invocation expression printS1 results in the invocation
of the hidden super class method P.printS1. The method invocation expression printS2 results in the invocation of the overridding sub class
method Q.printS2.
11-b -Prints: ABC -One of the overloaded methods is selected based on the type of the argument. The type of the argument is determined by
the reference type. The type of c1 is A. The type of c2 is B. The type of c3 is C.
12-b -Prints: BBABBA -The first method call, m(null,null), calls method m with two null literals that have type null. Both null literals are promoted
to type B because B is more specific than type A. As a result, m(B x, B y) is invoked. The second method call, m(a=null,b=null), calls m with a
first parameter of type A and a second parameter of type B. As a result, m(A x, B y) is invoked because the parameter types are matched
exactly. The third method call invokes m(B x, A y) because the parameter types match exactly.
13-f -Compiles and runs without error. -Although line 12 appears to be the source of a compiler error it is not. In line 12, the reference named
base actually refers to an object of type Sub. Therefore, the reference to the object may be cast to type Sub.
14-b -Prints: I10.s10,I20.s20,I20 -This is a trick question. Class C20 inherits ambiguous declarations of the "name" field. As long as the field is
not referenced as a member of class C20, then no compiler error occurs. Although line 13 may appear to generate the compiler error it does
not, because "name" is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.
15-e -Compile-time error. -A method argument will not be implicitly narrowed from type double to type float.
16-f -Compiler Error -The two-parameter constructor of U does not invoke the two-parameter constructor of T. Therefore, the constructor of U
will try to invoke a no-parameter constructor of T, but none exists.
17-a -Prints: AAA -Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C
overloads method m1 with a third implementation. Even though b1 is an instance of class B and c1 is an instance of class C the reference type
is always A so the overload methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared
in class A is always invoked.
18-c -Compiler Error. -There is no A.m2 method so polymorphism can not be used to invoke B.m2 using a reference of type A. A compiler error
is generated by the attempt to invoke method a1.m2 without first casting reference a1 to type B. Method B.m2 could be invoked by first casting
reference a1 to type B as follows: ((B)a1).m1().

Certified Java Programmer Mock Exam 10


Capítulo 6 – Object Oriented Programming (B)
No.-Answer-Remark
1-a -Prints: main,B.m1 -The no parameter version of B.m1 overrides A.m1. Therefore, B.m1 must comply with the rules of method overriding.
The single parameter version of B.m1 overloads the m1 method and therefore must comply with the rules of method overloading. For example,
one version of the overloaded method may be an instance method while the other is a static method.
2-c -Compile-time error. -Fields declared in an interface are implicitly public, final, and static. A compile-time error is generated in response to
the attempt to increment the value of i.
3-c f -final public -All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal.
Although "const" is a Java keyword it is not currently used by the Java programming language. An interface member can never be private or
protected.
4-c -Compile-time error. -In the declarations for interface B the keyword extends has been replaced by the keyword implements.
5-b -Prints: ABC -In all three cases, the object passed to method m1 is an instance of class C; however, the type of the reference is different
for each method. Since member variables are accessed based on the type of the reference, the value printed by each method is different even
though the same object instance is used for each method invocation.
6-c -Prints: F.printS1 E.printS2 -An instance method can override a superclass method, but a static method does not override a superclass
method. Instead, a static method can hide a superclass method. The static method that is accessed will depend on the type of the reference.
7-c -Prints: CBA -The method, m, is static; therefore, the method that is actually invoked at runtime is based on the type of the reference and
not based on the type of the object instance at runtime. When the object reference is of type A, then the method from A is invoked even though
the type of the object instance at runtime is C.
8-a -Prints: AAA -Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C
overloads method m1 with a third implementation. Reference c2 refers to an object of type A so only the A.m1 implementation is accessible.
9-i -Compiles and runs without error. -Although the reference named base is of type Base, the object that it refers to is of type Sub. Therefore,
the object may be cast to type Sub. Since objects of type Sub implement both interfaces, I1 and I2, the Sub type objects can be assigned to
references of type I1 and I2 without an explicit cast.
10-b -Prints: String -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, the String type is more specific than the Object type because String extends Object.
11-d -Prints: BB -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, B is more specific than A because B extends A. The method m(B b, B b) is more specific than the others because both
parameters are the most specific.
12-f -None of the above. -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers
can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an
interface are implicitly public a weaker access level can not be declared for an implementing method. The final, synchronized, native, and
strictfp modifiers can not appear in the declaration of an abstract method but they can be added to an implementation of an abstract method.
13-b -Prints: ABC -One of the overloaded methods is selected based on the type of the argument. The type of the argument is determined by
the reference type. The type of c1 is A. The type of c2 is B. The type of c3 is C.
14-a b c d -A Cat object inherits Fur and four Legs from the Dog super class. A Cat object is able to sleep and eat. A Cat object is able to
climb a tree. The relationship between Dog and Pet is an example of an appropriate use of inheritance. -An appropriate inheritance relationship
includes a sub class that "is-a" special kind of the super class. The relationship between the Dog subclass and the Pet superclass is an example
of an appropriate inheritance relationship because a Dog "is-a" Pet. The relationship between the Cat sub class and the Dog superclass is not
an example of an appropriate use of inheritance because a Cat is not a Dog.
15-a -Prints: float,float -An argument of type char can be widened to an int, long, float or double. An argument of type long can be widened to a
float or a double. Since the float type is the most specific type available here it selected before double.
16-g -Compiler error at line 10. -The reference "base" is of type "Base[]", therefore an explicit cast to type "Base[]" is required.
17-c -Compiler error. -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, both the String class and class A are more specific than type Object because both extend Object. However, String and A are
equally specific because one does not extend the other. The ambiguity generates a compiler error.

Capítulo 6 – Object Oriented Programming (C)


No.-Answer-Remark
1-g -None of the above. -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers
can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. An abstract method can not also be
declared private, static, final, native, strictfp, or synchronized so the same restriction applies to methods declared within an interface. Transient
and volatile are not method modifiers.
2-a -static -All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. A field
that is declared final can not also be declared volatile so a field of an interface can not be declared volatile. The synchronized and strictfp
modifiers are never applicable to a field.
3-e -strictfp -The private, protected, and static modifiers are applicable to member interfaces only. The synchronized modifier is applicable only
to concrete implementations of methods. The transient and volatile modifiers are applicable only to variables that are members of a class. The
strictfp modifier causes all float and double expressions within the interface declaration to be FP-strict. Please note that the abstract methods
declared within the interface are never strictfp.
4-c d -3 4 -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be
applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an interface are
implicitly public a weaker access level can not be declared.
5-c d e -3 4 5 -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can
legally be applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an
interface are implicitly public a weaker access level can not be declared.
6-a -Prints: ABCI -The field access expression ((X)this).identifier can be used to access hidden fields that are visible within the super class or
interface X.

Certified Java Programmer Mock Exam 11


7-h -Compiler error at line 13. -Class C10 inherits ambiguous declarations of the "name" field. As long as the field is not referenced as a
member of class C10, then no compiler error occurs. Line 13 generates the compiler error, because it is the first to access the "name" field as a
member of class C10.
8-f -Compiler error. -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, no method is clearly more specific than all of the others.
9-c -Prints: m3Cm2Bm1A -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is
both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is
more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
End of quote. In this case, the most specific version of method m3 is the one that declares a parameter of type C. The most specific version of
m2 is the one that declares a parameter of type B.
10-a -Prints: M.printS1,M.s1 N.printS1,N.s1 -A static method may hide a method in the super class, but a static method does not override a
super class method.
11-b -Prints: B -Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both
accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The
Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more
specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of
quote. In this case, type B is more specific than type A because B extends A.
12-b -Prints: DE -The instance variable referred to by x.s1 depends on the type of the reference at compile time. The instance method referred
to by x.getS1() depends on the type of the object at runtime.
13-f -Prints: H.printS1,null -The default constructor of H calls the constructor of G. The constructor of G calls the overridden method printS1
causing the invocation of H.printS1. The instance variable H.s1 hides G.s1. Although G.s1 has already been initialized, the initialization of the
instance variables of H won't begin until the initialization and construction of G is complete.
14-f -Compiler error -An instance method can not override a static method.
15-b -Prints: DDDD -Instance method D.m1 overrides the superclass methods and is therefore always invoked in place of the superclass
methods even if the reference is cast to the superclass type. The instance method that is invoked depends on the type of the object and not the
type of the reference.
16-e -System.out.print(new A.B().m1()); -A class nested within an enclosing interface can legally implement the enclosing interface. It is not
possible to create an instance of an interface so the syntax new A().new B().m1() is not applicable. A class declared within an interface is
implicitly static so an instance of the nested class can be created without first making a futile attempt to create an instance of the enclosing
interface. The fully qualified name of class B is A.B so an instance of A.B can be created using the expression new A.B().
17-b -Prints: D,C,B,A -Inherited fields hide super class fields but do not override super class fields. The field that is accessed at run time
depends on the type of the reference.

Capítulo 6 – Object Oriented Programming (D)


No.-Answer-Remark
1-c d -Compiler error at line 3. Compiler error at line 4. -Methods declared in an interface are implicitly public. A compiler error is generated if
an attempt is made to declare a method with a weaker access privilige. If no access modifier is included in the method declaration then the
method is implicitly public.
2-a b c d -1 2 3 4 -All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but
legal. No other modifiers can be applied to a field declaration within an interface.
3-a g -abstract public -All interfaces are implicitly abstract. The application of the abstract modifier to an interface is discouraged. The private,
protected, and static modifiers are applicable to member interfaces only. An interface can not be final. The keywords "extends" and
"implements" are not modifiers.
4-c d e -Compiler error at line 3. Compiler error at line 4. Compiler error at line 5. -A method declared in an interface is implicitly public. Any
class that implements the interface must not assign weaker access privileges.
5-b c -The relationship between a class and its super class is an example of an "is-a" relationship. The relationship between a class and a field
within the class is an example of a "has-a" relationship. -Inheritance is an example of an "is-a" relationship because the sub class "is-a"
specialized type of the super class. The relationship between a class and a field within the class is an example of a "has-a" relationship because
the class "has-a" instance of the declared field.
6-b c d -2 3 4 -All methods declared within an interface are implicitly abstract. The final, synchronized, native, and strictfp modifiers can not
appear in the declaration of an abstract method and can not be applied to an abstract method declared in an interface.
7-a b c d -An interface that is declared within the body of a class or interface is known as a nested interface. A constant can be a member of
an interface. An abstract method declaration can be a member of an interface. A class declaration can be a member of an interface. -An
interface can be declared within an enclosing class or interface. The members of an interface can be constants, abstract method declarations,
class declarations, or interface declarations.
8-a d e f g -public abstract static final strictfp -A class that is declared within an enclosing interface is implicitly public and static so the
access modifiers protected and private are not permitted.
9-a e -static strictfp -The private, protected, and static modifiers are applicable to member interfaces only. The synchronized modifier is
applicable only to concrete implementations of methods. The volatile and transient modifiers are only applicable to variables that are members
of a class. The strictfp modifier causes all float and double expressions within the interface declaration to be FP-strict. Please note that the
abstract methods declared within the interface are never strictfp.
10-h -None of the Above -Overloaded methods may have different return types and a different throws clause.
11-a e f g -abstract private protected public -All interfaces are implicitly abstract. The explicit application of the abstract modifier to an
interface is redundant and discouraged. The private, protected, and static modifiers are applicable to member interfaces only. The "final"
modifier is never applicable to an interface. The "extends" and "implements" keywords are not modifiers.
12-a -An interface declaration can be a member of an interface. -An interface can be declared within an enclosing class or interface. The
members of an interface can be constants, abstract method declarations, class declarations, or interface declarations. The body of a method
declared within an interface is represented by a semicolon. An interface can extend another interface but can not implement an interface. An
abstract class that implements an interface need not provide implementations for any of the methods declared within the interface.
13-b -Prints: R.printS1 S.printS2 -Method R.printS1 is private and therefore is not inherited by class S and is not overridden by method
S.printS1. In contrast, method R.printS2 is protected and therefore is inherited by class S and is overridden by method S.printS2. R.printS1S2
therefore calls the R.printS1 and S.printS2 methods.
14-b -Prints: B.s1 A.s2 A.s1 A.s2 -The variables of a subclass can hide the variables of a superclass or interface. The variable that is accessed
depends on the type of the reference.

Certified Java Programmer Mock Exam 12


15-b -2 -All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be
applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an interface are
implicitly public a weaker access level can not be declared for an implementing method. An abstract class that implements an interface is free to
override one of the interface methods with another abstract method declaration.
16-f -System.out.print(new C().new D().m1()); -A class nested within an enclosing interface can legally implement the enclosing interface.
Class C implements interface A and inherits all of the declarations within A including the declaration of the nested class B. Class D extends the
nested class B. Class D is not static and therefore can not be instantiated without being associated with an instance of the enclosing class C.
The following expression first creates an instance of the enclosing class C and then creates an instance of the inner class D: new C().new D().
17-b -Prints: B,SuperB,A,SuperA -The expression A.this.s1 is an example of a "qualified this" expression. The expression A.super.s1 is the
same as ((SuperA)A.this).s1.

Capítulo 7 – Inner Classes (A)


No.-Answer-Remark
1-b -2 -While the reference to an instance method is resolved at run-time based on the type of the referenced object; the reference to a static
method is resolved at compile-time based on the type of the reference. Suppose the existence of an abstract superclass A and a concrete
subclass C. Suppose that a reference, r, of type A refers to a concrete instance of type C. If r is used to invoke an instance method then the
reference will resolve to the overriding implemenation in the subclass C. The same is not true for static methods. Since the reference to a static
method is resolved at compile-time based on the type of the reference, the actual method referenced will be the static method in the superclass
and not the subclass method. If a method could be declared both abstract and static, then the actual method referenced at run-time could be the
abstract superclass method that provides no implementation. A nested class, however, can be declared both abstract and static because the
abstract nested class can be extended by another class.
2-c -Compile-time error. -Semicolons are needed at the end of the declarations of the anonymous classes.
3-c d e f g h i -3 4 5 6 7 8 9 -The following identifiers can not be applied to a top level class: private, protected, static, synchronized,
transient, volatile. The synchronized modifier can not be applied to a class because it is a method modifier. The modifiers, transient and volatile,
can not be applied to any class; because they are field modifiers.
4-a -Prints: 5 -The argument in the class instance creation expression is passed to the constructor of the superclass. An anonymous class can
not declare a constructor, but it can declare an instance initializer. In this case, the instance initializer invokes the incY method.
5-b -Prints: 3122 -The arguments in the class instance creation expression are passed to the constructor of the superclass.
6-b c d -2 3 4 -Synchronized is a method modifier but is not a class modifier. Any attempt to declare a synchronized class will fail. Since the
synchronized modifier specifies an implementation detail it makes no sense to use it with an abstract method that provides no implementation.
7-a c e -A local class is declared within a block. A local class is a nested class. A local class is always a named class. -Every class declared
within the body of another class is known as a nested class. If the nested class does not have a name then it is an anonymous class. If the
nested class has a name then it is not anonymous. If the nested class has a name and is declared inside of a block such as the body of a
method, constructor, initializer, or statement then it is a local class. If a nested class does not have a name then it can not be called a local class
even if it is declared inside of a block. Therefore, an anonymous class is never called a local class. If the nested class has a name and is not
declared inside of a block then it is a member class. If the class is not nested then it is a top level class. Chapter 8 of the Java Language
Specification defines class declarations. Section 8.1.2 of the JLS defines inner classes. Section 8.5 of the JLS defines member classes. Section
14.3 of the JLS defines local class declarations. Section 15.9.5 of the JLS defines anonymous class declarations.
8-e -None of the Above -A method, field, and a nested class can share the same name because they are used in different contexts and use
different lookup procedures. Please note, however, that class names usually begin with an upper case letter while method and field names
usually begin with a lower case letter. Also note that a nested class can not share the same name with its enclosing class. However, a method
and field can share a name with the enclosing class. Even so, it not a good idea to name a method with the name of the enclosing class
because it could be confused with a constructor.
9-c -Compile-time error. -An anonymous class can extend a superclass or it can implement an interface. However, an anonymous class
declaration that extends a superclass is not permitted to have an implements clause.
10-c e -3 5 -Local method variables and method parameters are stored on the stack and go out of scope after the method is exited. Although
a local reference to an object is also stored on the stack, the object itself is stored on the heap. If the reference to the local object is maintained
outside of the method then the locally created object can continue to exist long after the method returns. Since the local or anonymous object
can continue to exist longer than local method variables, the local or anonymous object is not permitted to access local method variables unless
the variable is declared final and is assigned a value before the declaration of the local or anonymous class. The JVM can place a copy of the
value of the final variable directly into the local or anonymous class so there is no further dependence on the method variable.
11-d f g -abstract final strictfp -A local class is a named class that is declared inside of a block. No access modifier can be applied to a local
class declaration. A local class can not be static. A local class can be abstract and can be final. The strictfp modifier can be applied to a class,
interface, or method to guarantee that all float or double operations are processed according to the IEEE 754 standard. Without the strictfp
modifier the float or double operations can be processed using the greatest precision available. For example, a 64 bit CPU could process
intermediate float operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or double expressions within all nested
classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be strictfp.
12-f -Compiler Error -Class Z is a non-static member class of class D. Since Z is non-static, an instance of Z can not be instantiated without
first creating an instance of class Z. Therefore, the correct syntax for creating an instance of class Z would be as follows: "new D().new Z()".
13-b -Prints: Z -Class Z is a static member class of class E. Static member classes are similar to ordinary top-level classes with the added
advantage of having access to all of the static fields and methods (including those that are private) of the enclosing class. Since Z can be
instantiated without first creating an instance of the enclosing class, only the letter "Z" is printed.
14-d -An anonymous class is implicitly final. -An anonymous class can extend another class or implement an interface. If the anonymous class
extends another class then the class instance creation expression can include parameters. The parameters will be passed to a constructor of
the super class. If the anonymous class implements an interface, then the instance creation expression can not include any parameters. An
anonymous class can not be extended so it also can not be abstract. An anonymous class can not be static. An anonymous class can not
declare a constructor, but it can declare an instance initializer.
15-e -Prints: Z.m1 Y.m1 -Member classes have access to the private fields and methods of the enclosing class and other member classes.
Furthermore, the enclosing class has access to the private fields and methods of the member classes.
16-a b c e -s1 s2 s3 s5 -Class Z is a local class defined within the code block of method m1 of class C. Class Z therefore has access to
all fields and methods (static, non-static, and private) of class C. Additionally, class Z has access to the local variables and parameters of
method m1 that are declared final. Consequently, class Z does not have access to parameter s6 or local variable s4. A local variable lives on
the stack and disappears as soon as the method is exited. A local class, however, might continue to exist even after the method has been
exited. For that reason, a local class can not access local variables that might have a short life cycle than the local class.
17-g -Compiler error at line 8. -A static nested class does not have access to non-static members of the enclosing class.

Capítulo 7 – Inner Classes (B)


No.-Answer-Remark
Certified Java Programmer Mock Exam 13
1-c -Compile-time error. -An anonymous class is not permitted to declare a constructor.
2-b c d -2 3 4 -An abstract method has no implementation and is not useful until an extending class implements the method. A final class
can not be overridden by a subclass method. An abstract final method can not be implemented and is not legal. An abstract class may contain
abstract method declarations and is therefore assumed to be incomplete. A final class can not be extended. The implementation of an abstract
final class could not be completed. The declaration of class C results in a compiler error because a final class may not be listed in the extends
clause.
3-a c e f g h l -abstract final private protected public static strictfp -The access modifiers, public, protected and private, can not be
applied to a local class. The access modifiers, protected and private, can be applied to member classes but not to top level classes. The public
modifier can be applied to a top level class or a member class, but not a local class. The static modifier can be applied to a member class but
not a local class or top level class. Extends and implements are keywords but are not modifiers. Synchronized can be applied to a method but
not a class. Transient and volatile can be applied to a field but not a class. The strictfp modifier can be applied to a class, interface, or method to
guarantee that all float or double operations are processed according to the IEEE 754 standard. Without the strictfp modifier the float or double
operations can be processed using the greatest precision available. For example, a 64 bit CPU could process intermediate float operations
using 64 rather than only 32 bits. If a class is marked strictfp then all float or double expressions within all nested classes, methods,
constructors, static initializers, instance initializers, and variable initializers will also be strictfp.
4-a c d f -A nested class is any class that is declared within the body of another class or interface. A top level class is a class this is not a
nested class. An inner class is a nested class that is not static. A named class is any class that is not anonymous. -Every class declared within
the body of another class or interface is known as a nested class. If the nested class does not have a name then it is an anonymous class. If the
nested class has a name then it is not anonymous. If the nested class has a name and is not declared inside of a block then it is a member
class. If the member class is not static then it is an inner class. If the class is not nested then it is a top level class. Chapter 8 of the Java
Language Specification defines class declarations. Section 8.1.2 of the JLS defines inner classes. Section 8.5 of the JLS defines member
classes. Section 15.9.5 of the JLS defines anonymous class declarations.
5-a b c d e f g -public protected private abstract static final strictfp -A nested class that has a name and is not a local class is a member
class. A member class can be static or non-static. A non-static member class is also known as an inner class. All of the class modifiers may be
applied to a member class. A member class may also be strictfp, but that information is not required for the exam. The strictfp modifier can be
applied to a class, interface, or method to guarantee that all float or double operations are processed according to the IEEE 754 standard.
Without the strictfp modifier the float or double operations can be processed using the greatest precision available. For example, a 64 bit CPU
could process intermediate float operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or double expressions
within all nested classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be strictfp.
6-b c -An incompletely implemented class must be declared abstract. A local class can be declared abstract. -An anonymous can not be
extended therefore an anonymous class can not be declared abstract. Even a local class can be abstract. An abstract class can not be
instantiated. Any class that declares an abstract method must also be abstract. A class can be declared abstract even if it does not declare an
abstract method. An abstract class can never be declared final.
7-b -2 -An abstract method has no implementation and is not useful until an extending class implements the method. Private methods are not
inherited and can not be overridden. If an abstract method is declared private then it can not be implemented in a subclass. Although a method
may not be both private and abstract a nested class can be; because another nested class can extend the abstract class and implement any
abstract methods.
8-d -4 -Methods and fields can share the same name because they are used in different contexts and use different lookup procedures. They
can even share the same name with the class in which they are declared. Please note, however, that class names usually begin with an upper
case letter while method and field names usually begin with a lower case letter. A nested class can not share the same name with its enclosing
class.
9-a b c d e f -nested classes methods constructors static initializers instance initializers variable initializers -The strictfp modifier can be
applied to a class, interface, or method to guarantee that all float or double operations are processed according to the IEEE 754 standard.
Without the strictfp modifier the float or double operations can be processed using the greatest precision available. For example, a 64 bit CPU
could process intermediate float or double operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or double
expressions within all nested classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be strictfp.
10-b -2 -The strictfp modifier in a superclass declaration has no impact on an overriding method in a subclass. For that reason, it makes no
sense to include the strictfp modifier in an abstract method declaration. By generating a compile time error, the compiler reminds the
programmer of the futility of combining the abstract and strictfp modifiers. A class, however, may be both abstract and strictfp. When applied to
a class, the strictfp modifier causes all float and double expressions within the class to be strictfp. This implies that all float and double
expressions are strictfp in all nested classes, methods, constructors and all static, instance, and variable initializers. The declaration of an
abstract method in a strictfp class is entirely legal even though the implementation of the method in a subclass need not be strictfp.
11-g h i -7 8 9 -The following modifiers can not be applied to a top-level class: private, protected, static, synchronized, transient, volatile. The
synchronized modifier can not be applied to any class because it is a method modifier. The modifiers, transient and volatile, can not be applied
to any class because they are field modifiers.
12-a b c d e f -s1 s2 s3 s4 s5 s6 -Class Z is a member class of class A and therefore has access to all of the fields and methods
(static, non-static, and private) of class A.
13-a c d e f -s1 s3 s4 s5 s6 -Class Z is a static member class of class B and therefore has access to all of the static fields and methods
of class B. However, a static member class does not have access to non-static fields and methods of the enclosing class. Consequently, class Z
does not have access to the s2 field of class B.
14-i j l -Compiler error at line 11. Compiler error at line 12. Compiler error at line 14. -A non-static nested class is called an inner class. An
inner class can not declare a static member unless it is a compile time constant. Even though f is final, it does not have a value at compile time
and therefore it causes a compilation error. Member variable g also causes a compiler error because it is static. The static initializer of
NonStaticInner causes a compiler error because inner classes (i.e. non-static nested classes) can not declare static initializers.
15-d -Compiler error at line 6. -An interface is implicitly static and therefore can not be declared as a member of a non-static nested class.
16-d -G.this.s1 -The qualified this expression, ClassName.this.name , can be used to access hidden variables within an enclosing class.

Capítulo 8 – Garbage Colection (A)


No.-Answer-Remark
1-a b e f -Prints: XY Prints: YX Nothing is printed. There is no guarantee that the garbage collector will run. -The finalize method of each
instance can only run once so X or Y can never be printed more than once. Instances x1 and y1 become eligible for garbage collection when
method m returns. Even though System.gc is invoked, there is no guarantee that the garbage collector will run.
2-a b c -i1 i2 i3 -Objects i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1
has a reference to i1, i2 or i3. When method J.m1 returns the ring becomes an island of isolated objects that are not reachable by any part of
the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two
objects form an island of isolated objects.
3-b c d -2 3 4 -Method m1 sets all elements of the array to null so the objects created on lines 2, 3 and 4 are all eligible for garbage
collection when method m1 returns.

Certified Java Programmer Mock Exam 14


4-a b -A B -Objects A and B have references to each other but no other Objects refer to A and B. Objects A and B form an island of islolated
objects and are eligible for garbage collection. All three references, i1, i2 and i3 refer to Object C so it is not eligible for garbage collection when
method m2 begins to execute.
5-e -Indeterminate. -If method m2 causes a reference to be saved for each Object that is passed in as an argument then none of the objects
are eligible for garbage collection when line 3 begins. In this case, we don't know what method m2 might be doing so we can not tell if the
objects are eligible for garbage collection.
6-e -None of the above. -Method m1 sets the argument a1 to null but that does not change the reference a1 in the J.main method. Since array
a1 continues to reference all three objects none of the three are eligible for garbage collection.
7-c -9 -With each pass through the loop the reference q1 is assigned to a new object and the old object becomes eligible for garbage
collection. When the processing of line 2 begins the last object referenced by q1 is not eligible for garbage collection.
8-c -Prints X. -This is a trick question. The argument, i, of method m1 is a copy of the local variable, i, of method J.main. Setting the argument,
i, of method m1 to null does not change the local variable, i, of method J.main.

Capítulo 8 – Garbage Colection (Opcionais)


No.-Answer-Remark
1-a b e f g -Prints: XY Prints: YX Nothing is printed. There is no guarantee that the garbage collector will run. There is no guarantee that
F.main will run to completion. -The finalize method of each instance can only run once so X or Y can never be printed more than once.
Although instance x1 becomes eligible for garbage collection first, there is no guarantee that it will be garbage collected first. Even though
System.gc is invoked, there is no guarantee that the garbage collector will run. The F.main method stays in a loop until the finalize method of
both object instances is invoked. If the garbage collector does not run, then F.main will remain in the loop.
2-c -4 -Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive. If the
garbage collector does not run then the program will produce no output. If the garbage collector does run then the output of the program could
be a series of integers that are the names of four of the five objects. As each new object is created its reference is assigned to the reference
variable j. The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage
collection until method m1 runs to completion. The while loop in the synchronized block will never complete because J.notFinalized will never
return zero. This program is intended to provide a working example of garbage collecting objects referenced by local variables.
3-c d -Prints: I'll be back! Prints nothing. -Objects that are finalizer-reachable or finalizable can become reachable again if the finalize method
assigns a reference to its instance to a member of another object that is reachable. Please see section 12.6.1 of the Java Language
Specification for more information. There is no guarantee that the garbage collector will run so it is possible that the program will wait forever
and never print anything. I do not recommend using the finalize method to make an object reachable again.
4-a b c d e -All three instances of I form a ring where each instance within the ring has a reference to the next instance. All three instances
of I are eligible for garbage collection after references i1, i2 and i3 are set to null. There is no guarantee that the garbage collector will run. The
order in which the instances in the ring are garbage collected is not guaranteed. The name of all three instances of I might be printed. -The
three instances of I form a ring where each instance of I has a reference to another instance. At the line marked 1 the references i1, i2 and i3
are set to null so the ring is no longer reachable from the user code in method J.m1. At that point the entire ring is eligible for garbage collection.
For more information, please see section 12.6.2 of the Java Language Specification.
5-b c d f -The user code can invoke the finalize method more than once. The garbage collector will never call the finalize method more than
once. The output could include the character X. The output could include the number 3. -Objects that are finalizer-reachable or finalizable can
become reachable again if the finalize method assigns a reference to its instance to a member of another object that is reachable. Please see
section 12.6.1 of the Java Language Specification for more information. User code can invoke the finalize method any number of times. The
garbage collector will not invoke the finalize method of a particular instance more than once so no more than one X can be printed and a
number larger than 3 can not be printed. It is possible that the garbage collector will not run so there is no guarantee that anything will be
printed. I do not recommend using the finalize method to make an object reachable again.
6-a b e f g -Prints: XY Prints: YX Nothing is printed. There is no guarantee that the garbage collector will run. There is no guarantee that
G.main will run to completion. -The finalize method of each instance can only run once so X or Y can never be printed more than once.
Although instance x1 becomes eligible for garbage collection first, there is no guarantee that it will be garbage collected first. Even though
System.gc is invoked, there is no guarantee that the garbage collector will run. The G.main method stays in a loop until the finalize method of
both object instances is invoked. If the garbage collector does not run, then G.main will remain in the loop.
7-b d e -The garbage collector will never call the finalize method more than once. Can not print Arnold's quote more than once. Could print
nothing. -Objects that are finalizer-reachable or finalizable can become reachable again if the finalize method assigns a reference to its
instance to a member of another object that is reachable. Please see section 12.6.1 of the Java Language Specification for more information.
The garbage collector will not call the finalize method of a particular instance more than once so Arnold's quote can not be printed more than
once. It is possible that the garbage collector will not run so there is no guarantee that Arnold's quote will be printed even once. I do not
recommend using the finalize method to make an object reachable again.

Capítulo 8 – Initializers (A)


No.-Answer-Remark
1-e -Compiler Error -The constructor of R has an explicit call to the super class constructor that includes an instance variable as a parameter.
A compiler error is generated since the instance of class R has not yet been fully initialized at the point in time when the super class constructor
is invoked. The instance variable R.j still contains the default value of zero rather than the assigned value of 2.
2-e -Compiler Error -The instance initializer of class R calls P.m1. Since P.m1 may throw an exception, that exception or a super class of the
exception must be declared in the throws clause of every constructor of class R. Note: Unlike a named class, an instance initializer of an
anonymous class may throw any exception. Prior to Java 1.4, the code example used in this question would not compile. The bug was fixed in
version 1.4. The bug ID is 4409174.
3-c -Prints: 682134 -Class D is loaded and the static initializer is processed before the main method begins to run. No instance of class D is
created, therefore, the instance initializer and constructor do not run. The main method invokes method "m" of class C. As a result, C is loaded
and the static initializer runs. A new instance of C is being created, so the constructor for class C is invoked. However, the instance initializer
runs to completion before the body of the constructor runs.
4-a -Prints: 5814 -Class B is loaded and the static initializer is processed before the main method begins to run. The use of the A.m() method
causes Class A to be loaded and the static initializer to be processed before method A.m() is processed. No instance of either class is created,
so no constructor or instance initializer runs.
5-e -Compiler Error -The constructor for class E contains an explicit call to the super class constructor. Since the super class constructor call is
not the first statement of the constructor a compiler error is generated.
6-e -Compiler Error -The no parameter constructor for class E contains an explicit call to the single parameter constructor. A compiler error is
generated because the explicit constructor call is not the first statement of the constructor.
7-e -Prints: Static Instance Constructor -Static initializers run before instance initializers.
8-b -Prints: Static Main Constructor -Static initializers run before main.
9-a -Prints: Static1 string1 Static2 -Static varibles and initializers are evaluated in the order they are defined.
Certified Java Programmer Mock Exam 15
10-c -Prints: 257 -The main method of class S begins to run and calls R.m1. Before m1 can run, class R and its super class Q are loaded and
the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been
initialized. After the classes are loaded and initialized, R.m1 runs. Since no new instance of class Q or R were created, the constructors and the
instance initializers were not processed.
11-e -Compiler Error -The single parameter constructor for class B does not have an explicit call to the super class constructor. Therefore, the
compiler tries to add an implicit call to the no parameter constructor of the super class. Since the super class has a declaration for a single
parameter constructor, the compiler does not implicitly create a no parameter construct. A compiler error is the result.
12-f -Prints: Static s2 s1 -Static varibles and initializers are evaluated before instance variables.
13-b -Prints: 682134134 -Class F is loaded and the static initializer is processed before the main method begins to run. No instance of class F
is created, therefore, the instance initializer and constructor do not run. The main method invokes method "m" of class E. As a result, E is
loaded and the static initializer runs. A new instance of E is being created, so the constructor for class E is invoked causing the instance
initializer to run to completion before the body of the constructor is processed. Method E.m then runs. A second instance of Class E is created
causing the constructor for class E and the instance initializer for class E to run again followed by method m. Of course, the constructor of the
Object class is always called each time a new instance of any Object is created.
14-e -Prints: 251346 -The main method of class V begins to run and calls for the creation of a new instance of U. Before the new instance can
be created, class U and its super class T are loaded and the static initializers of the super class and the sub class are processed. If any static
variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the constructor of class U begins
to run. The first statement is an implicit call to the super class constructor. The call to the super class constructor has been added by the
compiler, since it does not appear in the source code. Before the body of the super class constructor is processed, the instance initializer will run
to completion. If any instance variables had been declared, then they would also be initialized before the body of the super class constructor is
processed. Control then returns to the sub class constructor and the sub class instance initializer runs to completion before the body of the
constructor is processed.
15-b -Prints: SA SB IA CA IB CB -Static initialization of the super class occurs first followed by static initialization of the sub class. Instance
initialization of the super class occurs before instance initialization of the sub class.
16-a -Prints: 251346 -The main method of class C begins to run and calls for the creation of a new instance of class B. Before the new
instance can be created, class B and its super class A must be loaded and the static initializers of the super class and the sub class are
processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the
instance creation process begins. The single parameter constructor has as its first statement a call to the single parameter super class
constructor. Similarly, the super class constructor has as it first statement an implicit call to the no parameter constructor of the Object class.
Once the Object class constructor runs to completion, control returns to the constructor of class A. Before the body of the class A constructor is
processed, the instance initializer of class A runs to completion. After the body of the class A constructor runs to completion control returns to
the constructor of class B. The instance initializer of class B runs to completion before the body of the construct is processed.
17-b -Prints: 261358 -The main method of class L begins to run and calls for the creation of a new instance of class K. Before the new instance
can be created, class K and its super class J must be loaded and the static initializers of the super class and the sub class are processed. If any
static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the instance creation
process begins. The single parameter constructor has as its first implicit statement a call to the no parameter super class constructor. The
implicit statement is added by the compiler even if it does not appear in the source code. Another implicit feature of a constructor is the fact that
all instance variables are initialized and all instance initializer blocks are processed before the body of the constructor is processed.
18-d -Compiler Error: variable d might not have been initialized. -The compiler is able to recognize that b is definitely assigned after the
assignment expression on line 1. Please see JLS section 16.1.7. The compiler is able to recognize that c is definitely assigned in both the "if"
and "else" clause of the if/else expression on line 2. Please see JLS section 16.2.7. The compiler looks at the "if" statements on lines 3, 4 and 5
separately and does not look at the combined result of the three. As a result, the compiler recognizes that d may remain unassigned after each.
Therefore, the compiler concludes that d is not definitely assigned before the attempted use on line 6. 19-b -Prints: 2613587 -The main method
of class P begins to run and calls for the creation of a new instance of class N. Before the new instance can be created, class N and its super
class M are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they
would also have been initialized. After the classes are loaded and initialized, the instance creation process begins. The no parameter
constructor has as its first statement a call to the single parameter constructor. The single parameter constructor has as its first statement an
implicit call to the no parameter superclass constructor. Since the call to the super class constructor does not appear in the source code, the
compiler adds it implicitly. Before the body of the super class constructor is processed the constructor of the Object class is called and runs to
completion. Afterward, the instance initializer of class M runs to completion. If any instance variables had been declared, then they would also
be initialized. After the body of the super class constructor runs to completion control returns to the sub class constructor. Before the body of the
sub class constructor runs, the instance initializer runs to completion.
20-c -Prints: 27A13689 -The main method of class I begins to run and calls H.m2. Before the m2 method can run, class H and its super class
G are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would
also have been initialized. After the classes are loaded and initialized, H.m2 runs. Since no new instance of class H or G were created, the
constructors and the instance initializers were not yet processed. The next line of I.main creates a new instance of H and calls H.m1. The
instance creation process begins with a call to the constructor of H. The first line of the construct is an implicit call to the super class construct.
The call to the super class constructor does not appear in the source code, so the compiler added it implicitly. Before the body of the super class
constructor runs the instance initializer is processed. If any instance variables had been declared, then they would also have been initialized.
The body of the super class constructor is then processed before control returns to the sub class constructor. The instance initializer of the sub
class is processed before the body of the sub class. Following the instance creation process, the method H.m1 runs.

Capítulo 9 – Thread (A)


No.-Answer-Remark
1-a e i j -countStackFrames resume stop suspend -
2-a d e f g h i j -join resume run sleep start stop suspend yield -The run method is declared in the Runnable interface but is
implemented in the Thread class. The wait, notify, and notifyAll methods are declared in the Object class and are members of the Thread class
by inheritance. This question asks which methods are declared in the Thread class but are not inherited from the object class.
3-a c -Prints a number greater than or equal to 0. This program will run for at least ten seconds. -Thread a1 will run for at least ten seconds,
but the main method will run to completion very quickly. The start method will return without waiting for thread a1 to complete. Since thread a1
immediately goes to sleep the thread that is processing the main method has an opportunity to complete the main method quickly. The number
printed in the main method can be as small as zero.
4-b -Prints: false. -Thread a1 intends to wait forever since no other thread invokes the notify method. After starting thread a1 the main method
invokes the interrupt method on thread a1. In response, thread a1 moves out of the Not-Runnable state. Once the thread begins to run it throws
an InterruptedException and clears the interrupted flag. The exception is caught and the interrupted method is invoked to test the state of the
interrupted flag. Since the flag was cleared when the exception was thrown the return value of the interrupted method is false.

Certified Java Programmer Mock Exam 16


5-d -Prints: ABC. -Inside the main method a new thread, t1, is started and will move into the running state at the discretion of the thread
scheduler. The A.run method invokes the wait method on the String array object, sa, causing the thread to block until another thread invokes the
sa.notify method. Before the B.main method invokes sa.notify all of the elements of the String array object, sa, have already been updated.
6-b c -Some or all of the numbers 0 through 9 could be printed. There is no guarantee that anything will be printed. -All of the threads started
in method B.m1 are daemon threads so the program can run to completion even if some or all of the daemon threads have not run.
7-b d e h -The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler. The Runnable interface
declares the run method. The Thread class implements the Runnable interface. Some implementations of the Thread.yield method will not
yield to a thread of lower priority. -The Object.notify method can only be called from within a synchronized block by the thread that holds the
monitor for that object. Suppose that thread T1 is holding the monitor for an object, A, within a synchronized block. If thread T1 calls the notify
method immediately after entering the synchronized block, then no other thread can grab the monitor for object A until T1 leaves the
synchronized block. For that reason, the transfer of control from thread T1 to any waiting thread can not be accelerated by moving the notify
method to an earlier point in the synchronized block. The behavior of Thread.yield is platform specific. However, at least some implementations
of the yield method will not yield to a thread that has a lower priority. Invoking the Thread.yield method is like offering a suggestion to the JVM to
allow another thread to run. The response to the suggestion is platform specific.
8-a -Prints: A -If a Runnable target object is passed to the constructor of the Thread class, then the Thread.run method will invoke the run
method of the Runnable target. In this case, the Thread.run method is overridden by A.run. The A.run method does nothing more than print the
letter A. The invocation of the A.start method inside the main method results in the invocation of A.run and the letter A is printed. The B.run
method is never invoked.
9-b d -A program will terminate only when all user threads stop running. A thread inherits its daemon status from the thread that created it. -
10-e -Compile-time error. -Remember that the Object.notify method is an instance method and can not be invoked from a static context. Also,
the thread that invokes the notify method on an object must own the lock on the object.
11-d f g -By entering a synchronized instance method. By entering the body of a block that is synchronized on the Object. By entering a
synchronized static method of a class of type Class. -Blocking on IO or invoking the Thread.sleep or Object.wait method causes a Thread to
enter the non-runnable state. Invoking the notify method on an Object wakes up a Thread that is waiting on the Object. The Thread that invokes
wait or notify on an Object should already be the owner of the Object's monitor. Invoking the wait or notify method does not cause the Thread to
become the owner. Static methods are synchronized on the Class object of the class. Instance methods are synchronized on the instance of the
class.
12-d -Compiler error. -Both the sleep and join methods declare an InterruptedException that must be caught or declared in the throws clause of
A.main.
13-a -The process of executing a synchronized method requires the thread to acquire a lock. -The synchronized modifier can not be applied to
a class. A method that overrides a synchronized method does not have to be synchronized. A thread must first acquire the lock on the object
instance on which a synchronized instance method is invoked. A synchronized static method is synchronized on the lock for the Class object
that represents the class for which the method is a member.
14-a c -The first number printed is greater than or equal to 0. The second number printed must always be greater than 5000. -The notify
method is never invoked on Thread a1 so it will sleep for at least five seconds. The invocation of the join method forces the main thread to wait
for the completion of Thread a1. The argument of 6000 will allow the main thread to wait for six seconds if necessary, but we know that thread
a1 will complete in only five seconds. The first number printed will be greater than or equal to zero and the second number will be greater than
or equal to 5000. The synchronized block is necessary because it is necessary to hold the lock on an object when the wait method is invoked.
15-e -An IllegalThreadStateException is thrown at run-time. -In reality, the JVM will probably not behave as described by the correct answer.
This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4
version of the JVM. In reality, the JVM would probably ignore the second attempt to start the thread and would probably not throw the exception
if the thread were already dead. For the purposes of the exam, invoking the start method on a thread that has already been started will generate
an IllegalThreadStateException. The Thread.join method is included here to verify that the thread is already dead before the start method is
invoked the second time. If this code is executed using the 1.4 version of the JVM, the exception will not be thrown. However, for the purposes
of the exam, the exception is always thrown. The real exam question will probably not include the invocation of the join method.
16-a -1. -Thread is no Thread constructor that accepts a single argument that is a reference to a ThreadGroup. The String object is name of
the thread. Yes, the exam requires you to memorize the thread constructor signatures.
17-a -The priority assigned to thread T2 is greater than the priority assigned to T1. -The Java Language Specification suggests that higher
priority threads should be given preference over lower priority threads but explicitly states that such preference is not a guarantee. It is very
important to remember that no guarantee exists.
18-b d -Prints: [T1,A][T2,B]. Prints: [T2,B][T1,A]. -Since method m1 is synchronized it is guaranteed that no more than one thread will process
the method at any one time. Even though the start method is invoked on thread T1 first there is no guarantee that it will actually begin to run
first.
19-a b -Prints: A1A2A3B1B2B3. Prints: B1B2B3A1A2A3. -Two threads are started. The run method declares a local variable that is a String
literal containing an empty String. String literals are held in a pool and are shared. If the empty String literal appears at 10 locations in a program
then all 10 will refer to the same String. Both threads have a separate copy of the local variables so each thread has a separate copy of a
reference that refers to the empty String literal. Even though the reference held by each thread is unique the String instance itself is shared
between the two threads and therefore the lock on the String is also shared between the two threads. The shared lock on the shared String
literal will provide effective synchronization.

Capítulo 9 – Thread (B)


No.-Answer-Remark
1-b c k -notify notifyAll wait -
2-f j -sleep yield -
3-a c f -Thread.MAX_PRIORITY = 10. Thread.NORM_PRIORITY = 5. Thread.MIN_PRIORITY = 1. -
4-e -Compiler error -The Runnable.run method does not have a throws clause so any implementation of run may not throw a checked
exception.
5-b -Prints a number greater than 1000. -If the interrupt method is invoked on a thread that is sleeping or waiting, then the thread will wake up
and then throw an InterruptedException when it starts to run. If the interrupt method is invoked on a thread that is not sleeping or waiting, then
no exception is thrown. Instead, the interrupted flag is set and the thread may check the flag at its own discretion using the interrupted method.
In this code example the thread checks the interrupted flag and returns if the flag is set to true. The elapsed time printed by the main method will
therefore be greater than 1000. If plenty of CPU time is available then the printed number will be just a few milliseconds greater than 1000.
However, if the CPU is very busy and if higher priority threads are competing for CPU time then the printed number could be much greater than
1000.
6-b e f g h i -A compile time error occurs if the expression is a primitive type. If execution of the block completes normally, then the lock is
unlocked. If execution of the block completes abruptly, then the lock is unlocked. A thread can hold more than one lock at a time.
Synchronized statements can be nested. Synchronized statements with identical expressions can be nested. -

Certified Java Programmer Mock Exam 17


7-b c d f -The Thread.yield method might cause the thread to move to the ready state. The same thread might continue to run after calling
the Thread.yield method. The Thread.yield method is a static method. The Thread.sleep method causes the thread to move to the Not-
Runnable state. -The thread.yield method is intended to cause the currently executing thread to move from the running state to the ready state
and offer the thread scheduler an opportunity to allow a different thread to execute based on the discretion of the thread scheduler. The thread
scheduler may select the same thread to run immediately or it may allow a different thread to run. The thread.yield method is a native method so
the behavior is not guaranteed to be the same on every platform. However, at least some implementations of the yield method will not yield to a
thread that has a lower priority.
8-a b -Prints: X0Y1X2Y3X4Y5. Prints: X0X1X2Y3Y4Y5. -Two threads are started, t1 and t2, and run concurrently. Both threads invoke the
C.getI method to get the current value of C.i and C.i is incremented with each invocation of the method. The C.i method is synchronized so the
value returned by C.i is always unique. However, the order in which the threads run is entirely dependent on the thread scheduler and may vary
from one platform to the next.
9-d -Prints: ABC. -The block inside the main method is synchronized on the String array object, sa. Inside the block a new thread, t1, is started
and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa.
Even if the thread scheduler moves thread t1 into the run state it will block while attempting to acquire the lock on the String array object, sa.
Thread t1 will continue to block until the synchronized block in the B.main method runs to completion. At that time, the contents of the String
array object have all been updated.
10-c -Return from the run method based on the state of a boolean flag. -The destroy method stops the thread without any cleanup. The stop
method is deprecated. Checking a boolean flag in the run method allows the thread to stop at a predictable point in the execution of the thread
loop. Any locks held by the thread will be released when the run method is done.
11-e -None of the above. -All of the constructor invocation statements are legal. The String object is name of the thread. Yes, the exam
requires you to memorize the thread constructor signatures.
12-a b d f -Another Thread invokes the notify method on the Object, Obj1, and T1 is selected to move out of the wait set. Another Thread
invokes the notifyAll method on the Object. Another Thread interrupts Thread T1. A specified timeout period has elapsed. -
13-c -3. -The position of the arguments have been reversed in the constructor on line 3. The Runnable argument should appear before the
thread name String. Yes, the exam requires you to memorize the thread constructor signatures.
14-b c d e f -Thread.sleep method. Thread.join method. Object.wait method. By blocking on IO. Unsuccessfully attempting to acquire a
lock on an object. -The Thread.yield method may cause a thread to move into the ready state. Thread.yield will not cause a thread to move into
the not-runnable state.
15-b c d e h -The Thread.join method is always invoked on an instance of a Thread. The Thread.join method causes the current thread to
wait for the referenced thread to die. The Thread.join method throws an InterruptedException. The Thread.join method accepts a timeout value
as an argument. A timeout of zero will allow Thread.join to wait forever if necessary. -The Thread.join method is not static. Thread.join is
always invoked on an instance of a Thread. Thread.join causes the current thread to wait until the referenced thread has died. The maximum
time limit to wait for the death of the referenced thread can be specified in milliseconds by an argument. Thread.join will throw an
InterruptedException if the interrupt method is invoked on the referenced Thread.
16-e -None of the above. -
17-e -An IllegalThreadStateException is thrown at run-time. -For the purposes of the exam, invoking the start method on a thread that has
already been started will generate an IllegalThreadStateException. If the start method is invoked a second time when the thread is already
running then an illegal thread state exception will probably be thrown. However, if the thread is already dead then the second attempt to start the
thread will probably be ignored and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in
response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the
Thread.start method and ignores the actual behavior of the 1.4 version of the JVM.
18-b c d e -Prints: [T1,A][T2,B]. Prints: [T1,B][T2,B]. Prints: [T2,B][T1,A]. Prints: [T2,A][T1,A]. -Since method m1 is not synchronized there
is no guarantee that any one thread will complete the method before another begins. Since the behavior of Thread.yield is implementation
dependent there is no guarantee that control will be transferred to another thread. Even though the start method is invoked on thread T1 first
there is no guarantee that it will actually begin to run first.
19-a b c d -Prints: A1A2A3B1B2B3. Prints: B1B2B3A1A2A3. Prints: A1B1A2B2A3B3. Prints: B1A1B2A2B3A3. -Two threads are started. It
is possible that one will run to completion before the other starts running. In that case the output would be either A1A2A3B1B2B3 or
B1B2B3A1A2A3. It is also possible that the two will take turns running. In that case the output could be something like A1B1A2B2A3B3 or
B1A1B2A2B3A3. Other output sequences are also possible. The attempt to synchronize on the local variable, b, has no impact on the execution
of the code. Each thread invokes the run method separately and each has a separate copy of the local variables. Since each thread has a
separate copy of the local variable, b, then each thread also has a separate copy of the lock on its own copy of local variable b. In general, a
lock on a local variable will have no impact on synchronization. An exception to the rule would be if the local variable is a reference to a shared
instance of an Object. If you would like to verify the failure of the synchronization, then place a Thread.sleep method invocation between the
print statements.

Capítulo 9 – Thread (C)


No.-Answer-Remark
1-b c -Prints: 0. Prints: 1. -The new thread is started before the print statement, but there is no guarantee that the new thread will run before
the print statement is processed. An a.join() statement before the print statement would provide the guarantee, but the join statement does not
appear in this program. If the new thread runs before the print statement is processed, then 1 is printed. Otherwise, 0 is printed.
2-a e j -join sleep wait -
3-a e j -join sleep wait -
4-c -Prints: 1. -The a.run() method was called instead of a.start() so the entire program runs as a single thread and a.run() is guaranteed to
complete before the print statement is called.
5-d -InterruptedException -The Object.wait method and the Thread.join and Thread.sleep methods throw the checked exception
InterruptedException.
6-a b c d -Prints: XYZ. Prints: AYZ. Prints: ABZ. Prints: ABC. -After the start method is invoked on thread t1 it will start running at the
discretion of the thread scheduler. If the thread scheduler allows thread t1 to start and complete immediately then the output could be XYZ. If
the thread scheduler allows B.main to complete before thread t1 is allowed to run then ABC will be printed. It is also possible that t1 might start
after only one or two array elements are overwritten. For that reason it is not possible to be sure which elements of the String array will be
updated before the contents of the array are printed.
7-a b h -notify notifyAll wait -
8-a -The number printed is greater than or equal to 0. -The notify method is never invoked on Thread a1 so it will sleep forever and the
program will run forever.
9-b -The thread's interrupt status is set. -If the interrupt method is invoked on a thread that is not blocking as a result of a previous invocation of
the join, wait or sleep method and is not blocking on IO then the interrupt status of the thread is set. The thread is free to check the interrupt
status at its own discretion.

Certified Java Programmer Mock Exam 18


10-c d e f -The ready state to the running state. The running state to the Not-Runnable state. The running state to the ready state. The Not-
Runnable state to the ready state. -A dead thread can not be restarted.
11-a -The number printed is greater than or equal to 0. -The a1 is a daemon thread so the program can run to completion even if thread a1 is
still running, waiting or sleeping. The notify method is never invoked on Thread a1. If Thread a1 were not a daemon thread then the program
would wait forever. However, the program will run to completion without waiting for thread a1.
12-e -Compile-time error. -Remember that the Thread.start method is an instance method and can not be invoked from a static context.
13-e -Prints: T1T1T3 -The Thread.currentThread method returns a reference to the currently executing thread. When the run method is
invoked directly it does not start a new thread so T1 is printed twice.
14-a b c d -Thread.interrupted is a static method. Thread.isInterrupted is an instance method. Thread.interrupted clears the interrupt status
flag but Thread.isInterrupted does not. The boolean value false will be returned if Thread.interrupted or Thread.isInterrupted is invoked after the
InterruptedException has been thrown. -Thread.interrupted is a static method that determines if the currently executing thread has been
interrupted. Thread.isInterrupted is an instance method that determines if the referenced thread instance has been interrupted.
Thread.interrupted clears the interrupted status flag, but Thread.isInterrupted does not clear the status flag. If either method is invoked after the
InterruptedException has been thrown the returned value will be the boolean value false.
15-b -ready -
16-a c -primitives A local variable that is an object reference. -Primitives don't have locks and therefore can not be used to synchronize
threads. A local variable can not be used to synchronize threads because each thread has its own copy of local variables and their locks.
17-c -The printed number must always be greater than 10000. -If the interrupt method is invoked on a thread that is sleeping or waiting, then
the thread will wake up and then throw an InterruptedException when it starts to run. If the interrupt method is invoked on a thread that is not
sleeping or waiting, then no exception is thrown. Instead, the interrupted flag is set and the thread may check the flag at its own discretion using
the interrupted method. In this code example the thread never checks the interrupted flag so the interrupt is just ignored and the run method
runs to completion. The elapsed time printed by the main method will therefore be greater than 10000.
18-b c -The thread moves out of the Not-Runnable state. An InterruptedException is thrown when the thread moves into the running state. -A
sleeping thread is in the Not-Runnable state. If the interrupt method is invoked on a sleeping thread it will move out of the Not-Runnable state.
When it reaches the running state it will throw an InterruptedException and the interrupt status flag will be cleared. The Thread.interrupted
method will return false if it is invoked in the catch clause that catches the InterruptedException.
19-e -An IllegalThreadStateException is thrown at run-time. -Invoking the start method on a running thread will generate an
IllegalThreadStateException. The exam might have a question that invokes the start method on the same thread twice. The correct answer will
state that an exception is thrown at run-time. In reality, the JVM would probably ignore the second attempt to start the thread and would
probably not throw the exception if the thread were already dead. This code example includes a lot of thread synchronization code that ensures
that the thread is not dead when the start method is invoked the second time. You can run this example yourself and get the correct result. On
the real exam, the synchronization code will not be included.

Capítulo 10 – The java.lang Package (A)


No.-Answer-Remark
1-a -Prints: long primitive -The Long.parseLong method returns a long primitive.
2-c -Prints: int,int,int -The Math.min method is overloaded. There are versions that accept parameters of types int, long, float, and double. The
type of the return value is the same as the type of the input parameter. If a byte or a short is passed to the Math.min method as an input
parameter, then it will be promoted to a type that is accepted by Math.min. The type that it is promoted to will depend on the type of the other
parameter. If both parameters are of type byte, then both are promoted to an int. Similarly, if both parameters are of type short, then both are
promoted to int. If one is of type byte or short and the other is of type int, then the byte or short is promoted to an int.
3-e g -parseDouble valueOf -
4-f -None of the above. -None of these statements that pass a String as an argument will generate a compile-time error but some will generate
a run-time error. The Short wrapper class has two constuctors. One accepts an argument of type short and the other accepts a String. A String
argument must represent an integral primitive type. A leading minus sign can be added to indicate a negative value. A leading zero added to
represent an octal value will be ignored and the String will be interpreted as a decimal value. A leading zero and letter x added to a String to
indicate a hexadecimal value will result in a run-time error.
5-g -Runtime Error -The Long.parseLong method accepts a String parameter that represents a numeric value. Long.parseLong assumes that
the input String represents a decimal value unless a second parameter is provided to specify the radix. All of the characters in the String must
be digits of the specified radix. The parseLong method does not determine the type of the numeric value based on a suffix such as l, L, f, F, d, or
D. Use of any such suffix generates a NumberFormatException at run time.
6-f -Run-time Error -The parseByte and valueOf methods use a second parameter, radix, to specify the base of the number system. For
example, if the radix is two then the number is binary. If the radix is 8 then the number is octal. The Byte constructor does not accept a radix
parameter. The only Byte method that is able to decode a prefix such as 0x to determine the radix is the Byte.decode method, but that method
will not appear on the exam. None of the methods on the exam, parseByte, valueOf, or the constructor, are able to decode a prefix that specifies
the radix.
7-c -Compile-time Error -Integer has two constructors. The first accepts a parameter of type primitive int. The second accepts a parameter of
type String. The attempt to pass primitiveLong and primitiveFloat to a constructor of the Integer class generates a compiler error because
method invocation type conversions do not include implicit narrowing conversions such as the narrowing of a long or float to an int.
8-d -Prints: true,true -The expression (b1.equals(b2)) compares the values of two instances of byte objects. Since both instances contain the
value one the return value of the method call is true. The expression (a==b) compares the hash codes of two instances of Byte objects. Since
the two Byte objects contain the same value the result is true.
9-h -Prints: true,true,true -NaN is the only value that is not equal to itself. The Double.isNaN method returns the result of the expression (v !=
v).
10-c d -3. 4. -The boolean literals are written with lower case letters. The boolean literals can not be written with upper case letters. A String
representation of a boolean is acceptable in both upper and lower case letters.
11-c -Prints: 1,-1,0 -String.indexOf returns negative one if the argument is not found in the String. Zero is returned if the argument is an empty
String.
12-d -Prints: true,true -The Math.random method returns a primitive of type double. The value is equal to or greater than zero and less than
1.0.
13-k -None of the above. -The program compiles and runs without error. The output is 1.0,1.0,Infinity,-Infinity,NaN. The String parameter can
represent any floating point literal including Infinity and NaN. However, integral literals that are not floating point literals are not accepted in
String format. For example, the suffix used to declare a long, L, is not accepted in String format. However, a long literal value that is not in String
format is promoted to a float primitive and is accepted by the constructor that has a float parameter.
14-b d f -Float f2 = new Float("A"); Float f4 = new Float("1L"); Float f6 = new Float("0x10"); -The Float constructor is overloaded. One
accepts a primitive of type float. One accepts a primitive of type double. One accepts a String representation of a floating point literal. The
acceptable string representations of numeric values are more restricted than the acceptable primitive literal representations. The char primitive
literal is promoted to a float and is accepted by the constructor with a parameter of type float. The String containing the character A can not be

Certified Java Programmer Mock Exam 19


parsed into a numeric value and therefore generates a NumberFormatException. Although the suffix used to represent a primitive literal of type
long, L, is acceptable as a primitive literal it is not acceptable as a String. Similarly, the prefix used to represent a hexadecimal literal, 0x, is
acceptable as a primitive literal but is not acceptable as a String.
15-d -Prints: false,true,true -The Boolean constructor creates two instances of type Boolean containing the value true. The equality operator
compares reference values and finds that the unique instances of Boolean objects have different reference values. The result of the equality
expression is false. The Boolean.booleanValue method returns a primitive boolean that represents the value of the Boolean object. Since the
primitive boolean values are equal the result of the equality expression is true. The equals method compares the values of the primitives that are
wrapped by the Boolean objects. Since the wrapped primitive values are equal the result of the equals method is true.
16-e -Prints: true,false,false -Integer.MIN_VALUE represents the most negative number that can be represented by an int value.
Float.MIN_VALUE represents the smallest positive value that is greater than zero.
17-b -Prints: 0,32 -The capacity has been set to 32. The StringBuffer contains no characters so the length is zero.
18-b -Prints: false,false,true -The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the
expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String.
Since the equality operator compares references and not content values, the equality operator returns false. The equals method compares
content values so it returns true.
19-d -Run time Error -A StringIndexOutOfBounds exception is thrown at run time in response to the attempt to set the value of a character
beyond the current length of the StringBuffer.

Capítulo 10 – The java.lang Package (B)


No.-Answer-Remark
1-e -Compile-time Error -There are two overloaded constructors for the Integer class. The first has one parameter of type int. The second has
one parameter of type String. This code example generates a compiler error as a result of the attempt to pass two parameters to the Integer
constructor. The second parameter in this code example appears to be a radix parameter such as the second parameter of the Integer.parseInt
method. Yes, it is necessary to memorize the signatures of the wrapper constructors and the methods listed in the certification objectives.
2-a d e -abs max min -
3-a -Prints: 2,8,10,16,36 -The Byte.valueOf method is overloaded. The return value of both versions is a Byte object. The first version of
valueOf accepts a single parameter that is a string representation of a byte. The valueOf method does not examine the format of the string
parameter to determine the radix, so valueOf does not accept Strings that have a prefix such as the octal leading zero or the hexadecimal prefix
0x. The second version of the valueOf method accepts a second parameter which is a string that represents the radix used to parse the first
parameter. The radix is the base of the number system. For example, if the radix is two then the number is binary. If the radix is 8 then the
number is octal.
4-c -Compiler Error -Long has two constructors. The first accepts a parameter of type primitive long. The second accepts a parameter of type
String. There is no constructor that accepts a reference to an object of type Long.
5-a -Prints: Infinity,NaN,Infinity,NaN. -The Double and Float constructors recognize the Strings Infinity and NaN.
6-a b c d e f -Short s1 = new Short(1); Short s1 = new Short('1'); Short s1 = new Short(1L); Short s1 = new Short('b' - 'a'); Short s1 = new
Short(1.0); Short s1 = new Short((short)1 - (short)2); -The Short wrapper class has two constuctors. One accepts an argument of type short
and the other accepts a String. There is no implicit conversion from the primitive types char, int, long, float or int to type short so those types can
not be passed as an argument to the constructor without an explicit cast to the primitive short type. Please note that if both operands of a binary
arithmetic expression are of type byte, char or short then both are implicitly widened to type int and the result of the expression is of type int.
7-e -Compiler Error at 3 -The Byte class has only two constructors. One accepts a byte primitive and the other accepts a String. The literal
primitive that appears in the constructor invocation statement at line 3 has type int and the compiler will not implicitly narrow the int to a byte. In
line 1 a literal of type int is implicitly narrowed to a byte by the compiler. The compiler will implicitly do a narrowing conversion for an assignment
statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable
on the left and if the variable is of type byte, short, or char. For that reason, a constant int expression that is equal to one may be assigned to a
byte without an explicit cast. The same is not true for the parameters of a method or constructor. The designers of the Java programming
language felt that implicit narrowing conversions for method and constructor parameters would add unnecessary complexities to the process of
resolving overloaded method calls.
8-b -Prints: 3,9,11,17 -The Long.parseLong method parses the input String parameter and returns a long primitive value that is equal to the
value represented by the String. The second parameter, if present, is the radix. For example, the String value 11 with a radix of 2 is parsed as 2
plus 1 equals 3. Similarly, the String value 11 with a radix of 8 is an octal value that is parsed as 8 plus 1 equals 9. The String value 11 with a
radix of 10 is a decimal value that is parsed as 10 plus 1 equals 11. The String value 11 with a radix of 16 is a hexadecimal value that is parsed
as 16 plus 1 equals 17.
9-g -Compiler error -The StringBuffer class has an append method but the String class does not.
10-a -Compile-time error at 1. -The Float.toString method is overloaded. One accepts no parameters and just returns the value wrapped by the
Float object. The other accepts a primitive of type float. The literal, 1.0, is of type double and can not be implicitly narrowed to a float.
11-c -Prints: false,true,false -If the argument to the valueOf method is a String that represents the boolean value true ignoring case then the
result is a reference to the Boolean.TRUE object. Any other String causes the valueOf method to return a reference to the Boolean.FALSE
object. When the left and right operands of the equality operator are both references to the Boolean.FALSE object then the result of the equality
expression is true.
12-e -Returns the largest whole number that is less than or equal to the argument value. -The floor method is not overloaded and declares only
one parameter of type double. The return value is always of type double. Floor does not declare any exceptions.
13-a b c d e -doubleValue floatValue intValue longValue parseDouble -
14-e -Prints: true,false,false -If the argument to the constructor is a String that represents the boolean value true ignoring case then the result
is a Boolean object that wraps the primitive value true. Any other String generates a Boolean object that wraps the primitive value false.
15-k -None of the above. -The Float constructor is overloaded. One accepts a primitive of type float. One accepts a primitive of type double.
One accepts a String representation of a floating point literal. The String parameter can represent any floating point literal.
16-f -Prints: -1,-1,true -Integer is a subclass of the abstract class Number and implements all of the methods of Number such as byteValue,
shortValue, floatValue, etc. In this case, the Integer object contains the value 0xFFFF. When that value is cast to a byte the result is 0xFF which
is also the two's compliment representation of a byte value that is equal to negative one. Similarly, 0xFFFF is the two's compliment
representation of a short value that is equal to negative one. Please note there is no charValue method.
17-f -Prints: BDE -The substring method returns a new string that is a substring of the original string. The single parameter form creates a new
string that begins at the index specified by the parameter. The two parameter form creates a substring that starts at the index of the first
parameter and ends at the index of the second parameter. The character at the start index is included in the substring but the character at the
end index is not.
18-a -Prints: false,false -StringBuffer does not override the equals method of the Object class, so StringBuffer.equals just compares reference
values. The equals method of s1 returns false in response to the attempt to compare the equality of a String and a StringBuffer. Since the two
objects have different types, the result is false.

Certified Java Programmer Mock Exam 20


19-g -None of the Above -The capacity is the default capacity of a StringBuffer plus the length of the String used to initialize the StringBuffer.
The default size is 16 and the length of the String is seven so the capacity is 23. This form of the StringBuffer constructor performs two steps.
First, it instantiates the internal char array with the capacity described above. Second, it calls the StringBuffer.append method to place the String
into the char array.

Capítulo 10 – The java.lang Package (C)


No.-Answer-Remark
1-e f -Compiler error at line 13. Compiler error at line 14. -The overloaded contructors for the String class accept a parameter of type String or
StringBuffer or an array of byte or char. There is no constructor that will accept a primitive byte or char. Please note that if you want to convert a
primitive to a String then you can use the static String.valueOf method.
2-n -Prints: 1.0,8.0,16.0,1.0,1.0,1.0 -The Float constructor is overloaded. One accepts a primitive of type float. One accepts a primitive of type
double. One accepts a String representation of a floating point literal. All numeric values can be promoted to type double so all numeric values
are accepted by the float constructor. However, the acceptable String representations of numeric values are more restricted. For example,
hexadecimal values are not accepted as Strings.
3-c -Compile-time error. -The valueOf method of the numeric integral wrapper classes, byte, short, int and long, are overloaded. The first
version of valueOf accepts only a single parameter that is a String representation of a numeric integral value. The second parameter represents
the radix or base of the number system. For example, if the radix is two then the number is binary. If the radix is 8 then the number is octal. The
valueOf method of the floating-point wrapper classes do not accept a radix parameter.
4-f -Prints: Wrapper,Primitive,Wrapper -The Boolean.valueOf method returns a Boolean object. The Boolean.booleanValue method returns a
primitive. The Boolean.TRUE field is a Boolean object that wraps the primitive value true.
5-b c e -Compiler Error at 3 Compiler Error at 4 Compiler Error at 6 -The Byte class has only two constructors. One accepts a byte primitive
and the other accepts a String. The literal primitive that appears in the constructor invocation statement at line 3 has type int and the compiler
will not implicitly narrow the int to a byte. Line 4 generates a compiler error due to the attempt to use an argument of type char. Line 6 generates
a compiler error because the result of the expression is of type int but the variable is of type byte.
6-c -Compiler Error -A compiler error is generated because the Byte class does not have a charValue method. The Byte class extends the
Number class and implements all six of the Number methods. It is interesting to note that four of the Number methods are abstract while two,
byteValue and shortValue, are not abstract. That's because there is a concrete implementation of byteValue and shortValue that just call the
intValue method and then cast the result to the appropriate type.
7-e f g -parseDouble toString(double) valueOf -
8-e -Prints: true,false,false -Integer.MIN_VALUE represents the most negative number that can be represented by an int value.
Double.MIN_VALUE represents the smallest positive value that is greater than zero.
9-c -Prints: 0,0,-0.0,-0.0 -The Math.min method is overloaded. There are versions that accept parameters of types int, long, float, and double.
The float and double versions support the concept of negative and positive zero, and they view negative zero as being smaller than positive
zero. The int and long versions ignore the sign of zero.
10-c -Compile-time Error -Integer has two constructors. The first accepts a parameter of type primitive int. The second accepts a parameter of
type String. There is no constructor that accepts a reference to an object of type Integer.
11-h -Prints: true,true,true -The Boolean class contains two instances of Boolean objects that are declared public static final Boolean. The first
is Boolean.FALSE and wraps the primitive boolean value false. The other is Boolean.TRUE and wraps the primitive boolean value true. The
equality operator will return the value true if the left and right operand are both Boolean.TRUE or if the left and right operand are both
Boolean.FALSE. The same is true if the results of the Boolean.valueOf method are compared with the equality operator because the
comparison is really just a comparison of references to the constants Boolean.TRUE or Boolean.FALSE. Please note that Boolean.valueOf is
generally preferable over the use of the Boolean constructor because the Boolean.valueOf method does not create a new instance of a Boolean
object. The valueOf method is much faster than the use of the constructor and also saves memory. The use of the Boolean constructor is not
recommended in any situation where the valueOf method is applicable.
12-c -Prints: 6.0 -Long is a subclass of the abstract class Number and implements all of the methods of Number such as byteValue,
shortValue, floatValue, etc. In this case, the Long object contains the value 1L. Please note that there is no charValue method.
13-a -Prints: 8000000000000000 -A primitive long is a 64 bit value. The minimum value is represented by a 64 bit value with the sign bit set to
one and all the other bits set to zero. Each hexadecimal digit represents four bits so the result will have 64 divided by 4 equals 16 hexadecimal
digits. The first will be the number 8 because the most significant bit is set to one and the rest are zero.
14-f -Returns the smallest whole number that is greater than or equal to the argument value. -The ceil method is not overloaded and declares
only one parameter of type double. The return value is always of type double. Ceil does not declare any exceptions.
15-d -Prints: 12,true -The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object,
char array, or any primitive type. The toString method returns a reference to the String object. It does not create a new instance of the String.
16-g -None of the Above -The Boolean objects are converted to Strings before being placed into the internal character array of the
StringBuffer. After the first Boolean is appended to the StringBuffer, the StringBuffer has a length of four characters. The insert operation inserts
the word false into the StringBuffer starting a character position one. The result is the character sequence tfalserue.
17-d -Run time Error -The constructor for the StringBuffer class does not verify that the input reference parameter is not null before attempting
to invoke methods on the String object. As a result, passing a null reference to the constructor of the StringBuffer class results in an error at run
time. Please note that a null String reference can safely be passed to the StringBuffer.append method, but a null literal may not be passed to
StringBuffer.append.
18-e -Prints: true,false,false -The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the
expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String.
Since the equality operator compares the references to objects rather than the contents of the objects, the equality operator returns the value
false. The declaration of variables "c" and "d" with the final modifier does not transform the expressions into compile time constants. Instead, it
just prevents variables "c" and "d" from being changed after initialization at runtime. However, if variables "a" and "b" had been declared final
then the result would be different.

Capítulo 10 – The java.lang Package (D)


No.-Answer-Remark
1-d -Runtime Error -The Long.parseLong method accepts a String parameter that represents a numeric value. Long.parseLong assumes that
the input String represents a decimal value unless a second parameter is provided to specify the radix. All of the characters in the String must
be digits of the specified radix. The Long.parseLong method is not able to determine the radix of the input String based on the format of the
number. The code presented for this question produces a NumberFormatException at run time as a result of the attempt to pass in 0xf as an
input parameter.
2-a -Prints: -128,127 -
3-g -Prints: true,true,false -Integer.equals overrides Object.equals. The Integer.equals method compares the data values contained in the
Integer instances. If the argument is of type Integer and if the value contained in the argument instance is the same as the value contained in

Certified Java Programmer Mock Exam 21


the instance on which the method is invoked then the result is true. The equality operator, ==, does not compare data values. Instead, the
equality operator compares references. Since unique instances always have unique reference values the result is false.
4-b -Prints: false,true -The expression (b1==b2) compares the references of two instances of Byte objects. The result is false. The expression
(b1.equals(b2)) compares the values of two instances of byte objects. Since both instances contain the value one the return value of the method
call is true.
5-b -Compiler Error -A compiler error is generated as a result of the attempt to assign the return value of valueOf to a primitive byte variable.
The Byte.valueOf method is overloaded. The return value of both versions is a Byte object.
6-e -Prints: true,false,false -Long.MIN_VALUE represents the most negative number that can be represented by an long value.
Double.MIN_VALUE represents the smallest positive value that is greater than zero.
7-i -Compiler error -The String.indexOf method will accept an empty String, but not an empty char literal.
8-e -Prints: true,false,false -Integer.equals overrides Object.equals. The Integer.equals method compares the data values contained in the
Integer instances. If the argument is of type Integer and if the value contained in the argument instance is the same as the value contained in
the instance on which the method is invoked then the result is true. If the argument is not of type Integer then the result is false.
9-b -Prints: double primitive -The Double.parseDouble method returns a double primitive.
10-e -Compiler Error -The Math.random method returns a value of type double. The value is always positive. The range of values is greater
than or equal to zero and less than one. The Math.random method does not accept a seed value. If you need to use a seed, then use
java.util.Random.
11-b -Prints: 4.0 -Double has two constructors. The first accepts a parameter of type primitive double. The second accepts a parameter of type
String. Java will automatically widden primitive numeric types to type double.
12-h -None of the Above -The math.round method is overloaded. If the argument is of type double then the return value is of type long. If the
argument is of type float then the return value is of type int.
13-a -Prints: 5 -Integer has two constructors. The first accepts a parameter of type primitive int. The second accepts a parameter of type
String.
14-c -Prints: 0 -If the input parameter is NaN, then the result is zero.
15-c -Prints: ABCDE,16 -The setLength method has been used to reduce the length of the StringBuffer from seven down to five. As a result the
last two characters are truncated. The capacity remains 16.
16-e -Prints: AB C D EF -The trim method creates a new String Object with the leading and trailing white space removed.
17-e -Prints: 0,16 -The length is zero because the StringBuffer contains no characters. The default capacity of a StringBuffer is 16.
18-c -Compiler Error -An attempt to pass a null literal to the append method will result in a compile time error due to an ambiguous reference
type. However, if a null string reference is passed to the append method as the input parameter, then the null reference will be passed to the
String.valueOf method and the result will be appended to the StringBuffer.

Capítulo 10 – The java.lang Package (E)


No.-Answer-Remark
1-e -None of the Above -The random method is not overloaded and does not declare any parameters and does not throw any exceptions. The
returned value is greater than or equal to zero and less than but not equal to one.
2-c -Compiler Error -The Byte.parseByte method returns a primitive byte. A compiler error is generated as a result of the attempt to assign the
return value to a Byte reference.
3-c -Compile-time error at line 1. -The parseByte and valueOf methods use a second parameter, radix, to specify the base of the number
system. For example, if the radix is two then the number is binary. If the radix is 8 then the number is octal. The Byte constructor does not
accept a radix parameter.
4-a -Prints: int primitive -The Integer.parseInt method returns an int primitive.
5-a c e f -Four overloaded versions of abs exist. The type of the return value depends on the type of the argument. If the argument is greater
than or equal to zero then the returned value is equal to the argument. If the argument, arg, is less than zero then the returned value is -arg. -
The abs method is overloaded. Four versions exist with one for each of the four argument types--int, long, float, double. The type of the return
value is the same as the declared argument type. No exceptions are declared.
6-c -Compiler Error -Long has two constructors. The first accepts a parameter of type primitive long. The second accepts a parameter of type
String. The attempt to pass primitiveFloat to a constructor generates a compiler error because method invocation type conversions do not
include implicit narrowing conversions such as the narrowing of a float to a long.
7-e g -parseDouble valueOf -
8-d -Prints: true,true -An int is a 32 bit value. The left most bit is the sign bit. The sign bit is set to one for negative numbers and is set to zero
for positive numbers.
9-g -valueOf -
10-h -valueOf -The valueOf method is static. It returns a String representation of the argument. The StringBuffer methods append, delete, and
insert are not members of the java.lang.String class. A typical trick question will attempt to invoke StringBuffer methods on a String object.
11-a b c d -doubleValue floatValue intValue longValue -
12-c -Prints: true,false -The Math.sin, Math.cos, and Math.tan methods all accept an input parameter of type double that represents an angle
measured in radians. The return value of each method is of type double.
13-b -Prints: A B C -Strings are immutable. No method will change the contents of a String object. Some String methods such as concat,
replace, substring and trim will return a new String with the desired modifications. In this case, the String returned by the methods trim and
concat are ignored.
14-g -Run time Error -Byte.parseByte is overloaded. The first version accepts a String representation of an integer as the input parameter. The
second version accepts both a String representation of an integer and also a radix. The code presented for this question generates a run time
error as a result of the attempt to pass in a String that contains a prefix used to specify a hexadecimal integer. Byte.parseByte does not examine
the format of the String to determine the radix. Instead, parseByte uses the radix argument to determine the radix.
15-b -Prints: 7FFFFFFFFFFFFFFF -A primitive long is a 64 bit value. The maximum value is represented by a 64 bit value with the sign bit set
to zero and all the other bits set to one. Each hexadecimal digit represents four bits so the result will have 64 divided by 4 equals 16
hexadecimal digits. All of the answer options that do not have 16 hexadecimal digit can be eliminated. The left most bit is zero and all of the
other bits are set to one so the first hexadecimal digit must be a 7 which represents a zero bit followed by three bits that are set to one. All of the
other hexadecimal digits must be F which represents four bits set to one.
16-b -Prints: null -If a null String is passed to the append method as the input parameter, then the null reference will be passed to the
String.valueOf method and the result will be appended to the StringBuffer. Please note that an attempt to pass a null literal to the append
method will result in a compile time error due to an ambiguous reference type. Also, an attempt to pass a null String reference to the constructor
of the StringBuffer class results in a run time error.
17-a -Prints: false,false -Strings are immutable. String methods such as String.toLowerCase ans String.replace create and return a new
immutable String. In this case, the new String Object was ignored.
18-c -Prints: 100,100 -The setLength method has been used to increase the length of the StringBuffer from seven up to 100. As a result the
capacity is also increased to 100. If the length had been set to a value less than 34, then the capacity would have been set to 34. The smallest

Certified Java Programmer Mock Exam 22


capacity increase is defined by the following: newCapacity = (oldCapacity + 1) * 2. In this case, the old capacity was 16 so the smallest new
capacity would be 34.

Capítulo 10 – The java.lang Package (F)


No.-Answer-Remark
1-a b -intValue parseInt -Integer.valueOf returns an instance of the Integer wrapper class.
2-d -Prints: -1,-1,true -Double is a subclass of the abstract class Number and implements all of the methods of Number such as byteValue,
shortValue, floatValue, etc. In this case, the Double object contains the value 0xFFFF. When that value is cast to a byte the result is 0xFF which
is also the two's compliment representation of a byte value that is equal to negative one. Similarly, 0xFFFF is the two's compliment
representation of a short value that is equal to negative one. Please note there is no Double.charValue method.
3-d -Prints: 1,2 -The result is calculated by adding 0.5 to the argument and then taking the Math.floor of the result. In this case, the input
parameter is a float so the return value is an int. If the input parameter had been a double, then the return value would have been a long.
4-d -Prints: true,true -The String literals ("A"+"B") and ("AB") both refer to the same String Object in memory; therefore, the equality operator
returns true when the literals are compared. The variable c refers to the same String object in memory.
5-h -None of the Above -The math.round method is overloaded. If the argument is of type double then the return value is of type long. If the
argument is of type float then the return value is of type int.
6-a b c d e f g h i j k -abs ceil floor max min random round sin cos tan sqrt -All methods of the java.lang.Math class are static.
7-d -Prints: false,NFE -The Integer constructor is able to recognize both octal and hexadecimal literals but the parseInt method is not able to
recognize the prefix characters that are used to specify octal and hexadecimal values. The prefix character, 0, used to declare an octal literal is
ignored by the parseInt method and the remainder of the String is parsed as a decimal value. The prefix characters, 0x, used to declare a
hexadecimal value will cause the parseInt method to throw a NumberFormatException. In this program the octal literal 010 is correctly
interpreted by the Integer constructor but is misinterpreted by the Integer.parseInt method. The result of the comparison of the two results is
false. The Integer constructor is able to correctly interpret the hexadecimal literal, 0xf, but the Integer.parseInt method throws a
NumberFormatException.
8-b -Prints: 3,9,11,17 -The Integer.parseInt method parses the input String parameter and returns an int primitive value that is equal to the
value represented by the String. The second parameter, if present, is the radix. For example, the String value 11 with a radix of 2 is parsed as 2
plus 1 equals 3. Similarly, the String value 11 with a radix of 8 is an octal value that is parsed as 8 plus 1 equals 9. The String value 11 with a
radix of 10 is a decimal value that is parsed as 10 plus 1 equals 11. The String value 11 with a radix of 16 is a hexadecimal value that is parsed
as 16 plus 1 equals 17.
9-c -Prints: -0.0,0.0 -The Math.min and Math.max methods are overloaded. This version of the Math.min method accepts two parameters of
type double and returns the value that is closer to negative infinity. This version of the Math.max method accepts two input parameters of type
double and returns the value that is closer to positive infinity. Both Math.min and Math.max view -0.0 as being smaller than +0.0.
10-c -Compiler Error -Double has two constructors. The first accepts a parameter of type primitive double. The second accepts a parameter of
type String. There is no constructor that accepts a reference to an object of type Double.
11-g -round -
12-i -None of the above. -Strings are immutable. No method will change the contents of a String object. Some String methods such as concat,
replace, substring and trim will return a new String with the desired modifications. The StringBuffer methods append, delete, and insert are not
members of the java.lang.String class. A typical trick question will attempt to invoke StringBuffer methods on a String object.
13-e -Run-time Error -The Integer.parseInt method is not able to determine the radix of the input String based on the format of the number.
Integer.parseInt assumes that the input String represents a decimal value unless a second parameter is provided to specify the radix. The code
presented for this question produces a NumberFormatException at run time.
14-a b c d e g i -doubleValue floatValue intValue longValue parseDouble toString(double) valueOf -There is no Double.toHexString or
Double.getDouble method.
15-f -Run-time Error -None of the methods, parseDouble, valueOf, or the constructor, are able to decode a prefix that specifies a radix.
16-b -Prints: A B C -The String, s2, is passed to the m1 method by passing the value of the reference. The reference value used in method m1
is a local copy of the reference. If the copy used in method m1 is changed then the original copy in the main method is not changed.
17-c -Prints: true,false -The String literals ("A"+"B") and ("AB") are evaluated at compile time and found to be equal. Therefore, both share the
same String Object at runtime. When the two literals are compared at runtime using the equality operator, they are found to refer to the same
object. Therefore, the equality operator returns the value true because the references are identical. In contrast, the expression (a+b) is
evaluated at runtime and produces a new instance of a String Object that contains "AB". Therefore, the equality operator returns false because
the object references are not identical.
18-h -Prints: true,true,true -The key to understanding this program is understanding the impact of declaring variables "a" and "b" with the "final"
modifier. Since "a" and "b" are final, all of the String expressions become compile-time constant expressions that evaluate to "AB". As a result,
all are represented by a single String Object at runtime. Therefore, the equality operator finds that the references are equal and returns the
value true.

Capítulo 10 – The java.lang Package (G)


No.-Answer-Remark
1-b -Prints: false,false,true -The Math.round method is overloaded. If the argument is a float, then the return value is an int. If the argument is a
double, then the return value is a long. If the argument is a double and is greater than or equal to Long.MAX_VALUE then the return value is
Long.MAX_VALUE.
2-d -Prints: false,true,true -If one of the methods trim, toUpperCase, toLowerCase concat or replace is invoked on a String object that requires
no changes then a reference to the existing String instance is returned. In other words, the method invokes the statement "return this".
3-c -Prints: 0,0,-1 -The overloaded String.indexOf methods accept either char or String parameters. An optional offset parameter may be used
to specify the starting index for the search. In this case, the starting index is one, but the index of the A character is zero. As a result, negative
one is returned.
4-e -Compiler Error -The Math.min and Math.max methods are overloaded. This version of the Math.min method accepts two parameters of
type double and returns the value that is closer to negative infinity. The type of the return value is double. The attempt to assign the double
return value to a variable of type float generates a compiler error. Similarly, This version of the Math.max method accepts two parameters of
type double and returns the value that is closer to positive infinity. The type of the return value is double. The attempt to assign the double return
value to a variable of type float generates a compiler error.
5-a -Prints: true -The Math.sqrt method calculates the square root of a number. The Math.exp method raises e to the power specified by the
argument. Math.E is the value of e.
6-c -Prints: false,true,false -The Math.round method is overloaded. If the argument is a float, then the return value is an int. If the argument is a
double, then the return value is a long. If the argument is a float and is less than or equal to Integer.MIN_VALUE then the return value is
Integer.MIN_VALUE.
7-b c -Compiler Error at 1 Compiler Error at 2 -The Math.min method is overloaded. There are versions that accept parameters of types int,
long, float, and double. The type of the return value is the same as the type of the input parameter. If a byte or a short is passed to the Math.min
Certified Java Programmer Mock Exam 23
method as an input parameter, then it will be promoted to a type that is accepted by Math.min. The type that it is promoted to will depend on the
type of the other parameter.
8-a -It is not overloaded. -The argument is of type double.
9-a -Prints: 10.0 -The Math.abs method is overloaded. If the argument is of type int, then the return value is an int. If the argument is of type
long, then the return value is a long. If the argument is of type float, then the return value is a float. If the argument is of type double, then the
return value is a double.
10-l -None of the Above -
11-d -Prints: 1.0,2.0 -The Math.ceil method accepts an argument of type double and returns a double. The returned value is the smallest whole
number that is greater than or equal to the argument.
12-a c d -append delete insert -The StringBuffer class has methods named append, delete and insert but the String class does not. A typical
trick question will attempt to invoke StringBuffer methods on a String object.
13-d -Prints: int,int,long -The Math.min method is overloaded. There are versions that accept parameters of types int, long, float, and double.
The type of the return value is the same as the type of the input parameter. If a byte or a short is passed to the Math.min method as an input
parameter, then it will be promoted to a type that is accepted by Math.min. The type that it is promoted to will depend on the type of the other
parameter. If both parameters are of type byte, then both are promoted to an int. If one is of type byte and the other is of type int, then the byte
is promoted to an int. If one is of type byte and the other is of type long, then the byte is promoted to a long.
14-e -Prints: true,false,false -The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the
expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String.
Since the equality operator compares the references to objects rather than the contents of the objects, the equality operator returns the value
false.
15-c -Compiler Error -A compiler error is generated due to the attempt to access the length method of the String class as though it were a
variable.
16-b -Prints: 1,0,-1 -The String.compareTo method returns an integer. The returned value is zero if both strings are equal. Otherwise, a
negative or positive value is returned depending on which string precedes the other when the two are compared character-by-character
according to Unicode ordering.
17-b -Prints: false,true -The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the
expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String.
The String.intern() method causes the JVM to share a previously existing String object if one is found that contains the same value. The
expression ("A"+"B") is a compile-time constant and therefore its String object can be shared with other compile-time constant Strings with the
same value or with Strings that have had their intern method invoked.
18-b -Prints: false,true -The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the
expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String.
The String.intern() method causes the JVM to share a previously existing String object if one is found that contains the same value.

Capítulo 10 – The java.lang Package (H)


No.-Answer-Remark
1-e -The returned value is of type long only if the argument is of type double. -
2-a -Prints: 0,0 -The math.round method adds 0.5 to the argument and then takes the floor of the sum. The result of Math.round with an
argument of -3.6 is -4. The result of Math.round with an argument of 3.6 is 4.0. The sum of the two results is zero.
3-d -Prints: double,double,double -The Math.sin, Math.cos, and Math.tan methods return a value of type double.
4-e -Prints: FDDD -There are four overloaded versions of the abs method with parameter types of int, long, float and double. The return type is
the same as the parameter type. The sqrt method is not overloaded. The parameter type is double and the return type is double.
5-a c -It is not overloaded. The argument is an angle measured in radians. -The argument is of type double and represents an angle
measured in radians. This method does not throw any exceptions.
6-b -Prints: true -The magnitude of Integer.MIN_VALUE is one greater than the magnitude of Integer.MAX_VALUE. Therefore, it is not possible
to use an int variable to correctly represent the absolute value of Integer.MIN_VALUE. The binary representation of Integer.MIN_VALUE is a
single bit set to one followed by 31 bits set to zero. To calculate the two's compliment of Integer.MIN_VALUE all of the bits must be inverted and
then one must be added. When all of the bits are inverted the result is a single zero followed by 31 bits set to one. That is also the binary
representation of Integer.MAX_VALUE. The final step of calculating the two's compliment is to add one. The result is a single bit set to one
followed by 31 bits set to zero. Once again, that is the binary representation of Integer.MIN_VALUE.
7-c -Adds 0.5 to the argument and takes the floor of the result. -
8-e -Compiler Error -There are two versions of the Math.round method. One accepts a float parameter and returns an int. The other accepts a
double parameter and returns a long. A compiler error is generated here due to the attempt to assign the long return value to an int parameter.
9-l -None of the Above -
10-c -Prints: -4.0,-3.0,3.0,4.0 -The Math.floor method returns a primitive of type double that is equal to an integral value and is larger than any
other such value that is smaller than the argument. For example, negative four is the floor of negative 3.6. Similarly, the Math.ceil method
returns a primitive of type double that is equal to an integral value and is smaller than any other such value that is larger than the argument. For
example, negative 3.0 is the ceil of negative 3.6. The term larger refers to values that are closer to positive infinity. For example, negative three
is larger than negative four because negative three is closer to positive infinity.
11-e -Prints: DDDLDD -The round method does not return either of the two floating point primitive types. The other methods named in this
question are able to return a floating point primitive type. The sin and sqrt methods return only a primitive double type. The abs, ceil and max
methods are able to return either a float or a double depending on the parameter type.
12-c -Prints: -0.0,0.0 -The Math.min and Math.max methods are overloaded. This version of the Math.min method accepts two parameters of
type float and returns the value that is closer to negative infinity. This version of the Math.max method accepts two input parameters of type float
and returns the value that is closer to positive infinity. Both Math.min and Math.max view -0.0 as being smaller than +0.0.
13-d -Prints: IIILFD -There are four overloaded versions of the Math.min method with versions that declare parameters of type int, long, float
and double. The return type is the same as the parameter type. Arguments of type byte, char and short are promoted to type int.
14-d -Prints: double -The Math.random method returns a value of type double. The value is always positive. The range of values is greater
than or equal to zero and less than one.
15-g -Prints: FDIL -
16-f -None of the Above -If the java.lang.Math.round method is invoked with a primitive float type and the result exceeds the range of type int
then the result is Integer.MAX_VALUE.
17-e -Prints: DDDD -The floor and ceil methods are not overloaded. There is only one version of each method. The parameter type is double
and the return type is double.
18-a -Prints: 0.0,1.0 -The Math.floor method accepts an argument of type double and returns a double. The returned value is the largest whole
number that is smaller than or equal to the argument.

Capítulo 11 – Collections (A)


Certified Java Programmer Mock Exam 24
No.-Answer-Remark
1-c d e f g -TreeMap TreeSet HashMap HashSet WeakHashMap -The Vector and Hashtable methods are synchronized and do not allow
for simultaneous access by multiple threads. The concrete subclasses of the AbstractList, AbstractMap, and AbstractSet classes allow for
unsynchronized read operations by multiple threads. Additionally, the sychronized wrapper methods of the Collections class allow for the
instantiation of a Collection, List, Map, Set, SortedMap, or SortedSet with synchronized methods. If simultaneous read and write operations are
necessary then a synchronized instance should be used.
2-h -Prints: true,true,true -
3-a -false -
4-b -true -
5-e -Prints: true,false,false -HashSet is a subclass of AbstractSet and AbstractCollection and therefore implements the Collection interface.
HashMap extends AbstractMap and implements the Map interface. HashMap does not implement the Collection interface. Hashtable extends
Dictionary and implements the Map interface. Hashtable does not implement Collection.
6-b -Programmer B -Both implementations are technically legal. If two instances of class C are equal, then the field of type int in both classes
will contain the same value and produce a hashcode that is equal. However, by ignoring the contents of the array it is possible that a very large
number of unequal objects will produce the same hashcode causing the distribution of the objects across a hashtable to be less than optimal.
For that reason, the algorithm suggested by programmer A is inferior to the algorithm suggested by programmer B. Therefore, programmer B
gets the higher pay raise.
7-d -Prints: true,true -TreeSet implements the Set interface and the SortedSet interface.
8-c -Calculate the hashCode using both the int field and the array, but only calculate the hashCode once and store the value for future use in an
instance variable. -The value of an immutable object does not change so the value of the hashcode will not change either. Therefore,
calculating the hashcode only once is acceptable.
9-c -LinkedList -The ArrayList and Vector both use an array to store the elements of the list so access to any element using an index is very
fast. The LinkedList is implemented using a doubly linked list so access an element requires the list to be traversed using the links.
10-e -Prints: true,false,false -The Vector.elements method returns an Enumeration over the elements of the vector. Vector implements the List
interface and extends AbstractList so it is also possible to get an Iterator over a Vector by invoking the iterator or listIterator method.
11-b -TreeMap -The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and
Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a Hashtable.
TreeMap and TreeSet store elements in a sorted order based on the key, but the TreeSet does not support key/value pairs.
12-h -Prints: true,true,true -Vector has been upgraded to implement the List interface.
13-a -false -If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal
according to the equals method, then the hashcodes may or may not be equal. It is preferable that unequal objects have different hashcodes,
but that is not always possible. Since the hashcode value is a 32 bit primitive int it is not possible to produce a unique hashcode for each value
of a primitive long.
14-b -true -If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal
according to the equals method, then the hashcodes may or may not be equal. It is preferable that unequal objects have different hashcodes,
but that is not always possible. Since the hashcode value is a 32 bit primitive int it is not possible to produce a unique hashcode for each value
of a primitive long.
15-e -HashSet -The elements of a Map are key/value pairs so a Map is not a good choice. A List generally accepts duplicate elements. A Set
stores a collection of unique elements. Any attempt to store a duplicate element in a Set is rejected. Adding and removing an element in a
TreeSet involves walking the tree to determine the location of the element. A HashSet stores the elements in a hash table so elements in a
HashSet can be accessed almost as quickly as elements in an array.
16-e -SortedMap -The List and Set interfaces do not support key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate
entries. A Map organizes the entries as key/value pairs. The SortedMap is similar to a Map except that the ordering of the elements is
determined by a comparator or the Comparable interface.
17-b -Prints: false,false,true -This is a trick question. The Collections class is not the same as the Collection interface. The Collections class
contains a variety of methods used to work with collections. For example, Collections.shuffle is used to randomly shuffle the elements of a
Collection. Similarly, the Arrays class provides utility methods for working with arrays.
18-e -LinkedHashSet -The elements of a Map are key/value pairs so a Map is not a good choice. A List generally accepts duplicate elements.
A Set stores a collection of unique elements. Any attempt to store a duplicate element in a Set is rejected. TreeSet stores elements in a sorted
order based on the key. HashSet does not sort the elements based on the key. The iteration order of LinkedHashMap and LinkedHashSet is
clearly defined. By default, the interation order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted.
While a LinkedHashSet rejects duplicate entries, the LinkedHashMap allows duplicate entries to replace old entries.
19-d -LinkedList -A stack or queue must be implemented using a data structure that stores the elements based on the order of insertion. Any
data structure that is implemented using a hash table is not a good choice. The ArrayList and Vector are both implemented using an internal
array. Although an array allows elements to be easily organized based on the order of insertion an array does not allow the list of elements to be
easily shifted in memory as elements are appended to the tail of the list and removed from the head of the list. The LinkedList is implemented
using a doubly linked list that allows elements to be easily appended to the tail of the list and removed from the head of the list.

Capítulo 11 – Collections (B)


No.-Answer-Remark
1-d -SortedSet -The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.
The SortedSet is similar to a Set except that the ordering of the elements is determined by a comparator or the Comparable interface.
2-g -Prints: true,true,false -The List and Set interfaces extend the Collection interface but Map does not. TreeMap implements Map and
therefore is not an instance of Collection.
3-d -Prints: false,true,true -ListIterator extends Iterator.
4-g -Prints: true,true,false -HashMap does not implement the Collection interface.
5-h -Prints: true,true,true -All three implement Cloneable.
6-c -LinkedList -The ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the
list the elements that follow the insertion point must be shifted to make room for the new element. The LinkedList is implemented using a doubly
linked list. An insertion at any point requires only the updating of the links. Therefore, the LinkedList allows for fast insertions and deletions.
7-d -Prints: false,true,true -HashSet implements the Set interface but not the Map interface. HashMap extends AbstractMap and implements
the Map interface. Hashtable extends Dictionary and implements the Map interface.
8-a -List -The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A List
allows entries to be accessed using an index.
9-b -Map -The List and Set interfaces do not support key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.
10-c -Prints: false,true,false -LinkedHashMap does not implement the Collection interface or the List interface.
11-c -Prints: false,true,false -A ListIterator can be obtained by invoking the listIterator method.

Certified Java Programmer Mock Exam 25


12-a -Prints: 1026 -The hashCode is calculated using all three array elements. Since the object is immutable, the hashcode is only calculated
the first time hashCode is invoked.
13-e -None of the above. -The Iterator interface declares three methods: hasNext, next and remove. The ListIterator interface was introduced
in Java 1.2 along with the Iterator interface. The ListIterator interface extends the Iterator interface and declares additional methods to provide
forward and backward iteration capabilities, List modification capabilities, and the ability to determine the position of the iterator in the List. The
ListIterator interface does not extend the List interface.
14-g -Prints: true,true,false -LinkedHashSet does not implement the List interface.
15-c -LinkedList -The LinkedList provides methods that facilitate the implementation of stacks and queues.
16-f -HashMap -The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and
Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a Hashtable.
TreeMap and TreeSet store elements in a sorted order based on the key. The iteration order of LinkedHashMap and LinkedHashSet is not
unspecified. By default, the interation order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted.
Optionally, the iteration order of the LinkedHashMap can be set to the order in which the elements were last accessed.
17-g -None of the above. -The RandomAccess interface is a marker interface so it does not declare any methods. Its purpose is to provide
information about the RandomAccess capabilities of a List implementation. Generic list algorithms can check to see if an instance of a List
implements the RandomAccess marker interface. If not, then the algorithm can avoid operations that require fast random access. Both Vector
and ArrayList implement the RandomAccess interface. LinkedList does not implement RandomAccess.
18-b -Prints: false,false,true -This is a trick question. The Collections class is not the same as the Collection interface. The Collections class
contains a variety of methods used to work with collections. For example, Collections.shuffle is used to randomly shuffle the elements of a
Collection. Similarly, the Arrays class provides utility methods for working with arrays.

Capítulo 11 – Collections (C)


No.-Answer-Remark
1-c -Prints: true,false -HashSet implements the Set interface but not the SortedSet interface.
2-a b c d j -ArrayList LinkedHashMap LinkedHashSet LinkedList Vector -The HashMap, HashSet, Hashtable, and WeakHashMap are all
implemented with an internal hash table that organizes the elements in buckets according to the hash code and not according to the order of
insertion. Although the LinkedHashMap and LinkedHashSet classes also use an internal hash table they both also maintain a linked list through
all of the elements and the list is ordered based on the order of insertion. Optionally, the LinkedHashMap can also maintain the order of the list
based on the time of the most recent access of each element. Both the Vector and ArrayList classes are implemented using an array that
appends elements to the end of the list and so the elements are naturally ordered according to the order of insertion. The LinkedList class is
implemented based on a doubly linked list that appends new elements to the end of the List so elements are naturally stored in the order of
insertion. Of course, the ArrayList, LinkedList, and Vector also allow new elements to be inserted at any point in the List. The TreeMap and
TreeSet classes are both implemented using a tree structure that is ordered based on a comparator or the Comparable interface.
3-b d -The Enumeration interface declares only two methods: hasMoreElements and nextElement. The Iterator interface declares a total of
three methods. -The Enumeration interface was introduced with Java 1.0 to provide an easy means of moving through the elements of a Vector
or the key or values of a Hashtable. The Iterator interface was introduced with the collections framework with Java 1.2. The Iterator interface
declares three methods: hasNext, next and remove. The first two methods, hasNext and next, are similar to the two methods declared in the
Enumeration interface, hasMoreElements and nextElement. The third method of the Iterator interface, remove, provides new functionality
relative to the Enumeration interface.
4-f -Prints: true,false,true -The List and Set interfaces extend the Collection interface but Map does not. WeakHashMap implements Map and
therefore is not an instance of Collection.
5-g -WeakHashMap -Objects become eligible for garbage collection when all references to the object are removed from active threads. With
the exception of a WeakHashMap a key stored in a Map is not garbage collected when the references to the key are removed. A
WeakHashMap can be used to avoid the memory leaks generally associate with the use of other implementations of the Map interface.
6-c -TreeSet -There is no requirement to store key/value pairs so a Map is not a good choice. A List generally accepts duplicate elements. A
Set stores a collection of unique objects. Any attempt to store a duplicate object is rejected. TreeSet stores elements in an order that is
determined either by a comparator or by the comparable interface.
7-d -Prints: false,true,true -The List and Set interfaces extend the Collection interface but Map does not. HashMap implements Map and
therefore is not an instance of Collection.
8-d -Prints: false,true,true -LinkedHashMap does not implement the Collection interface.
9-c -Set -The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.
10-d -HashMap -The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and
Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a Hashtable.
TreeMap and TreeSet store elements in a sorted order based on the key.
11-c -LinkedHashSet -The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The
iteration order of a LinkedHashSet is determined by the order in which elements are inserted. When a new LinkedHashSet is created by passing
a reference to an existing Collection to the constructor of a LinkedHashSet, the Collection.addAll method will ultimately be invoked. The addAll
method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance of the
new LinkedHashSet. Since the iteration order of the LinkedHashSet is determined by the order of insertion, the iteration order of the new
LinkedHashSet must be the same as the interation order of the old Collection.
12-a f h -LinkedHashMap HashMap Hashtable -The requirement to store key/value pairs is directly satisfied by a concrete implementation of
the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The Hashtable, HashMap, and
LinkedHashMap classes store elements in a hash table. Elements are accessed using a hash code that identifies the bucket that contains the
element. Access time is therefore not dependent on the number of buckets. As long as the hash code methods of the elements are properly
implemented, the time required to access an element in a hash table remains constant as the number of buckets in the hash table grows. In
contrast, the TreeMap and TreeSet classes store elements in a sorted order in a tree structure. Access to any element requires walking the tree
so access time depends on the size of the tree.
13-a c d e -The Iterator interface declares only three methods: hasNext, next and remove. The ListIterator interface provides forward and
backward iteration capabilities. The ListIterator interface provides the ability to modify the List during iteration. The ListIterator interface
provides the ability to determine its position in the List. -The ListIterator interface extends the Iterator interface and declares additional methods
to provide forward and backward iteration capabilities, List modification capabilities, and the ability to determine the position of the iterator in the
List.
14-h -Hashtable -The requirement to store key/value pairs is directly satisfied by a Hashtable or any concrete implementation of the Map
interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to NOT allow null elements is
satisfied by HashTable but not by HashMap or any of the other Collection implementations that were introduced with Java 1.2 and later.
15-f -Prints: true,false,true -The RandomAccess interface is a marker interface so it does not declare any methods. Its purpose is to provide
information about the RandomAccess capabilities of a List implementation. Generic list algorithms can check to see if an instance of a List

Certified Java Programmer Mock Exam 26


implements the RandomAccess marker interface. If not, then the algorithm can avoid operations that require fast random access. Both Vector
and ArrayList implement the RandomAccess interface. LinkedList does not implement RandomAccess.
16-a -LinkedHashMap -The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The
List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a
Hashtable. The LinkedHashMap offers the option to remove the least recently used element when a new element is added. The LinkedHashSet
does not offer the LRU option.
17-c -LinkedHashMap -The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The
iteration order of a LinkedHashMap is determined by the order in which elements are inserted. When a new LinkedHashMap is created by
passing a reference to an existing Collection to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be invoked. The
addAll method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance
of the new LinkedHashMap. Since the iteration order of the LinkedHashMap is determined by the order of insertion, the iteration order of the
new LinkedHashMap must be the same as the interation order of the old Collection.
18-b -LinkedHashMap -The LinkedHashMap offers the option to remove the least recently used element when a new element is added. The
removeEldestEntry method is invoked by the put and putAll methods. If removeEldestEntry returns true, then the least recently used element is
removed. A typical implementation of the method returns true if the size of the LinkedHashMap exceeds the desired maximum.

Certified Java Programmer Mock Exam 27

You might also like