Java Programmer Mock Exam Questions
Java Programmer Mock Exam Questions
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
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.
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);
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
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[]) {
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;
}
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
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
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
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.
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) {
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
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
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
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);
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;
}
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.
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. }
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.
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
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
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.
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
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
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
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++;}
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
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
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
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
}
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.
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
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
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
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
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
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
}
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
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;}
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
Question 2
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I();
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.
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();
}
}
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());
}
}
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.
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 {
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
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();
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.
Question 2
Which of the following methods are static members of the Thread class?
a. join
b. notify
c. notifyAll
d. resume
e. run
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.
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
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
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
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
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
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[]) {
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
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";}
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
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)) + ",");
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
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
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
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
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.
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
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
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
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
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
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.
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
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 {
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
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.
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
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 -