This is a mock exam for the Sun Certified Programmer for the Java 2 Platform (SCPJ2 1.4). It comprises brand new questions, which are similar to the questions that can be expected on the real exam. Working through this exam will give the reader a good indication of how well she is prepared for the real exam, and whether any topics need further study.
Q1 |
Given the following class, which statements can be inserted at position 1 without causing a compilation error?
public class Q6db8 {
int a;
int b = 0;
static int c;
public void m() {
int d;
int e = 0;
// Position 1
}
}
Select the four correct answers.
a++; b++; c++; d++; e++;
|
Q2 |
Which statements are true about the effect of the >> and >>> operators?
Select the three correct answers.
For non-negative values of the left operand, the >> and >>> operators will have the same effect. The result of (-1 >> 1) is 0. The result of (-1 >>> 1) is ?1. The value returned by >>> will never be negative if the left operand is non-negative. When using the >> operator, the left-most bit of the resulting value will always have the same bit value as the left-most bit of the left operand.
|
Q3 |
What is wrong with the following code?
class MyException extends Exception {}
public class Qb4ab {
public void foo() {
try {
bar();
} finally {
baz();
} catch (MyException e) {}
}
public void bar() throws MyException {
throw new MyException();
}
public void baz() throws RuntimeException {
throw new RuntimeException();
}
}
Select the one correct answer.
Since the method foo() does not catch the exception generated by the method baz(), it must declare the RuntimeException in a throws clause. A try block cannot be followed by both a catch and a finally block. An empty catch block is not allowed. A catch block cannot follow a finally block. A finally block must always follow one or more catch blocks.
|
Q4 |
What will be written to the standard output when the following program is run?
public class Qd803 {
public static void main(String[] args) {
String word = "restructure";
System.out.println(word.substring(2, 3));
}
}
Select the one correct answer.
est es str st s
|
Q5 |
Given that a static method doIt() in the class Work represents work to be done, which block of code will succeed in starting a new thread that will do the work?
Select the one correct answer.
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread(r);
t.start();
Thread t = new Thread() {
public void start() {
Work.doIt();
}
};
t.start();
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
r.start();
Thread t = new Thread(new Work());
t.start();
Runnable t = new Runnable() {
public void run() {
Work.doIt();
}
};
t.run();
|
Q6 |
What will be printed when the following program is run?
public class Q8929 {
public static void main(String[] args) {
for (int i=12; i>0; i-=3)
System.out.print(i);
System.out.println("");
}
}
Select the one correct answer.
12 129630 12963 36912 None of the above.
|
Q7 |
What will be the result of attempting to compile and run the following code?
public class Q275d {
static int a;
int b;
public Q275d() {
int c;
c = a;
a++;
b += c;
}
public static void main(String[] args) {
new Q275d();
}
}
Select the one correct answer.
The code will fail to compile since the constructor is trying to access static members. The code will fail to compile since the constructor is trying to use static field a before it has been initialized. The code will fail to compile since the constructor is trying to use field b before it has been initialized. The code will fail to compile since the constructor is trying to use local variable c before it has been initialized. The code will compile and run without any problems.
|
Q8 |
What will be written to the standard output when the following program is run?
public class Q63e3 {
public static void main(String[] args) {
System.out.println(9 ^ 2);
}
}
Select the one correct answer.
81 7 11 0 false
|
Q9 |
Which statements is true about the compilation and execution of the following program with assertions enabled?
public class Qf1e3 {
String s1;
String s2 = "hello";
String s3;
Qf1e3() {
s1 = "hello";
}
public static void main(String[] args) {
(new Qf1e3()).f();
}
{
s3 = "hello";
}
void f() {
String s4 = "hello";
String s5 = new String("hello");
assert(s1.equals(s2)); // (1)
assert(s2.equals(s3)); // (2)
assert(s3 == s4); // (3)
assert(s4 == s5); // (4)
}
}
Select the one correct answer.
The compilation will fail. The assertion on the line marked (1) will fail. The assertion on the line marked (2) will fail. The assertion on the line marked (3) will fail. The assertion on the line marked (4) will fail. The program will run without any errors.
|
Q10 |
Which declarations of the main() method are valid in order to start the execution of an application?
Select the two correct answers.
public void main(String args[]) public void static main(String args[]) public static main(String[] argv) final public static void main(String [] array) public static void main(String args[])
|
Q11 |
Under which circumstance will a thread stop?
Select the one correct answer.
The run() method that the thread is executing ends. The call to the start() method of the Thread object returns. The suspend() method is called on the Thread object. The wait() method is called on the Thread object.
|
Q12 |
When creating a class that associates a set of keys with a set of values, which of these interfaces is most applicable?
Select the one correct answer.
Collection Set SortedSet Map
|
Q13 |
What is the result of running the following code with assertions enabled?
public class Q1eec {
static void test(int i) {
int j = i/2;
int k = i >>> 1;
assert j == k : i;
}
public static void main(String[] args) {
test(0);
test(2);
test(-2);
test(1001);
test(-1001);
}
}
Select the one correct answer.
The program executes normally and produces no output. An AssertionError with 0 as message is thrown. An AssertionError with 2 as message is thrown. An AssertionError with -2 as message is thrown. An AssertionError with 1001 as message is thrown. An AssertionError with -1001 as message is thrown.
|
Q14 |
What will be written to the standard output when the following program is run?
class Base {
int i;
Base() { add(1); }
void add(int v) { i += v; }
void print() { System.out.println(i); }
}
class Extension extends Base {
Extension() { add(2); }
void add(int v) { i += v*2; }
}
public class Qd073 {
public static void main(String[] args) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
Select the one correct answer.
9 18 20 21 22
|
Q15 |
Which declarations of a native method are valid in the declaration of the following class?
public class Qf575 {
// Insert declaration of a native method here
}
Select the two correct answers.
native public void setTemperature(int kelvin); private native void setTemperature(int kelvin); protected int native getTemperature(); public abstract native void setTemperature(int kelvin); native int setTemperature(int kelvin) {}
|
Q16 |
Which collection implementation is suitable for maintaining an ordered sequence of objects, when objects are frequently inserted and removed from the middle of the sequence?
Select the one correct answer.
TreeMap HashSet Vector LinkedList ArrayList
|
Q17 |
Which statements can be inserted at the indicated position in the following code to make the program print 1 on the standard output when executed?
public class Q4a39 {
int a = 1;
int b = 1;
int c = 1;
class Inner {
int a = 2;
int get() {
int c = 3;
// Insert statement here.
return c;
}
}
Q4a39() {
Inner i = new Inner();
System.out.println(i.get());
}
public static void main(String[] args) {
new Q4a39();
}
}
Select the two correct answers.
c = b; c = this.a; c = this.b; c = Q4a39.this.a; c = c;
|
Q18 |
Which is the earliest line in the following code after which the object created in the line marked (0) will be a candidate for garbage collection, assuming no compiler optimizations are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b; // (2)
b = a; // (3)
d = a; // (4)
return c; // (5)
}
public static void main(String[] args) {
String msg = f();
System.out.println(msg); // (6)
}
}
Select the one correct answer.
The line marked (1). The line marked (2). The line marked (3). The line marked (4). The line marked (5). The line marked (6).
|
Q19 |
Which method from the String and StringBuffer classes modifies the object on which it is invoked?
Select the one correct answer.
The charAt() method of the String class. The toUpperCase() method of the String class. The replace() method of the String class. The reverse() method of the StringBuffer class. The length() method of the StringBuffer class.
|
Q20 |
Which statement, when inserted at the indicated position in the following code, will cause a runtime exception?
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String[] args) {
A x = new A();
B y = new B();
C z = new C();
// Insert statement here
}
}
Select the one correct answer.
x = y; z = x; y = (B) x; z = (C) y; y = (A) y;
|
Q21 |
Which of these are keywords in Java?
Select three correct answers.
default NULL String throws long
|
Q22 |
A method within a class is only accessible by classes that are defined within the same package as the class of the method. How can such a restriction be enforced?
Select the one correct answer.
Declare the method with the keyword public. Declare the method with the keyword protected. Declare the method with the keyword private. Declare the method with the keyword package. Do not declare the method with any accessibility modifiers.
|
Q23 |
Which code initializes the two-dimensional array tab so that tab[3][2] is a valid element?
Select the two correct answers.
int[][] tab = {
{ 0, 0, 0 },
{ 0, 0, 0 }
};
int tab[][] = new int[4][];
for (int i=0; i<tab.length; i++) tab[i] = new int[3];
int tab[][] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
int tab[3][2]; int[] tab[] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
|
Q24 |
What will be the result of attempting to run the following program?
public class Qaa75 {
public static void main(String[] args) {
String[][][] arr = {
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};
System.out.println(arr.length + arr[1][2].length);
}
}
Select the one correct answer.
The program will terminate with an ArrayIndexOutOfBoundsException. The program will terminate with a NullPointerException. 4 will be written to standard output. 6 will be written to standard output. 7 will be written to standard output.
|
Q25 |
Which expressions will evaluate to true if preceded by the following code?
String a = "hello";
String b = new String(a);
String c = a;
char[] d = { 'h', 'e', 'l', 'l', 'o' };
Select the two correct answers.
(a == "Hello") (a == b) (a == c) a.equals(b) a.equals(d)
|
Q26 |
Which statements are true about the following code?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
Select the two correct answers.
The code will fail to compile. The constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C. Class C defines three constructors. Objects of class B cannot be constructed. At most one of the constructors of each class is called as a result of constructing an object of class C.
|
Q27 |
Given two collection objects referenced by col1 and col2, which statements are true?
Select the two correct answers.
The operation col1.retainAll(col2) will not modify the col1 object. The operation col1.removeAll(col2) will not modify the col2 object. The operation col1.addAll(col2) will return a new collection object, containing elements from both col1 and col2. The operation col1.containsAll(Col2) will not modify the col1 object.
|
Q28 |
Which statements are true about the relationships between the following classes?
class Foo {
int num;
Baz comp = new Baz();
}
class Bar {
boolean flag;
}
class Baz extends Foo {
Bar thing = new Bar();
double limit;
}
Select the three correct answers.
A Bar is a Baz. A Foo has a Bar. A Baz is a Foo. A Foo is a Baz. A Baz has a Bar.
|
Q29 |
Which statements are true about the value of a field, when no explicit assignments have been made?
Select the three correct answers.
The value of a field of type int is undetermined. The value of a field of any numeric type is zero. The compiler may issue an error if the field is used in a method before it is initialized. A field of type String will denote the empty string (""). The value of all fields which are references is null.
|
Q30 |
Which statements describe guaranteed behavior of the garbage collection and finalization mechanisms?
Select the two correct answers.
An object is deleted as soon as there are no more references that denote the object. The finalize() method will eventually be called on every object. The finalize() method will never be called more than once on an object. An object will not be garbage collected as long as it is possible for a live thread to access it through a reference. The garbage collector will use a mark and sweep algorithm.
|
Q31 |
Which main() method will succeed in printing the last program argument to the standard output, and exit gracefully with no output if no program arguments are specified?
Select the one correct answer.
public static void main(String[] args) {
if (args.length != 0)
System.out.println(args[args.length-1]);
}
public static void main(String[] args) {
try { System.out.println(args[args.length]); }
catch (ArrayIndexOutOfBoundsException e) {}
}
public static void main(String[] args) {
int ix = args.length;
String last = args[ix];
if (ix != 0) System.out.println(last);
}
public static void main(String[] args) {
int ix = args.length-1;
if (ix > 0) System.out.println(args[ix]);
}
public static void main(String[] args) {
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
|
Q32 |
Which statements are true about the collection interfaces?
Select the three correct answers.
Set extends Collection. All methods defined in Set are also defined in Collection. List extends Collection. All methods defined in List are also defined in Collection. Map extends Collection.
|
Q33 |
Which is the legal range of values for a short?
Select the one correct answer.
?27 to 27?1 ?28 to 28 ?215 to 215?1 ?216 to 216?1 0 to 216?1
|
Q34 |
What is the name of the method that threads can use to pause their execution until signalled to continue by another thread?
Fill in the name of the method (do not include a parameter list). |
Q35 |
Given the following class definitions, which expression identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C, and D?
class A {}
class B extends A {}
class C extends B {}
class D extends A {}
Select the one correct answer.
obj instanceof B obj instanceof A && !(obj instanceof C) obj instanceof B && !(obj instanceof C) !(obj instanceof C || obj instanceof D) !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)
|
Q36 |
What will be written to the standard output when the following program is executed?
public class Q8499 {
public static void main(String[] args) {
double d = -2.9;
int i = (int) d;
i *= (int) Math.ceil(d);
i *= (int) Math.abs(d);
System.out.println(i);
}
}
Select the one correct answer.
-12 18 8 12 27
|
Q37 |
What will be written to the standard output when the following program is executed?
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String[] args) {
Qcb90 obj = new Qcb90();
obj.f();
}
}
Select the one correct answer.
0 0 0 0 0 1 0 1 0 1 0 0 1 0 1
|
Q38 |
Given the following class, which are correct implementations of the hashCode() method?
class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}
Select the three correct answers.
return 0; return a; return a + b; return a - b; return a ^ b; return (a << 16) | b;
|
Q39 |
Which statements are true regarding the execution of the following code?
public class Q3a0a {
public static void main(String[] args) {
int j = 5;
for (int i = 0; i<j; i++) {
assert i < j-- : i > 0;
System.out.println(i*j);
}
}
}
Select the two correct answers.
An AssertionError will be thrown if assertions are enabled at runtime. The last number printed is 4 if assertions are disabled at runtime. The last number printed is 20 if assertions are disabled at runtime. The last number printed is 4 if assertions are enabled at runtime. The last number printed is 20 if assertions are enabled at runtime.
|
Q40 |
Which of the following method names are overloaded?
Select the three correct answers.
The method name yield in java.lang.Thread The method name sleep in java.lang.Thread The method name wait in java.lang.Object The method name notify in java.lang.Object
|
Q41 |
Which are valid identifiers?
Select the three correct answers.
_class $value$ zer@ ångström 2much
|
Q42 |
What will be the result of attempting to compile and run the following program?
public class Q28fd {
public static void main(String[] args) {
int counter = 0;
l1:
for (int i=0; i<10; i++) {
l2:
int j = 0;
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
}
}
Select the one correct answer.
The program will fail to compile. The program will not terminate normally. The program will write 10 to the standard output. The program will write 0 to the standard output. The program will write 9 to the standard output.
|
Q43 |
Given the following interface definition, which definition is valid?
interface I {
void setValue(int val);
int getValue();
}
Select the one correct answer.
class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
interface B extends I {
void increment();
}
abstract class C implements I {
int getValue() { return 0; }
abstract void increment();
}
interface D implements I {
void increment();
}
class E implements I {
int value;
public void setValue(int val) { value = val; }
}
|
Q44 |
Which statements are true about the methods notify() and notifyAll()?
Select the two correct answers.
An instance of the class Thread has a method named notify that can be invoked. A call to the method notify() will wake the thread that currently owns the lock of the object. The method notify() is synchronized. The method notifyAll() is defined in class Thread. When there is more than one thread waiting to obtain the lock of an object, there is no way to be sure which thread will be notified by the notify() method.
|
Q45 |
Which statements are true about the correlation between the inner and outer instances of member classes?
Select the two correct answers.
Fields of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers. Fields of the outer instance can never be accessed using only the variable name within the inner instance. More than one inner instance can be associated with the same outer instance. All variables from the outer instance that should be accessible in the inner instance must be declared final. A class that is declared final cannot have any member classes.
|
Q46 |
What will be the result of attempting to compile and run the following code?
public class Q6b0c {
public static void main(String[] args) {
int i = 4;
float f = 4.3;
double d = 1.8;
int c = 0;
if (i == f) c++;
if (((int) (f + d)) == ((int) f + (int) d)) c += 2;
System.out.println(c);
}
}
Select the one correct answer.
The code will fail to compile. The value 0 will be written to the standard output. The value 1 will be written to the standard output. The value 2 will be written to the standard output. The value 3 will be written to the standard output.
|
Q47 |
Which operators will always evaluate all the operands?
Select the two correct answers.
|| + && ? : %
|
Q48 |
Which statement concerning the switch construct is true?
Select the one correct answer.
All switch statements must have a default label. There must be exactly one label for each code segment in a switch statement. The keyword continue can never occur within the body of a switch statement. No case label may follow a default label within a single switch statement. A character literal can be used as a value for a case label.
|
Q49 |
Which modifiers and return types would be valid in the declaration of a main() method that starts the execution of a Java standalone application?
Select the two correct answers.
private final static int abstract String
|
Q50 |
Which of the following expressions are valid?
Select the three correct answers.
System.out.hashCode() "".hashCode() 42.hashCode() ("4"+2).equals(42) (new java.util.Vector()).hashCode()
|
Q51 |
Which statement regarding the following method definition is true?
boolean e() {
try {
assert false;
} catch (AssertionError ae) {
return true;
}
return false; // (1)
}
Select the one correct answer.
The code will fail to compile since catching an AssertionError is illegal. The code will fail to compile since the return statement at (1) is unreachable. The method will return true under all circumstances. The method will return false under all circumstances. The method will return true if and only if assertions are enabled at runtime.
|
Q52 |
If str denotes a String object with the string "73", which of these expressions will convert the string to the int value 73?
Select the two correct answers.
Integer.intValue(str) ((int) str) (new Integer(str)).intValue() Integer.parseInt(str) Integer.getInt(str)
|
Q53 |
Insert a line of code at the indicated location that will call the print() method in the Base class.
class Base {
public void print() {
System.out.println("base");
}
}
class Extension extends Base {
public void print() {
System.out.println("extension");
// Insert a line of code here.
}
}
public class Q294d {
public static void main(String[] args) {
Extension ext = new Extension();
ext.print();
}
}
Fill in a single line of code. |
Q54 |
Given the following code, which statements are true?
public class Vertical {
private int alt;
public synchronized void up() {
++alt;
}
public void down() {
--alt;
}
public synchronized void jump() {
int a = alt;
up();
down();
assert(a == alt);
}
}
Select the two correct answers.
The code will fail to compile. Separate threads can execute the up() method concurrently. Separate threads can execute the down() method concurrently. Separate threads can execute both the up() and down() method concurrently. The assertion in the jump() method will not fail under any circumstances.
|
Q55 |
What will be written to the standard output when the following program is run?
public class Q03e4 {
public static void main(String[] args) {
String space = " ";
String composite = space + "hello" + space + space;
composite.concat("world");
String trimmed = composite.trim();
System.out.println(trimmed.length());
}
}
Select the one correct answer.
5 6 7 12 13
|
Q56 |
Given the following code, which statements are true about the objects referenced through the fields i, j, and k, given that any thread may call the methods a(), b(), and c() at any time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}
Select the two correct answers.
i.v is guaranteed always to be 0 or 1. j.v is guaranteed always to be 0 or 1. k.v is guaranteed always to be 0 or 1 j.v will always be greater than or equal to k.v at any give time. k.v will always be greater than or equal to j.v at any give time.
|
Q57 |
Which statements are true about casting and conversion?
Select the three correct answers.
Conversion from int to long does not need a cast. Conversion from byte to short does not need a cast. Conversion from float to long does not need a cast. Conversion from short to char does not need a cast. Conversion from boolean to int using a cast is not possible.
|
Q58 |
Which method declarations, when inserted at the indicated position, will not cause the program to fail during compilation?
public class Qdd1f {
public long sum(long a, long b) { return a + b; }
// Insert new method declarations here.
}
Select the two correct answers.
public int sum(int a, int b) { return a + b; } public int sum(long a, long b) { return 0; } abstract int sum(); private long sum(long a, long b) { return a + b; } public long sum(long a, int b) { return a + b; }
|
Q59 |
The 8859-1 character code for the uppercase letter A is the decimal value 65. Which code fragments declare and initialize a variable of type char with this value?
Select the two correct answers.
char ch = 65; char ch = '\65'; char ch = '\0041'; char ch = 'A'; char ch = "A";
|
Q60 |
What will be the result of executing the following program code with assertions enabled?
import java.util.*;
public class Q4d3f {
public static void main(String[] args) {
LinkedList lla = new LinkedList();
LinkedList llb = new LinkedList();
assert lla.size() == llb.size() : "empty";
lla.add("Hello");
assert lla.size() == 1 : "size";
llb.add("Hello");
assert llb.contains("Hello") : "contains";
assert lla.get(0).equals(llb.get(0)) : "element";
assert lla.equals(llb) : "collection";
}
}
Select the one correct answer.
Execution proceeds normally and produces no output. An AssertionError with the message "size" is thrown. An AssertionError with the message "empty" is thrown. An AssertionError with the message "element" is thrown An IndexOutOfBoundsException is thrown. An AssertionError with the message "container" is thrown.
|