You are on page 1of 13

Certified Java Programmer Mock Exam

--------------------------------------------------------------------------------

Question 1
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 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

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
e. Prints: B C D
f. Prints: C D E
g. Compiler Error
h. Run time Exception
i. None of the Above

--------------------------------------------------------------------------------

Question 4

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

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 5

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 6

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 7

class D {
public static void main (String[] args) {
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
char d = 0x0031; // 4
char e = '\u0031'; // 5
System.out.print(""+a+b+c+d+e);
}
}

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 8

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 9

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. An attempt to run Basics1 from the command line results in a run-time error.
e. An attempt to run Basics2 from the command line results in a run-time error.
f. An attempt to run Basics3 from the command line results in a run-time error.
g. None of the Above

--------------------------------------------------------------------------------

Question 10

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 11

Which of these words belong to the set of java keywords?

a. clause
b. loop
c. expression
d. overrides
e. statement
f. assertion
g. validate
h. exception
i. cast
j. None of the Above

--------------------------------------------------------------------------------

Question 12

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 13

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 14

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. An attempt to run Basics1 from the command line results in a run-time error.
e. An attempt to run Basics2 from the command line results in a run-time error.
f. An attempt to run Basics3 from the command line results in a run-time error.
g. None of the Above

--------------------------------------------------------------------------------

Question 15

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: XABCDEF
b. Prints: ABCDEF
c. Compile-time error
d. Run-time exception
e. None of the Above

--------------------------------------------------------------------------------

Question 16

class C {
public static void main(String args[]) {
System.out.print(Long.toHexString(Long.MIN_VALUE)+",");
System.out.print(Long.toHexString(Long.MAX_VALUE));
}
}

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 17

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 18

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 19

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 20

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

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 21

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 22

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.

--------------------------------------------------------------------------------
No. Answer Remark
Null is converted to a string and is printed as the
1 b Prints: null
word null.
b
d Both "const" and "goto" are java keywords, but
2 strictfp super goto native
g neither is used.
h
The index for the first element of an array is
3 b Prints: BCD
zero.
Member variables are initialized automatically.
4 d Prints: 0
Type int variables are initialized to zero.
b
d
f catch instanceof const goto import Both "const" and "goto" are java keywords, but
5
i transient neither is used.
j
k
Variables declared inside of a block or method
are called local variables and they are not
automatically initialized. (Locally declared
arrays are an exception to the rule, but none are
6 f Compiler Error
used in this question.) The compiler will
generate an error as a result of the attempt to
access the local variables before a value has
been assigned.
All of the declarations are legal. The first three
are declared in octal format. The fourth is
7 f None of the Above
declared as a hexadecimal literal. The fifth is a
unicode escape sequence.
Both "const" and "goto" are java keywords, but
neither is used. Serializable, Runnable,
a
8 transient volatile Externalizable, and Cloneable are all interfaces.
e
Thread.run is a method. The keywords transient
and volatile are field modifiers.
Section 12.1.4 of the Java Language
Specification states the following, "The method
An attempt to run Basics1 from the
main must be declared public, static, and void. It
command line results in a run-time error.
d must accept a single argument that is an array of
An attempt to run Basics2 from the
9 e Strings." The rule is not enforced by the
command line results in a run-time error.
f compiler, but should instead be enforced by the
An attempt to run Basics3 from the
Java Virtual Machine. However, not every JVM
command line results in a run-time error.
enforces the rule. Even so, for the purposed of
the SCJP exam, the main method should be
No. Answer Remark
declared as stated in the Java Language
Specification. In this example, each of the three
main methods generates a run-time exception
because they are not declared static.
Unicode literals are declared using single quotes
but none of the declarations here use single
10 e None of the Above quotes. The declaration of char, b, is also
problematic because it contains more than one
char.
11 j None of the Above
a
b
c
d byte short int long float double boolean
12
g char
i
j
k
A byte is an 8 bit signed value. The left most bit
is the sign bit. The sign bit is set to zero for
positive numbers. An eight bit binary value is
represented as three octal digits. The first of the
three digits represents the left most two bits of
the binary value including any leading zeros.
Please note that Integer.toBinaryString does not
include the leading zeros. The second octal digit
13 b Prints: 1010101,125,85,55 represents the next three bits of the binary value.
The last of the octal digits represents the right
most three bits of binary value. An eight bit
binary value is represented as two hexadecimal
digits. The left hex digit represents the left most
bits of the binary value. The right hex digit
represents the right most bits of the binary value.
The decimal value of 85 can be converted to
binary as follows. 85=64+16+4+1=01010101
Section 12.1.4 of the Java Language
Specification states the following, "The method
An attempt to run Basics2 from the main must be declared public, static, and void. It
e command line results in a run-time error. must accept a single argument that is an array of
14
f An attempt to run Basics3 from the Strings." The rule is not enforced by the
command line results in a run-time error. compiler, but should instead be enforced by the
Java Virtual Machine. However, not every JVM
enforces the rule. Even so, for the purposes of
No. Answer Remark
the SCJP exam, the main method should be
declared as stated in the Java Language
Specification. Note: the Java Language
Specification provides the option to disallow the
declaration of more than one class in a single
source code file if more than one of the classes
in the file is referred to by code that exists
outside of the file. The restriction is stated as an
option and not a requirement. This option is not
enforced by any compiler that I have tested.
The index is incremented before the array access
expression is evaluated so the first element
accessed is at index one instead of zero. An
15 d Run-time exception
ArrayIndexOutOfBoundsException is thrown
when an attempt is made to access an element
past the end of the array.
A long is a 64 bit signed value. The left most bit
16 b Prints: 8000000000000000,7fffffffffffffff is the sign bit. The sign bit is zero for positive
numbers and one for negative numbers.
A byte is an 8 bit signed value. The left most bit
is the sign bit. The sign bit is set to zero for
positive numbers and is set to one for negative
numbers. The most positive byte value is
represented as a sign bit that is set to zero and all
of the other bits set to one. The
Integer.toBinaryString method does not print
leading zeros. An eight bit binary value is
represented as three octal digits. The first of the
three digits represents the left most two bits of
the binary value. In this case, the left most two
17 a Prints: 1111111,177,127,7f
bits are zero and one. The second octal digit
represents the next three bits of the binary value.
The last of the octal digits represents the right
most three bits of binary value. Note that the
Integer.toOctalString method does not print a
leading zero as is required for an octal literal
value. An eight bit binary value is represented as
two hexadecimal digits. The left hex digit
represents the left most bits of the binary value.
The right hex digit represents the right most bits
of the binary value.
A byte is an 8 bit signed value. A char is a 16 bit
18 c Prints: 7f,ffff,7fff
unsigned value. A short is a 16 bit signed value.
No. Answer Remark
The left most bit of a signed value is the sign bit.
The sign bit is zero for positive numbers and one
for negative numbers. The hexadecimal value 7f
is equal to the decimal value 127. The
hexadecimal value ff is equal to the decimal
value negative 128.
There are two primitive boolean values: true and
false. Both must be written with lower case
19 a boolean b1 = true; letters. Although the C programming language
accepts zero as a boolean value, the Java
programming language does not.
The numeric sum of variables a, b, c, d, and e is
20 c Prints: 0null zero. The zero is converted to a string and
concatenated with s.
The non-static field x can not be referenced from
e Compiler error at line 5. Compiler error at
21 within a static method. The local variable y has
f line 6.
not been initialized.
Generally speaking, numberic type variables are
initialized to zero. Booleans are initialized to
22 c Prints 0,0.0,0.0,false,null
false. Reference type variables are initialized to
null.