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.
|
Q2 |
Which statements are true about the effect of the >> and >>> operators?
Select the three correct answers.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
Q10 |
Which declarations of the main() method are valid in order to start the execution of an application?
Select the two correct answers.
|
Q11 |
Under which circumstance will a thread stop?
Select the one correct answer.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
Q19 |
Which method from the String and StringBuffer classes modifies the object on which it is invoked?
Select the one correct answer.
|
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.
|
Q21 |
Which of these are keywords in Java?
Select three correct answers.
|
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. |
Q23 |
Which code initializes the two-dimensional array tab so that tab[3][2] is a valid element?
Select the two correct answers.
|
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.
|
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.
|
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.
|
Q27 |
Given two collection objects referenced by col1 and col2, which statements are true?
Select the two correct answers.
|
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.
|
Q29 |
Which statements are true about the value of a field, when no explicit assignments have been made?
Select the three correct answers. |
Q30 |
Which statements describe guaranteed behavior of the garbage collection and finalization mechanisms?
Select the two correct answers.
|
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.
|
Q32 |
Which statements are true about the collection interfaces?
Select the three correct answers.
|
Q33 |
Which is the legal range of values for a short?
Select the one correct answer.
|
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.
|
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.
|
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.
|
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.
|
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.
|
Q40 |
Which of the following method names are overloaded?
Select the three correct answers.
|
Q41 |
Which are valid identifiers?
Select the three correct answers.
|
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.
|
Q43 |
Given the following interface definition, which definition is valid?
interface I { void setValue(int val); int getValue(); } Select the one correct answer.
|
Q44 |
Which statements are true about the methods notify() and notifyAll()?
Select the two correct answers.
|
Q45 |
Which statements are true about the correlation between the inner and outer instances of member classes?
Select the two correct answers.
|
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.
|
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.
|
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.
|
Q50 |
Which of the following expressions are valid?
Select the three correct answers.
|
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.
|
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.
|
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.
|
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.
|
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.
|
Q57 |
Which statements are true about casting and conversion?
Select the three correct answers.
|
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.
|
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.
|
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.
|
Q61 |
Which of these are keywords in Java?
Select the two correct answers.
|