Tuesday 4 October 2011

scjp questions set1

QUESTION NO: 26
Click the Exhibit button.
What is the output of the program shown in the exhibit?

A. 300-300-100-100-100
B. 300-300-300-100-100
C. 300-300-300-300-100
D. 300-100-100-100-100
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 27
Given:
12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)
A. The value of b is 2.00.
B. The value of a is 3.141.
C. The value of a is 3.14.
D. The value of b is 2.0000.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.
-------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 28
Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A. public class MinMax<? extends Object> {
B. public class MinMax<N extends Integer> {
C. public class MinMax<N extends Object> {
D. public class MinMax<N extends Number> {
E. public class MinMax<?> {
F. public class MinMax<? extends Number> {
---------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 29
Given:
11. class A {
12. public void process() { System.out.print("A,"); }
13. class B extends A {
20
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19. public static void main(String[] args) {
20. try { new B().process(); }
21. catch (IOException e) { System.out.println("Exception"); }}
What is the result?
A. Compilation fails because of an error in line 14.
B. Exception
C. A,B,Exception
D. Compilation fails because of an error in line 20.
E. A NullPointerException is thrown at runtime.
------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 30 DRAG DROP
Click the Task button.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 31
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. An instance of Forest and an instance of Tree are both serialized.
B. An instance of Forest is serialized.
C. Compilation fails.
D. An exception is thrown at runtime.
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 32
Click the Exhibit button.
What is the result?
A. first second third snootchy 420
B. snootchy 420 first second third
C. third first second snootchy 420
D. first second first third snootchy 420
E. snootchy 420 third second first
F. third second first snootchy 420
---------------------------------------------------------------------------------------------------------------------------  
QUESTION NO: 33
Given:
1. public class TestOne implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread(new TestOne());
4. t.start();
5. System.out.print("Started");
6. t.join();
7. System.out.print("Complete");
8. }
9. public void run() {
10. for (int i = 0; i < 4; i++) {
11. System.out.print(i);
12. }
13. }
14. }
What can be a result?
A. The code executes and prints "StartedComplete".
B. Compilation fails.
C. The code executes and prints "Started0123Complete".
D. The code executes and prints "StartedComplete0123".
E. An exception is thrown at runtime.
----------------------------------------------------------------------------------------------------------------- 
QUESTION NO: 34
Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString ws1 = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }
What is the result?
A. Compilation fails.
B. 1
C. 4
D. 2
E. 0
F. An exception is thrown at runtime.
G. 3
QUESTION NO: 35
Given:
31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }
Under which three circumstances will the code on line 37 be executed? (Choose three.)
A. The code on line 33 executes successfully.
B. The code on line 33 throws an exception.
C. The instance gets garbage collected.
D. The code on line 31 throws an exception.
E. The code on line 35 throws an exception.
-------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 36
Given:
11. public static void main(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerException npe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. NullPointerException
C. Exception
D. Compilation fails.
-------------------------------------------------------------------------------------------------------------------------- 
QUESTION NO: 37 DRAG DROP
Click the Task button.
--------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 38
DRAG DROP
Click the Task button.
---------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 39
Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
Which three changes must be made to the method sum to use generics? (Choose three.)
A. replace line 13 with "for (int i : intList) {"
B. replace the method declaration with "sum(List<int> intList)"
C. replace the method declaration with "sum(List<Integer> intList)"
D. replace line 14 with "int i = iter.next();"
E. replace line 13 with "for (Iterator iter : intList) {"
F. remove line 14
----------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 40
Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally, but nothing prints.
D. The code executes normally and prints "bar".
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 41
Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile if public Tree() { Plant(); } is added to the Tree class.
B. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
C. The code will compile without changes.
D. The code will compile if public Plant() { Tree(); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
-------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 42
Given:
1. public interface A {
2. String DEFAULT_GREETING = "Hello World";
3. public void method1();
4. }
A programmer wants to create an interface called B that has A as its parent. Which interface
declaration is correct?
A. public interface B extends A {}
B. public interface B inheritsFrom A {}
C. public interface B implements A {}
D. public interface B instanceOf A {}
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 43
 DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------ 
QUESTION NO: 44
Given:
11. rbo = new ReallyBigObject();
12. // more code here
13. rbo = null;
14. /* insert code here */
Which statement should be placed at line 14 to suggest that the virtual machine expend effort
toward recycling the memory used by the object rbo?
A. System.freeMemory();
B. Runtime.gc();
C. Runtime.getRuntime().freeMemory();
D. Runtime.getRuntime().growHeap();
E. System.gc();
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 45
Given:
11. class A {
12. public void process() { System.out.print("A,"); }
13. class B extends A {
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19. public static void main(String[] args) {
20. try { new B().process(); }
21. catch (IOException e) { System.out.println("Exception"); }
What is the result?
A. A NullPointerException is thrown at runtime.
B. A,B,Exception
C. Compilation fails because of an error in line 14.
D. Compilation fails because of an error in line 20.
E. Exception
----------------------------------------------------------------------------------------------------------------------------------------- 
QUESTION NO: 46
Which two statements are true about has-a and is-a relationships? (Choose two.)
A. Interfaces must be used when creating a has-a relationship.
B. Instance variables can be used when creating a has-a relationship.
C. Inheritance represents an is-a relationship.
D. Inheritance represents a has-a relationship.
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 47
DRAG DROP
Click the Task button.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 48
DRAG DROP
Click the Task button.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 49
Click the Exhibit button.
Which two statements are true if this class is compiled and run? (Choose two.)
A. The code may run with no output, exiting normally.
B. The code may run with output "A A A B C A B C C ", then exit.
C. The code may run with output "A B A B C C ", then exit.
D. An exception may be thrown at runtime.
E. The code may run with no output, without exiting.
F. The code may run with output "A B C A B C A B C ", then exit.
G. The code may run with output "A B C A A B C A B C ", then exit.
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 50
Click the Exhibit button.
Given:
ClassA a = new ClassA();
a.methodA();
What is the result?
A. Compilation fails.
B. ClassC is displayed.
C. The code runs with no output.
D. An exception is thrown at runtime.
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 51
Given:
1. public class TestString1 {
2. public static void main(String[] args) {
3. String str = "420";
4. str += 42;
5. System.out.print(str);
6. }
7. }
What is the output?
A. Compilation fails.
B. 42042
C. An exception is thrown at runtime.
D. 462
E. 42
F. 420
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 52
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2. public class Commander {
3. public static void main(String[] args) {
4. // more code here
5. }
6. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that
the classpath contains "." (current directory).
Which command line correctly runs Commander?
A. java Commander
B. java com/sun/sjcp/Commander
C. java com.sun.sjcp.Commander
D. java -cp com/sun/sjcp Commander
E. java -cp com.sun.sjcp Commander
-------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 53
Given a class Repetition:
1. package utils;
2.
3. public class Repetition {
4. public static String twice(String s) { return s + s; }
5. }
and given another class Demo:
1. // insert code here
2.
3. public class Demo {
4. public static void main(String[] args) {
5. System.out.println(twice("pizza"));
6. }
7. }
Which code should be inserted at line 1 of Demo.java to compile and run Demo to print
"pizzapizza"?
A. import static utils.Repetition.twice;
B. static import utils.*;
C. static import utils.Repetition.*;
D. import utils.Repetition.*;
E. import utils.*;
F. import utils.Repetition.twice();
G. static import utils.Repetition.twice;
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 54
DRAG DROP
Click the Task button.
---------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 55
Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Which three are valid on line 12? (Choose three.)
A. static
B. native
C. abstract
D. final
E. private
F. protected
G. public
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 56
Which three will compile and run without exception? (Choose three.)
A. void go() {
synchronized() { /* code here */ }
B. private synchronized(this) void go() { /* code here */ }
C. void go() {
Object o = new Object();
synchronized(o) { /* code here */ }
D. private synchronized Object o;
E. void go() {
synchronized(Object.class) { /* code here */ }
F. public synchronized void go() { /* code here */ }
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 57
Given:
1. import java.util.*;
2. public class Old {
3. public static Object get0(List list) {
4. return list.get(0);
5. }
6. }
Which three will compile successfully? (Choose three.)
A. String s = Old.get0(new LinkedList<String>());
B. String s = (String)Old.get0(new LinkedList<String>());
C. Object o = Old.get0(new LinkedList<?>());
D. Object o = Old.get0(new LinkedList<Object>());
E. Object o = Old.get0(new LinkedList());
-----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 58
Given:
10. class One {
11. public One() { System.out.print(1); }
12. }
13. class Two extends One {
14. public Two() { System.out.print(2); }
15. }
16. class Three extends Two {
17. public Three() { System.out.print(3); }
18. }
19. public class Numbers{
20. public static void main( String[] argv ) { new Three(); }
21. }
What is the result when this code is executed?
A. 321
B. 3
C. 1
D. 123
E. The code runs with no output.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 59
Given:
10. package com.sun.scjp;
11. public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
A. import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
B. package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
D. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
--------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 60
Given:
enum Example { ONE, TWO, THREE }
Which statement is true?
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
B. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must
use a java.util.EnumMap.
C. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is
guaranteed to be less than one.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted
because enumerated types do NOT implement java.lang.Comparable.
-----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 61
Given:
11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13. System.out.print("pi is bigger than 3. ");
14. }
15. else {
16. System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }
What is the result?
A. pi is not bigger than 3. Have a nice day.
B. pi is bigger than 3. Have a nice day.
C. pi is bigger than 3.
D. Compilation fails.
E. An exception occurs at runtime.
---------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 62
Given:
11. public static void append(List list) { list.add("0042"); }
12. public static void main(String[] args) {
13. List<Integer> intList = new ArrayList<Integer>();
14. append(intList);
15. System.out.println(intList.get(0));
16. }
What is the result?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 14.
C. Compilation fails because of an error in line 13.
D. 0042
E. 42
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 63
Given:
10. interface Jumper { public void jump(); }
...
20. class Animal {}
...
30. class Dog extends Animal {
31. Tail tail;
32. }
...
40. class Beagle extends Dog implements Jumper{
41. public void jump() {}
42. }
...
50. class Cat implements Jumper{
51. public void jump() {}
52. }
Which three are true? (Choose three.)
A. Dog is-a Jumper
B. Beagle has-a Jumper
C. Cat has-a Animal
D. Dog is-a Animal
E. Cat is-a Jumper
F. Beagle has-a Tail
G. Cat is-a Animal
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 64
Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x = 1; x < yahoo.length; x++) {
16. System.out.print(yahoo[x] + " ");
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A. a b c
B. Compilation fails.
C. a b
D. An exception is thrown at runtime.
E. b c
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 65
DRAG DROP
Click the Task button.
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 66
A programmer needs to create a logging method that can accept an arbitrary number of
arguments. For example, it may be called in these ways:
logIt("log message1");
logIt("log message2","log message3");
logIt("log message4","log message5","log message6");
Which declaration satisfies this requirement?
A. public void logIt(String msg1, String msg2, String msg3)
B. public void logIt(String [] msgs)
C. public void logIt(String * msgs)
D. public void logIt(String... msgs)
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 67
DRAG DROP
Click the Task button.



QUESTION NO: 68
Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue<String> pq = new PriorityQueue<String>();
5. pq.add("carrot");
6. pq.add("apple");
7. pq.add("banana");
8. System.out.println(pq.poll() + ":" + pq.peek());
9. }
10. }
What is the result?
A. apple:apple
B. banana:apple
C. carrot:apple
D. apple:banana
E. carrot:banana
F. carrot:carrot
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 69
Given:
1. public class Threads3 implements Runnable {
2. public void run() {
3. System.out.print("running");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A. Compilation fails.
B. The code executes and prints "runningrunning".
C. The code executes and prints "runningrunningrunning".
D. The code executes and prints "running".
E. An exception is thrown at runtime.
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 70
Given:
int[] myArray = new int[] {1, 2, 3, 4, 5};
What allows you to create a list from this array?
A. List myList = Arrays.asList(myArray);
B. List myList = Collections.fromArray(myArray);
C. List myList = new ArrayList(myArray);
D. List myList = myArray.asList();
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 71
Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. Declaring the doThings() method as static would make the class thread-safe.
C. Synchronizing the run() method would make the class thread-safe.
D. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would
make the class thread-safe.
E. An exception is thrown at runtime.
F. The data in variable "x" are protected from concurrent access problems.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 72
DRAG DROP
Click the Task button.


--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 73
Given:
1. public class TestFive {
2. private int x;
3. public void foo() {
4. int current = x;
5. x = current + 1;
6. }
7. public void go() {
8. for(int i = 0; i < 5; i++) {
9. new Thread() {
10. public void run() {
11. foo();
12. System.out.print(x + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A. change the variable declaration on line 2 to private volatile int x;
B. wrap the code inside the foo() method with a synchronized( this ) block
C. change line 7 to public synchronized void go() {
D. wrap the for loop code inside the go() method with a synchronized block synchronized(this) { //
for loop code here }
E. move the line 12 print statement into the foo() method
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 74
Given:
11. public static void main(String[] args) {
12. String str = "null";
13. if (str == null) {
14. System.out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }
What is the result?
A. An exception is thrown at runtime.
B. null
C. Compilation fails.
D. zero
E. some
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 75
Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a
candidate for garbage collection?
A. Line 19
B. Line 16
C. Line 17
D. The object is NOT a candidate for garbage collection.
E. Line 18
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 76
Given:
31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }
Under which three circumstances will the code on line 37 be executed? (Choose three.)
A. The code on line 31 throws an exception.
B. The code on line 35 throws an exception.
C. The code on line 33 executes successfully.
D. The code on line 33 throws an exception.
E. The instance gets garbage collected.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 77
Given:
11. class Cup { }
12. class PoisonCup extends Cup { }
...
21. public void takeCup(Cup c) {
22. if (c instanceof PoisonCup) {
23. System.out.println("Inconceivable!");
24. } else if (c instanceof Cup) {
25. System.out.println("Dizzying intellect!");
26. } else {
27. System.exit(0);
28. }
29. }
And the execution of the statements:
Cup cup = new PoisonCup();
takeCup(cup);
What is the output?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 22.
C. Dizzying intellect!
D. Inconceivable!
E. The code runs with no output.
QUESTION NO: 78
Given:
10. public class Bar {
11. static void foo( int... x ) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will allow the class to compile?
(Choose two.)
A. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);
B. for( int z : x ) System.out.println(z);
C. foreach( x ) System.out.println(z);
D. while( x.hasNext() ) System.out.println( x.next() );
-------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 79
DRAG DROP
Click the Task button.
---------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 80
Given:
11. public static Collection get() {
12. Collection sorted = new LinkedList();
13. sorted.add("B"); sorted.add("C"); sorted.add("A");
14. return sorted;
15. }
16. public static void main(String[] args) {
17. for (Object obj: get()) {
18. System.out.print(obj + ", ");
19. }
20. }
What is the result?
A. Compilation fails.
B. B, C, A,
C. The code runs with no output.
D. A, B, C,
E. An exception is thrown at runtime.
--------------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 81
Given:
11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println("Long ");}
14. public static void go(Short n) {System.out.println("Short ");}
15. public static void go(int n) {System.out.println("int ");}
16. public static void main(String [] args) {
17. short y = 6;
18. long z = 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. An exception is thrown at runtime.
B. Short Long
C. Compilation fails.
D. int Long
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 82
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------
 QUESTION NO: 83
Given:
1. public class Threads2 implements Runnable {
2.
3. public void run() {
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. run.
java.lang.RuntimeException: Problem
End of method.
E. End of method.
run.
java.lang.RuntimeException: Problem
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 84
Given:
13. public class Pass {
14. public static void main(String [] args) {
15. int x = 5;
16. Pass p = new Pass();
17. p.doStuff(x);
18. System.out.print(" main x = " + x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(" doStuff x = " + x++);
23. }
24. }
What is the result?
A. An exception is thrown at runtime.
B. doStuff x = 6 main x = 6
C. doStuff x = 5 main x = 5
D. Compilation fails.
E. doStuff x = 6 main x = 5
F. doStuff x = 5 main x = 6
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 85
A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user wants to execute the
main method of Poker on a UNIX system using the command:
java games.cards.Poker
What allows the user to do this?
A. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include
/stuff/java/*.jar
B. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java
C. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include
/stuff/java/Poker.jar
D. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar
E. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar
F. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 86
Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java
serialization and given:
13. import java.io.*;
14. class Food implements Serializable {int good = 3;}
15. class Fruit extends Food {int juice = 5;}
16. public class Banana extends Fruit {
17. int yellow = 4;
18. public static void main(String [] args) {
19. Banana b = new Banana(); Banana b2 = new Banana();
20. b.serializeBanana(b); // assume correct serialization
21. b2 = b.deserializeBanana(); // assume correct
22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good);
24. }
25. // more Banana methods go here 50. }
What is the result?
A. restore 403
B. An exception is thrown at runtime.
C. restore 453
D. Compilation fails.
E. restore 400
-------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 87
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes);
B. import util.BitUtils.*; process(bytes);
C. BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. util.BitUtils.process(bytes);
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 88
Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?
A. After line 15, the value of age is 3.
B. Compilation fails.
C. An exception is thrown at runtime.
D. After line 15, the value of age is 5.
--------------------------------------------------------------------------------------------------------------------------------------------------
 QUESTION NO: 89
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
protected int a;
B. Change line 13 to:
public Sub() { super(a); }
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 2 to:
public int a;
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 90
Given:
10. class One {
11. public One() { System.out.print(1); }
12. }
13. class Two extends One {
14. public Two() { System.out.print(2); }
15. }
16. class Three extends Two {
17. public Three() { System.out.print(3); }
18. }
19. public class Numbers{
20. public static void main( String[] argv ) { new Three(); }
21. }
What is the result when this code is executed?
A. 1
B. 321
C. 3
D. The code runs with no output.
E. 123
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 91
Given:
11. public static Collection get() {
12. Collection sorted = new LinkedList();
13. sorted.add("B"); sorted.add("C"); sorted.add("A");
14. return sorted;
15. }
16. public static void main(String[] args) {
17. for (Object obj: get()) {
18. System.out.print(obj + ", ");
19. }
20. }
What is the result?
A. Compilation fails.
B. A, B, C,
C. The code runs with no output.
D. An exception is thrown at runtime.
E. B, C, A,
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 92
Click the Exhibit button.
Given this code from Class B:
25. A a1 = new A();
26. A a2 = new A();
27. A a3 = new A();
28. System.out.println(A.getInstanceCount());
What is the result?
A. Compilation fails because of an error on line 28.
B. Line 28 prints the value 3 to System.out.
C. Compilation of class A fails.
D. Line 28 prints the value 1 to System.out.
E. A runtime error occurs when line 25 executes.
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 93
Given:
1. package test;
2.
3. class Target {
4. public String name = "hello";
5. }
What can directly access and change the value of the variable name?
A. only the Target class
B. any class in the test package
C. any class that extends Target
D. any class
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 94
Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
B. The code will compile if public Plant() { Tree(); } is added to the Plant class.
C. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
D. The code will compile without changes.
E. The code will compile if public Tree() { Plant(); } is added to the Tree class.
QUESTION NO: 95
 DRAG DROP
Click the Task button.
-----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 96
Given:
1. class SuperClass {
2. public A getA() {
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass {
7. public B getA(){
8. return new B();
9. }
10. }
Which statement is true?
A. Compilation will always fail because of an error in line 8.
B. Compilation will succeed if A extends B.
C. Compilation will succeed if B extends A.
D. Compilation will always fail because of an error in line 7.
-------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 97
DRAG DROP
Click the Task button.
QUESTION NO: 98
Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A. Line.Point p = Line.getPoint();
B. Point p = (new Line()).getPoint();
C. Point p = Line.getPoint();
D. Line.Point p = (new Line()).getPoint();
--------------------------------------------------------------------------------------------------------------------------------------------- QUESTION NO: 99
Given:
1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print("foo");
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }
What is the result?
A. An exception is thrown at runtime.
B. The code executes normally, but nothing is printed.
C. Compilation fails.
D. The code executes normally and prints "foo".
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 100
Which three statements are true? (Choose three.)
A. A public static method in class X can be called by a subclass of X without explicitly referencing
the class X.
B. A non-static public final method in class X can be overridden in any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A protected method in class X can be overridden by any subclass of X.
E. A protected method in class X can be overridden by a subclass of A only if the subclass is in
the same package as X.
F. A method with the same signature as a private final method in class X can be implemented in a
subclass of X.
G. A final method in class X can be abstract if and only if X is abstract.
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 101
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. An instance of Forest is serialized.
B. Compilation fails.
C. An exception is thrown at runtime.
D. An instance of Forest and an instance of Tree are both serialized.
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 102
Which two statements are true about the hashCode method? (Choose two.)
A. The hashCode method for a given class can be used to test for object equality and object
inequality for that class.
B. The hashCode method is used by the java.util.HashSet collection class to group the elements
within that set into hash buckets for swift retrieval.
C. The only important characteristic of the values returned by a hashCode method is that the
distribution of values must follow a Gaussian distribution.
D. The hashCode method is used by the java.util.SortedSet collection class to order the elements
within that set.
E. The hashCode method for a given class can be used to test for object inequality, but NOT
object equality, for that class.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 103
Given:
12. public class Wow {
13. public static void go(short n) {System.out.println("short");}
14. public static void go(Short n) {System.out.println("SHORT");}
15. public static void go(Long n) {System.out.println(" LONG");}
16. public static void main(String [] args) {
17. Short y = 6;
18. int z = 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. short LONG
B. Compilation fails.
C. SHORT LONG
D. An exception is thrown at runtime.
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 104
Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }
Which statement is true?
A. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
B. The time to find the value from HashMap with a Person key depends on the size of the map.
C. The time to determine whether a Person object is contained in a HashSet is constant and does
NOT depend on the size of the map.
D. Inserting a second Person object into a HashSet will cause the first Person object to be
removed as a duplicate.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 105
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. An instance of Forest and an instance of Tree are both serialized.
B. An instance of Forest is serialized.
C. An exception is thrown at runtime.
D. Compilation fails.
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 106
DRAG DROP
Click the Task button.
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 107
Given:
1. class Pizza {
2. java.util.ArrayList toppings;
3. public final void addTopping(String topping) {
4. toppings.add(topping);
5. }
6. }
7. public class PepperoniPizza extends Pizza {
8. public void addTopping(String topping) {
9. System.out.println("Cannot add Toppings");
10. }
11. public static void main(String[] args) {
12. Pizza pizza = new PepperoniPizza();
13. pizza.addTopping("Mushrooms");
14. }
15. }
What is the result?
A. The code runs with no output.
B. Cannot add Toppings
C. A NullPointerException is thrown in Line 4.
D. Compilation fails.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 108
Given:
11. public static void main(String[] args) {
12. for (int i = 0; i <= 10; i++) {
13. if (i > 6) break;
14. }
15. System.out.println(i);
16. }
What is the result?
A. Compilation fails.
B. 10
C. 7
D. 6
E. 11
F. An exception is thrown at runtime.
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 109
Given:
10. interface Data { public void load(); }
11. abstract class Info { public abstract void load(); }
Which class correctly uses the Data interface and Info class?
A. public class Employee extends Info implements Data{
public void Data.load() { /*do something*/ }
public void Info.load() { /*do something*/ }
}
B. public class Employee extends Info implements Data
public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}
C. public class Employee extends Info implements Data {
public void load() { /*do something*/ }
}
D. public class Employee implements Info extends Data {
public void Data.load(){ /*do something*/ }
public void load(){ /*do something*/ }
}
E. public class Employee implements Info extends Data {
public void load() { /*do something*/ }
}
F. public class Employee implements Info extends Data {
public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 110
Given:
11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }
What is the result?
A. A NumberFormatException is thrown by the parse method at runtime.
B. A ParseException is thrown by the parse method at runtime.
C. Compilation fails.
D. 0.0
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 111
Which two statements are true about has-a and is-a relationships? (Choose two.)
A. Interfaces must be used when creating a has-a relationship.
B. Instance variables can be used when creating a has-a relationship.
C. Inheritance represents a has-a relationship.
D. Inheritance represents an is-a relationship.
--------------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 112
Click the Exhibit button.
What is the outcome of the code?
A. Scrumdiddlyumptious
B. Compilation fails.
C. Gobstopper
Fizzylifting
D. Scrumdiddlyumptious
Fizzylifting
E. Gobstopper
Scrumdiddlyumptious
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 113
Given:
55. int [] x = {1, 2, 3, 4, 5};
56. int y[] = x;
57. System.out.println(y[2]);
Which statement is true?
A. Compilation will fail because of an error in line 55.
B. Line 57 will print the value 2.
C. Compilation will fail because of an error in line 56.
D. Line 57 will print the value 3.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 114
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void run() { doStuff(); }
};
D. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
E. new Thread() {
public void start() { doStuff(); }
}.run();
F. new Thread() {
public void run() { doStuff(); }
}.start();
--------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 115
Click the Exhibit button.
What is the result?
A. third first second snootchy 420
B. first second first third snootchy 420
C. first second third snootchy 420
D. snootchy 420 third second first
E. snootchy 420 first second third
F. third second first snootchy 420
-------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 116
A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure
where the old one is installed. Bob is currently able to run a Java chess program starting from his
home directory /home/bob using the command:
java -classpath /test:/home/bob/downloads/*.jar games.Chess
Bob's CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar
What is a possible location for the Chess.class file?
A. /usr/lib/games/Chess.class
B. /home/bob/Chess.class
C. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
D. /test/Chess.class
E. /home/bob/games/Chess.class
F. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)
G. /test/games/Chess.class
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 117
Which four statements are true? (Choose four.)
A. Is-a relationships can be implemented using the implements keyword.
B. The relationship between Movie and Actress is an example of an is-a relationship.
C. Is-a relationships can be implemented using the extends keyword.
D. An array or a collection can be used to implement a one-to-many has-a relationship.
E. Has-a relationships should be implemented using inheritance.
F. Has-a relationships should never be encapsulated.
G. Has-a relationships can be implemented using instance variables.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 118
DRAG DROP
Click the Task button.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 119
Given:
11. class Animal { public String noise() { return "peep"; } }
12. class Dog extends Animal {
13. public String noise() { return "bark"; }
14. }
15. class Cat extends Animal {
16. public String noise() { return "meow"; }
17. }
...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());
What is the result?
A. An exception is thrown at runtime.
B. peep
C. bark
D. meow
E. Compilation fails.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 120
Click the Exhibit button.
Given:
34. Test t = new Test();
35. t.method(5);
What is the output from line 5 of the Test class?
A. 24
B. 5
C. 10
D. 17
E. 12
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 121
Given:
8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. }
12. }
Which two will produce an AssertionError? (Choose two.)
A. java -ea test
B. java -ea test file1
C. java -ea:test test file1
D. java test
E. java -ea test file1 file2
F. java test file1
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 122
DRAG DROP
Click the Task button.
-------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 123
A programmer must create a generic class MinMax and the type parameter of MinMax must
implement Comparable. Which implementation of MinMax will compile?
A. class MinMax<E implements Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() {}
public <E> void put(E value) { /* store min or max */ }
B. class MinMax<E implements Comparable<E>> {
E min = null;
E max = null;
public MinMax() {}
public void put(E value) { /* store min or max */ }
C. class MinMax<E extends Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() {}
public <E> void put(E value) { /* store min or max */ }
D. class MinMax<E extends Comparable<E>> {
E min = null;
E max = null;
public MinMax() {}
public void put(E value) { /* store min or max */ }
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 124
Click the Exhibit button.
What is the result?
A. Compilation fails because of an error in line 18.
B. 0000
C. 4321
D. An exception is thrown at runtime.
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 125
Given:
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestB
B. An exception is thrown at runtime.
C. TestA
D. Compilation fails.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 126
Given:
1. public class Base {
2. public static final String FOO = "foo";
3. public static void main(String[] args) {
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";}
What is the result?
A. foofoofoobarbar
B. foofoofoobarfoo
C. foobarfoofoofoo
D. barbarbarbarbar
E. foobarfoobarfoo
F. foofoofoofoofoo
G. foobarfoobarbar
-----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 127
 DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 128
Click the Exhibit button.
Given:
31. public void method() {
32. A a = new A();
33. a.method1();
34. }
Which statement is true if a TestException is thrown on line 3 of class B?
A. The method declared on line 31 must be declared to throw a RuntimeException.
B. Line 33 must be called within a try block.
C. The exception thrown by method1 in class A is not required to be caught.
D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch
block.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 129
Given
10. class Foo {
11. static void alpha() { /* more code here */ }
12. void beta() { /* more code here */ }
13. }
Which two statements are true? (Choose two.)
A. Foo.beta() is a valid invocation of beta().
B. Method alpha() can directly call method beta().
C. Method beta() can directly call method alpha().
D. Foo.alpha() is a valid invocation of alpha().
--------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 130
Given:
35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;
Which two statements are true? (Choose two.)
A. Line 35 will not compile.
B. Line 38 will not compile.
C. Line 37 will not compile.
D. Line 36 will not compile.
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 131
DRAG DROP
Click the Task button.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 132
Click the Exhibit button.
Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?
A. this = s.defaultReadObject();
B. y = s.readInt(); x = s.readInt();
C. x = s.readInt(); y = s.readInt();
D. s.defaultReadObject();
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 133
Given:
1. public class Boxer1{
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. A NumberFormatException occurs at runtime.
B. An IllegalStateException occurs at runtime.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. Compilation fails because of an error in line 5.
F. The value "4" is printed at the command line.
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 134
Given the command line java Pass2 and:
15. public class Pass2 {
16. public void main(String [] args) {
17. int x = 6;
18. Pass2 p = new Pass2();
19. p.doStuff(x);
20. System.out.print(" main x = " + x);
21. }
22.
23. void doStuff(int x) {
24. System.out.print(" doStuff x = " + x++);
25. }
26. }
What is the result?
A. doStuff x = 6 main x = 6
B. doStuff x = 6 main x = 7
C. doStuff x = 7 main x = 7
D. doStuff x = 7 main x = 6
E. Compilation fails.
F. An exception is thrown at runtime.
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 135
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B"); }
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }
What is the result?
A. Compilation fails because of an error in line 15.
B. Compilation fails because of an error in line 18.
C. B
D. The code runs with no output.
E. Compilation fails because of an error in line 12.
-----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 136
Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. The code executes normally, but nothing prints.
B. An exception is thrown at runtime.
C. The code executes normally and prints "bar".
D. Compilation fails.
-------------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 137
Given:
1. public class Drink implements Comparable {
2. public String name;
3. public int compareTo(Object o) {
4. return 0;
5. }
6. }
and:
20. Drink one = new Drink();
21. Drink two = new Drink();
22. one.name= "Coffee";
23. two.name= "Tea";
23. TreeSet set = new TreeSet();
24. set.add(one);
25. set.add(two);
A programmer iterates over the TreeSet and prints the name of each Drink object.
What is the result?
A. Coffee
B. Coffee
Tea
C. Compilation fails.
D. An exception is thrown at runtime.
E. Tea
F. The code runs with no output.
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 138
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 139
Given:
10: public class Hello {
11: String title;
12: int value;
13: public Hello() {
14: title += " World";
15: }
16: public Hello(int value) {
17: this.value = value;
18: title = "Hello";
19: Hello();
20: }
21: }
and:
30: Hello c = new Hello(5);
31: System.out.println(c.title);
What is the result?
A. An exception is thrown at runtime.
B. Hello World 5
C. The code runs with no output.
D. Hello World
E. Compilation fails.
F. Hello
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 140
DRAG DROP
Click the Task button.
----------------------------------------------------------------------------------------------------------------------------------------------- QUESTION NO: 141
Given:
1. class ClassA {
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances) {
4. this.numberOfInstances = numberOfInstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfInstances) {
9. super(numberOfInstances);
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOfInstances);
14. }
15. }
Which statement is true?
A. An exception is thrown at runtime.
B. All constructors must be declared public.
C. Constructors CANNOT use the private modifier.
D. 420 is the output.
E. Constructors CANNOT use the protected modifier.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 142
Click the Exhibit button.
What two must the programmer do to correct the compilation errors? (Choose two.)
A. insert a call to super(vin) in the MeGo constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to this() in the Car constructor
D. change line 3 in the MeGo class to super.wheelCount = 3;
E. change the wheelCount variable in Car to protected
F. insert a call to super() in the MeGo constructor
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 143
Given:
20. public class CreditCard {
21.
22. private String cardID;
23. private Integer limit;
24. public String ownerName;
25.
26. public void setCardInformation(String cardID,
27. String ownerName,
28. Integer limit) {
29. this.cardID = cardID;
30. this.ownerName = ownerName;
31. this.limit = limit;
32. }
33. }
Which statement is true?
A. The ownerName variable breaks encapsulation.
B. The cardID and limit variables break polymorphism.
C. The class is fully encapsulated.
D. The setCardInformation method breaks encapsulation.
E. The code demonstrates polymorphism.
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 144
Click the Exhibit button.
What is the output if the main() method is run?
A. 5
B. 8
C. An exception is thrown at runtime.
D. It is impossible to determine for certain.
E. 9
F. 4
G. Compilation fails.
--------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 145
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 146
Click the Exhibit button.
Given:
34. Test t = new Test();
35. t.method(5);
What is the output from line 5 of the Test class?
A. 17
B. 10
C. 12
D. 5
E. 24

QUESTION NO: 147
Given:
10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} }
And:
20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a : list) {
24. a.x();
25. a.y();
26. }
What is the result?
A. An exception is thrown at runtime.
B. The code runs with no output.
C. Compilation fails because of an error in line 25.
D. Compilation fails because of an error in line 20.
E. Compilation fails because of an error in line 21.
F. Compilation fails because of an error in line 23.
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 148
DRAG DROP
Click the Task button.



QUESTION NO: 149
Given:
1. public class Target {
2. private int i = 0;
3. public int addOne(){
4. return ++i;
5. }
6. }
And:
1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?
A. Line 3 of class Target can be changed to private int addOne(){
B. Line 2 of class Target can be changed to private Integer i = 0;
C. Line 4 of class Target can be changed to return i++;
D. Line 2 of class Target can be changed to private int i = 1;
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 150
Given:
10. abstract public class Employee {
11. protected abstract double getSalesAmount();
12. public double getCommision() {
13. return getSalesAmount() * 0.15;
14. }
15. }
16. class Sales extends Employee {
17. // insert method here
18. }
Which two methods, inserted independently at line 17, correctly complete the Sales class?
(Choose two.)
A. private double getSalesAmount() { return 1230.45; }
B. protected double getSalesAmount() { return 1230.45; }
C. public double getSalesAmount() { return 1230.45; }
D. double getSalesAmount() { return 1230.45; }
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 151
Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + " ");
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
What is the result?
A. An exception is thrown at runtime.
2 3 4
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3
D. An exception is thrown at runtime.
1 2 3 4
E. An exception is thrown at runtime.
1 2 3
F. No output is produced.
1 2 3 4
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 152
Which three will compile and run without exception? (Choose three.)
A. private synchronized Object o;
B. public synchronized void go() { /* code here */ }
C. void go() {
synchronized() { /* code here */ }
D. void go() {
Object o = new Object();
synchronized(o) { /* code here */ }
E. void go() {
synchronized(Object.class) { /* code here */ }
F. private synchronized(this) void go() { /* code here */ }

QUESTION NO: 153
A programmer needs to create a logging method that can accept an arbitrary number of
arguments. For example, it may be called in these ways:
logIt("log message1");
logIt("log message2","log message3");
logIt("log message4","log message5","log message6");
Which declaration satisfies this requirement?
A. public void logIt(String * msgs)
B. public void logIt(String... msgs)
C. public void logIt(String [] msgs)
D. public void logIt(String msg1, String msg2, String msg3)
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 154
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 155
Given:
23. Object [] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print(" ");
33. }
What is the result?
A. A ClassCastException occurs in line 29.
B. Compilation fails due to an error in line 29.
C. Compilation fails due to an error in line 23.
D. A ClassCastException occurs in line 31.
E. The value of all four objects prints in natural order.
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 156
Given:
12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11.
What is the result if this code is run on December 14, 2000?
A. An exception is thrown at runtime.
B. The value of s is Dec 14, 2000.
C. Compilation fails because of an error in line 13.
D. The value of s is 14-dic-2004.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 157
Given:
11. public enum Title {
12. MR("Mr."), MRS("Mrs."), MS("Ms.");
13. private final String title;
14. private Title(String t) { title = t; }
15. public String format(String last, String first) {
16. return title + " " + first + " " + last;
17. }
18. }
19. public static void main(String[] args) {
20. System.out.println(Title.MR.format("Doe", "John"));
21. }
What is the result?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 15.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 20.
E. Mr. John Doe
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 158
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------------------- :
QUESTION NO: 159
Assuming that the serializeBanana2() and the deserializeBanana2() methods will correctly use
Java serialization and given:
13. import java.io.*;
14. class Food {Food() { System.out.print("1"); } }
15. class Fruit extends Food implements Serializable {
16. Fruit() { System.out.print("2"); } }
17. public class Banana2 extends Fruit { int size = 42;
18. public static void main(String [] args) {
19. Banana2 b = new Banana2();
20. b.serializeBanana2(b); // assume correct serialization
21. b = b.deserializeBanana2(b); // assume correct
22. System.out.println(" restored " + b.size + " "); }
23. // more Banana2 methods
24. }
What is the result?
A. An exception is thrown at runtime.
B. 12 restored 42
C. 1212 restored 42
D. Compilation fails.
E. 121 restored 42
F. 1 restored 42
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 160
Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base?
A. The class must belong to the geometry package.
B. It can be any class.
C. No class has access to base.
D. The class must be a subclass of the class Hypotenuse.
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 161
Given:
11. public abstract class Shape {
12. int x;
13. int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
and a class Circle that extends and fully implements the Shape class.
Which is correct?
A. Shape s = new Circle();
s.setAnchor(10,10);
s.draw();
B. Circle c = new Circle();
c.Shape.setAnchor(10,10);
c.Shape.draw();
C. Circle c = new Shape();
c.setAnchor(10,10);
c.draw();
D. Shape s = new Shape();
s.setAnchor(10,10);
s.draw();
E. Shape s = new Circle();
s->setAnchor(10,10);
s->draw();
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 162
Given:
enum Example { ONE, TWO, THREE }
Which statement is true?
A. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted
because enumerated types do NOT implement java.lang.Comparable.
B. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must
use a java.util.EnumMap.
D. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is
guaranteed to be less than one.
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 163
Given:
1. public class Person {
2. private String name;
3. public Person(String name) { this.name = name; }
4. public boolean equals(Person p) {
5. return p.name.equals(this.name);
6. }
7. }
Which statement is true?
A. When adding Person objects to a java.util.Set collection, the equals method in line 4 will
prevent duplicates.
B. Compilation fails because the private attribute p.name cannot be accessed in line 5.
C. The equals method does NOT properly override the Object.equals method.
D. To work correctly with hash-based data structures, this class must also implement the
hashCode method.
-----------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 164
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2. public class Commander {
3. public static void main(String[] args) {
4. // more code here
5. }
6. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that
the classpath contains "." (current directory).
Which command line correctly runs Commander?
A. java Commander
B. java com.sun.sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com/sun/sjcp Commander
E. java -cp com.sun.sjcp Commander
--------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 165
DRAG DROP
Click the Task button.
-----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 166
A JavaBeans component has the following field:
11. private boolean enabled;
Which two pairs of method declarations follow the JavaBeans standard for accessing this field?
(Choose two.)
A. public void setEnabled( boolean enabled )
public boolean getEnabled()
B. public boolean setEnabled( boolean enabled )
public boolean getEnabled()
C. public void setEnabled( boolean enabled )
public void isEnabled()
D. public void setEnabled( boolean enabled )
public boolean isEnabled()
----------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 167
Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base?
A. It can be any class.
B. The class must belong to the geometry package.
C. The class must be a subclass of the class Hypotenuse.
D. No class has access to base.
---------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 168
A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user wants to execute the
main method of Poker on a UNIX system using the command:
java games.cards.Poker
What allows the user to do this?
A. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java
B. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar
C. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include
/stuff/java/*.jar
D. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include
/stuff/java/Poker.jar
E. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar
F. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java
-------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 169
Which two statements are true? (Choose two.)
A. An encapsulated class allows subclasses to overload methods, but does NOT allow overriding
methods.
B. An encapsulated, public class promotes re-use.
C. Classes that share the same interface are always tightly encapsulated.
D. An encapsulated class allows a programmer to change an implementation without affecting
outside code.
----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 170
Given:
11. interface DeclareStuff {
12. public static final int EASY = 3;
13. void doStuff(int t); }
14. public class TestDeclare implements DeclareStuff {
15. public static void main(String [] args) {
16. int x = 5;
17. new TestDeclare().doStuff(++x);
18. }
19. void doStuff(int s) {
20. s += EASY + ++s;
21. System.out.println("s " + s);
22. }
23. }
What is the result?
A. Compilation fails.
B. s 16
C. s 14
D. An exception is thrown at runtime.
E. s 10
----------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 171
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. Compilation fails.
B. An instance of Forest is serialized.
C. An exception is thrown at runtime.
D. An instance of Forest and an instance of Tree are both serialized.
---------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 172
Given:
23. Object [] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print(" ");
33. }
What is the result?
A. A ClassCastException occurs in line 31.
B. The value of all four objects prints in natural order.
C. Compilation fails due to an error in line 23.
D. A ClassCastException occurs in line 29.
E. Compilation fails due to an error in line 29.
-----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 173
Given:
11. rbo = new ReallyBigObject();
12. // more code here
13. rbo = null;
14. /* insert code here */
Which statement should be placed at line 14 to suggest that the virtual machine expend effort
toward recycling the memory used by the object rbo?
A. Runtime.getRuntime().growHeap();
B. System.freeMemory();
C. System.gc();
D. Runtime.getRuntime().freeMemory();
E. Runtime.gc();
------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 174
Given this method in a class:
21. public String toString() {
22. StringBuffer buffer = new StringBuffer();
23. buffer.append('<');
24. buffer.append(this.name);
25. buffer.append('>');
26. return buffer.toString();
27. }
Which statement is true?
A. The programmer can replace StringBuffer with StringBuilder with no other changes.
B. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
C. This code is NOT thread-safe.
D. This code will perform well and converting the code to use StringBuilder will not enhance the
performance.
QUESTION NO: 175
Given:
foo and bar are public references available to many other threads. foo refers to a Thread and bar
is an Object. The thread foo is currently executing bar.wait().
From another thread, what provides the most reliable way to ensure that foo will stop executing
wait()?
A. bar.notifyAll();
B. Object.notify();
C. Thread.notify();
D. bar.notify();
E. foo.notify();
F. foo.notifyAll();
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 176
DRAG DROP
Click the Task button.
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 177
Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here
What updates d's value with the date represented by ds?
A. 18. d = df.getDate(ds);
B. 18. d = df.parse(ds);
C. 18. try {
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D. 18. try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 178
Given:
33. try {
34. // some code here
35. } catch (NullPointerException e1) {
36. System.out.print("a");
37. } catch (RuntimeException e2) {
38. System.out.print("b");
39. } finally {
40. System.out.print("c");
41. }
What is the result if a NullPointerException occurs on line 34?
A. ab
B. abc
C. bc
D. c
E. ac
F. a

QUESTION NO: 179
Given:
11. double input = 314159.26;
12. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
13. String b;
14. //insert code here
Which code, inserted at line 14, sets the value of b to 314.159,26?
A. b = nf.parseObject( input );
B. b = nf.parse( input );
C. b = nf.format( input );
D. b = nf.equals( input );
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 180
Given:
11. public static Iterator reverse(List list) {
12. Collections.reverse(list);
13. return list.iterator();
14. }
15. public static void main(String[] args) {
16. List list = new ArrayList();
17. list.add("1"); list.add("2"); list.add("3");
18. for (Object obj: reverse(list))
19. System.out.print(obj + ", ");
20. }
What is the result?
A. The code runs with no output.
B. 3, 2, 1,
C. An exception is thrown at runtime.
D. Compilation fails.
E. 1, 2, 3,
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 181
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. }
And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH
environment variable is set to "." (current directory).
Which two java commands entered at the command line will run MainClass? (Choose two.)
A. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
B. java -classpath . MainClass if run from the /apps/com/company/application directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java MainClass if run from the /apps directory
E. java com.company.application.MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application
directory
----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 182
Given:
1. public class Target {
2. private int i = 0;
3. public int addOne(){
4. return ++i;
5. }
6. }
And:
1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?
A. Line 3 of class Target can be changed to private int addOne(){
B. Line 2 of class Target can be changed to private Integer i = 0;
C. Line 4 of class Target can be changed to return i++;
D. Line 2 of class Target can be changed to private int i = 1;
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 183
Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a
candidate for garbage collection?
A. The object is NOT a candidate for garbage collection.
B. Line 18
C. Line 19
D. Line 16
E. Line 17
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 184
Click the Exhibit button.
What is the result?
A. Compilation fails.
B. Value is: -12
C. Value is: 8
D. The code runs with no output.
E. Value is: 12
F. An exception is thrown at runtime.
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 185
Given:
1. public class TestOne implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread(new TestOne());
4. t.start();
5. System.out.print("Started");
6. t.join();
7. System.out.print("Complete");
8. }
9. public void run() {
10. for (int i = 0; i < 4; i++) {
11. System.out.print(i);
12. }
13. }
14. }
What can be a result?
A. The code executes and prints "Started0123Complete".
B. An exception is thrown at runtime.
C. Compilation fails.
D. The code executes and prints "StartedComplete".
E. The code executes and prints "StartedComplete0123".
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 186
Given: 1. public class TestString3 { 2. public static void main(String[] args) { 3. // insert code here
5. System.out.println(s); 6. } 7. } Which two code fragments, inserted independently at line 3,
generate the output 4247? (Choose two.)
A. StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");
B. StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
D. StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E. StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
Answer: A,B
QUESTION NO: 187
Which three statements concerning the use of the java.io.Serializable interface are true? (Choose
three.)
A. The values in fields with the transient modifier will NOT survive serialization and deserialization.
B. The values in fields with the volatile modifier will NOT survive serialization and deserialization.
C. An object serialized on one JVM can be successfully deserialized on a different JVM.
D. It is legal to serialize an object of a type that has a supertype that does NOT implement
java.io.Serializable.
E. Objects from classes that use aggregation cannot be serialized.
------------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 188
Given:
11. interface DeclareStuff {
12. public static final int EASY = 3;
13. void doStuff(int t); }
14. public class TestDeclare implements DeclareStuff {
15. public static void main(String [] args) {
16. int x = 5;
17. new TestDeclare().doStuff(++x);
18. }
19. void doStuff(int s) {
20. s += EASY + ++s;
21. System.out.println("s " + s);
22. }
23. }
What is the result?
A. s 10
B. s 16
C. An exception is thrown at runtime.
D. Compilation fails.
E. s 14
--------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 189
Given:
11. public static void main(String[] args) {
12. String str = "null";
13. if (str == null) {
14. System.out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }
What is the result?
A. An exception is thrown at runtime.
B. some
C. zero
D. Compilation fails.
E. null
-----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 190
Given:
10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args ) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?
A. Beta b = (Beta)(Alpha)x;
B. Foo f = (Alpha)x;
C. Alpha a = x;
D. Foo f = (Delta)x;
------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 191
Given:
1. public class MyLogger {
2. private StringBuilder logger = new StringBuuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7. }
The programmer must guarantee that a single MyLogger object works properly for a multithreaded
system.
How must this code be changed to be thread-safe?
A. replace StringBuilder with StringBuffer
B. synchronize the log method
C. No change is necessary, the current MyLogger code is already thread-safe.
D. replace StringBuilder with just a String object and use the string concatenation (+=) within the
log method
---------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 192
DRAG DROP
Click the Task button.
------------------------------------------------------------------------------------------------------------------------------------------------
 QUESTION NO: 193
Given:
35. int x = 10;
36. do { 37. x--;
38. } while (x < 10);
How many times will line 37 be executed?
A. more than ten times
B. ten times
C. zero times
D. one to nine times
----------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 194
Given:
11. public abstract class Shape {
12. private int x;
13. private int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
Which two classes use the Shape class correctly? (Choose two.)
A. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
B. public abstract class Circle implements Shape {
private int radius;
public void draw() { /* code here */ }
C. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
D. public class Circle implements Shape {
private int radius;
}
E. public abstract class Circle extends Shape {
private int radius;
}
F. public class Circle extends Shape {
private int radius;
public void draw();
}
--------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 195
Given:
public class NamedCounter {
private final String name;
private int count;
public NamedCounter(String name) { this.name = name; }
public String getName() { return name; }
public void increment() { count++; }
public int getCount() { return count; }
public void reset() { count = 0; }
}
Which three changes should be made to adapt this class to be used safely by multiple threads?
(Choose three.)
A. declare reset() using the synchronized keyword
B. declare the constructor using the synchronized keyword
C. declare getName() using the synchronized keyword
D. declare getCount() using the synchronized keyword
E. declare increment() using the synchronized keyword
------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 196
Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A. public class MinMax<N extends Number> {
B. public class MinMax<?> {
C. public class MinMax<N extends Object> {
D. public class MinMax<? extends Object> {
E. public class MinMax<? extends Number> {
F. public class MinMax<N extends Integer> {
------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 197
Given:
11. static void test() {
12. try {
13. String x = null;
14. System.out.print(x.toString() + " ");
15. }
16. finally { System.out.print("finally "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (Exception ex) { System.out.print("exception "); }
21. }
What is the result?
A. finally exception
B. Compilation fails.
C. null finally
D. finally
E. null
-----------------------------------------------------------------------------------------------------------------------------------------------------
QUESTION NO: 198
When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in
only one of the two?
A. writing to the stream
B. closing the stream
C. flushing the stream
D. marking a location in the stream
E. writing a line separator to the stream
Answer: E
QUESTION NO: 199
Given:
1. public class Threads2 implements Runnable {
2.
3. public void run() {
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. run.
java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
End of method.
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem

 136

E. java.lang.RuntimeException: Problem
Answer: B,D
QUESTION NO: 200
Which three statements concerning the use of the java.io.Serializable interface are true? (Choose
three.)
A. The values in fields with the transient modifier will NOT survive serialization and deserialization.
B. It is legal to serialize an object of a type that has a supertype that does NOT implement
java.io.Serializable.
C. The values in fields with the volatile modifier will NOT survive serialization and deserialization.
D. Objects from classes that use aggregation cannot be serialized.
E. An object serialized on one JVM can be successfully deserialized on a different JVM.
Answer: A,B,E
QUESTION NO: 201
Given:
1. public class TestFive {
2. private int x;
3. public void foo() {
4. int current = x;
5. x = current + 1;
6. }
7. public void go() {
8. for(int i = 0; i < 5; i++) {
9. new Thread() {
10. public void run() {
11. foo();
12. System.out.print(x + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A. wrap the code inside the foo() method with a synchronized( this ) block
B. wrap the for loop code inside the go() method with a synchronized block synchronized(this) { //
for loop code here }
C. move the line 12 print statement into the foo() method
D. change line 7 to public synchronized void go() {
E. change the variable declaration on line 2 to private volatile int x;
Answer: A,C
QUESTION NO: 202 DRAG DROP
Click the Task button.


QUESTION NO: 203
Which two classes correctly implement both the java.lang.Runnable and the java.lang.Clonable
interfaces? (Choose two.)
A. public abstract class Session
implements Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /*make a copy */ }
B. public class Session
implements Runnable, implements Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }
C. public class Session
extends Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }
D. public class Session
implements Runnable, Clonable {
public void run();
public Object clone();
}
E. public class Session
implements Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }
QUESTION NO: 204 DRAG DROP
Click the Task button.



QUESTION NO: 205
Click the Exhibit button.
What is the result?

A. An exception is thrown at runtime.
B. The code will deadlock.
C. The code may run with output "0 2 4 6".
D. The code may run with output "0 6".
E. The code may run with no output.
F. The code may run with output "2 0 6 4".
QUESTION NO: 206



Given:
11. abstract class Vehicle { public int speed() { return 0; }
12. class Car extends Vehicle { public int speed() { return 60; }
13. class RaceCar extends Car { public int speed() { return 150; }
...
21. RaceCar racer = new RaceCar();
22. Car car = new RaceCar();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + ", " + car.speed()
25. + ", " + vehicle.speed());
What is the result?
A. 0, 0, 0
B. 150, 150, 150
C. Compilation fails.
D. An exception is thrown at runtime.
E. 150, 60, 0

QUESTION NO: 207
Given:
1. package test;
2.
3. class Target {
4. public String name = "hello";
5. }
What can directly access and change the value of the variable name?
A. any class in the test package
B. any class
C. only the Target class
D. any class that extends Target



QUESTION NO: 208
Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError();
13. System.out.print("test ");
14. }
15. public static void main(String[] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }
What is the result?
A. exception end
B. exception test end
C. end
D. A Throwable is thrown by main.
E. Compilation fails.
F. An Exception is thrown by main.

QUESTION NO: 209
Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + " ");
19. }
20. }
21. } a
nd two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
What is the result?
A. An exception is thrown at runtime.
1 2 3 4
B. No output is produced.
1 2 3
C. No output is produced.
2 3 4
D. No output is produced.
1 2 3 4
E. An exception is thrown at runtime.
1 2 3
F. An exception is thrown at runtime.
2 3 4

QUESTION NO: 210
Click the Exhibit button.
Given:
25. A a = new A();
26. System.out.println(a.doit(4, 5));
What is the result?
A. Line 26 prints "b" to System.out.
B. Line 26 prints "a" to System.out.
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.



QUESTION NO: 211
Given:
11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13. System.out.print("pi is bigger than 3. ");
14. }
15. else {
16. System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }
What is the result?
A. pi is not bigger than 3. Have a nice day.
B. pi is bigger than 3. Have a nice day.
C. Compilation fails.
D. pi is bigger than 3.
E. An exception occurs at runtime.
QUESTION NO: 212
Given:
8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. }
12. }
Which two will produce an AssertionError? (Choose two.)
A. java -ea test file1 file2
B. java -ea test
C. java test
D. java test file1
E. java -ea test file1
F. java -ea:test test file1

QUESTION NO: 213 DRAG DROP
Click the Task button.

QUESTION NO: 214 DRAG DROP
Click the Task button.

QUESTION NO: 215
Given:
1. class TestA {
2. public void start() { System.out.println("TestA"); }



3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestB
B. Compilation fails.
C. TestA
D. An exception is thrown at runtime.

QUESTION NO: 216
Click the Exhibit button.
Given:
25. try {
26. A a = new A();
27. a.method1();
28. } catch (Exception e) {
29. System.out.print("an error occurred");
30. }
Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose
two.)


A. The code on line 5 of class A will execute.
B. The code on line 5 of class B will execute.
C. The application will crash.
D. The exception will be propagated back to line 27.
E. The code on line 29 will be executed.
QUESTION NO: 217
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. }
And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH
environment variable is set to "." (current directory).
Which two java commands entered at the command line will run MainClass? (Choose two.)
A. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath . MainClass if run from the /apps/com/company/application directory
D. java MainClass if run from the /apps directory
E. java com.company.application.MainClass if run from the /apps/com/company/application
directory
F. java -classpath /apps com.company.application.MainClass if run from any directory

QUESTION NO: 218
Given:
11. static void test() throws RuntimeException {
12. try {
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch (Exception ex) { System.out.print("exception "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (RuntimeException ex) { System.out.print("runtime "); }
21. System.out.print("end ");
22. }
What is the result?
A. test runtime end
B. A Throwable is thrown by main at runtime.
C. test end
D. Compilation fails.
E. test exception end

QUESTION NO: 219
Given:
1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print("foo");
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "foo".
D. The code executes normally, but nothing is printed.

QUESTION NO: 220
Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here
What updates d's value with the date represented by ds?
A. 18. d = df.parse(ds);
B. 18. try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
C. 18. try {
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D. 18. d = df.getDate(ds);

QUESTION NO: 221 DRAG DROP
Click the Task button.

QUESTION NO: 222 DRAG DROP
Click the Task button.

QUESTION NO: 223
Given:
1. import java.util.*;
2.
3. public class LetterASort{
4. public static void main(String[] args) {
5. ArrayList<String> strings = new ArrayList<String>();
6. strings.add("aAaA");
7. strings.add("AaA");
8. strings.add("aAa");
9. strings.add("AAaa");
10. Collections.sort(strings);
11. for (String s : strings) { System.out.print(s + " "); }
12. }
13. }
What is the result?
A. Compilation fails.
B. AAaa AaA aAa aAaA
C. aAaA aAa AAaa AaA
D. aAa AaA aAaA AAaa
E. AaA AAaa aAaA aAa
F. An exception is thrown at runtime.
QUESTION NO: 224
Given:
10. public class Bar {
11. static void foo( int... x ) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will allow the class to compile?
(Choose two.)
A. while( x.hasNext() ) System.out.println( x.next() );
B. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);
C. foreach( x ) System.out.println(z);
D. for( int z : x ) System.out.println(z);
QUESTION NO: 225 DRAG DROP
Click the Task button.




QUESTION NO: 226
Given:
25. int x = 12;
26. while (x < 10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. Line 29 will never be reached.
B. 12
C. 0
D. 10

QUESTION NO: 227
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
};
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void run() { doStuff(); }
}.start();
D. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();
F. new Thread() {
public void start() { doStuff(); }
}.run();
QUESTION NO: 228
Click the Exhibit button.
Given the fully-qualified class names:
com.foo.bar.Dog
com.foo.bar.blatz.Book
com.bar.Car
com.bar.blatz.Sun
Which graph represents the correct directory structure for a JAR file from which those classes can
be used by the compiler and JVM?
A. Jar D
B. Jar E
C. Jar C
D. Jar A
E. Jar B
QUESTION NO: 229
Click the Exhibit button.
Which two are possible results? (Choose two.)
A. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
B. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 2, 4, 4, 6, 8, 10, 6,
E. 0, 2, 4, 6, 8, 10, 2, 4,
QUESTION NO: 230
Given:
10. public class Foo {
11. static int[] a;
12. static { a[0]=2; }
13. public static void main( String[] args ) {}
14. }
Which exception or error will be thrown when a programmer attempts to run this code?
A. java.lang.StackOverflowError
B. java.lang.ArrayIndexOutOfBoundsException
C. java.lang.IllegalStateException
D. java.lang.ExceptionInInitializerError

QUESTION NO: 231
Given:
1. public class Person {
2. private String name;
3. public Person(String name) { this.name = name; }
4. public boolean equals(Person p) {
5. return p.name.equals(this.name);
6. }
7. }
Which statement is true?
A. Compilation fails because the private attribute p.name cannot be accessed in line 5.
B. The equals method does NOT properly override the Object.equals method.
C. To work correctly with hash-based data structures, this class must also implement the
hashCode method.
D. When adding Person objects to a java.util.Set collection, the equals method in line 4 will
prevent duplicates.

QUESTION NO: 232
Given:
d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
C. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
D. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));

QUESTION NO: 233
Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. Synchronizing the run() method would make the class thread-safe.
C. An exception is thrown at runtime.
D. The data in variable "x" are protected from concurrent access problems.
E. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would
make the class thread-safe.
F. Declaring the doThings() method as static would make the class thread-safe.

QUESTION NO: 234
A developer is creating a class Book, that needs to access class Paper. The Paper class is
deployed in a JAR named myLib.jar. Which three, taken independently, will allow the developer to
use the Paper class while compiling the Book class? (Choose three.)
A. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp
/foo/myLib.jar/Paper Book.java.
B. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that
includes /foo/myLib.jar/Paper.class.
D. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
E. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that
includes /foo/myLib.jar.
F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath
/foo/myLib.jar Book.java
G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d
/foo/myLib.jar Book.java
QUESTION NO: 235
Given:
11. class ClassA {}
12. class ClassB extends ClassA {}
13. class ClassC extends ClassA {}
and:
21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();
Which three are valid? (Choose three.)
A. p0 = p1;
B. p1 = (ClassB)p3;
C. p2 = (ClassC)p1;
D. p2 = (ClassC)p4;
E. p2 = p4;
F. p1 = p2;
Answer: A,B,D

 161

QUESTION NO: 236
Which Man class properly represents the relationship "Man has a best friend who is a Dog"?
A. class Man extends Dog { }
B. class Man { private Dog<bestFriend>; }
C. class Man implements Dog { }
D. class Man { private Dog bestFriend; }
E. class Man { private BestFriend<dog>; }
F. class Man { private BestFriend dog; }

QUESTION NO: 237 DRAG DROP
Click the Task button.


QUESTION NO: 238
Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java
serialization and given:
13. import java.io.*;
14. class Food implements Serializable {int good = 3;}
15. class Fruit extends Food {int juice = 5;}
16. public class Banana extends Fruit {
17. int yellow = 4;
18. public static void main(String [] args) {
19. Banana b = new Banana(); Banana b2 = new Banana();
20. b.serializeBanana(b); // assume correct serialization
21. b2 = b.deserializeBanana(); // assume correct
22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good);
24. }
25. // more Banana methods go here 50. }
What is the result?
A. restore 403
B. An exception is thrown at runtime.
C. Compilation fails.
D. restore 453
E. restore 400

QUESTION NO: 239
Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue<String> pq = new PriorityQueue<String>();
5. pq.add("carrot");
6. pq.add("apple");
7. pq.add("banana");
8. System.out.println(pq.poll() + ":" + pq.peek());
9. }
10. }
What is the result?
A. banana:apple
B. carrot:apple
C. apple:banana
D. carrot:banana
E. apple:apple
F. carrot:carrot

QUESTION NO: 240
Given:
1. public class A {
2. public void doit() {
3. }
4. public String doit() {
5. return "a";
6. }
7. public double doit(int x) {
8. return 1.0;
9. }
10. }
What is the result?
A. Compilation fails because of an error in line 7.
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 4.
D. Compilation succeeds and no runtime errors with class A occur.
QUESTION NO: 241
Given:
1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. The code executes normally and prints "sleep".
B. The code executes normally, but nothing is printed.
C. Compilation fails.
D. An exception is thrown at runtime.

QUESTION NO: 242
Given:
11. public static Iterator reverse(List list) {
12. Collections.reverse(list);
13. return list.iterator();
14. }
15. public static void main(String[] args) {
16. List list = new ArrayList();
17. list.add("1"); list.add("2"); list.add("3");
18. for (Object obj: reverse(list))
19. System.out.print(obj + ", ");
20. }
What is the result?
A. 1, 2, 3,
B. 3, 2, 1,
C. The code runs with no output.
D. Compilation fails.
E. An exception is thrown at runtime.
QUESTION NO: 243
Given:
11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);
What is the result?
A. 4
B. Compilation fails.
C. 0
D. An exception is thrown at runtime.
E. 1

QUESTION NO: 244
Given:
11. public static void main(String[] args) {
12. Integer i = new Integer(1) + new Integer(2);
13. switch(i) {
14. case 3: System.out.println("three"); break;
15. default: System.out.println("other"); break;
16. }
17. }
What is the result?
A. three
B. Compilation fails because of an error on line 12.
C. Compilation fails because of an error on line 13.
D. other
E. An exception is thrown at runtime.
F. Compilation fails because of an error on line 15.
QUESTION NO: 245
Given:
11. String[] elements = { "for", "tea", "too" };
12. String first = (elements.length > 0) ? elements[0] : null;
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The variable first is set to elements[0].
D. The variable first is set to null.
QUESTION NO: 246
A programmer has an algorithm that requires a java.util.List that provides an efficient
implementation of add(0, object), but does NOT need to support quick random access.
What supports these requirements?
A. java.util.ArrayList
B. java.util.Queue
C. java.util.LinearList
D. java.util.LinkedList
QUESTION NO: 247 DRAG DROP
Click the Task button.

QUESTION NO: 248
Assuming that the serializeBanana2() and the deserializeBanana2() methods will correctly use
Java serialization and given:
13. import java.io.*;
14. class Food {Food() { System.out.print("1"); } }
15. class Fruit extends Food implements Serializable {
16. Fruit() { System.out.print("2"); } }
17. public class Banana2 extends Fruit { int size = 42;
18. public static void main(String [] args) {
19. Banana2 b = new Banana2();
20. b.serializeBanana2(b); // assume correct serialization
21. b = b.deserializeBanana2(b); // assume correct
22. System.out.println(" restored " + b.size + " "); }
23. // more Banana2 methods
24. }
What is the result?
A. 1212 restored 42
B. 121 restored 42
C. 12 restored 42
D. 1 restored 42
E. Compilation fails.
F. An exception is thrown at runtime.

QUESTION NO: 249
Given:
10. class Nav{
11. public enum Direction { NORTH, SOUTH, EAST, WEST }
12. }
13. public class Sprite{
14. // insert code here
15. }
Which code, inserted at line 14, allows the Sprite class to compile?
A. Nav.Direction d = Nav.Direction.NORTH;
B. Nav.Direction d = NORTH;
C. Direction d = NORTH;
D. Direction d = Direction.NORTH;
QUESTION NO: 250
Given:
1. public class Boxer1{
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. A NullPointerException occurs at runtime.
B. The value "4" is printed at the command line.
C. Compilation fails because of an error in line 5.
D. A NumberFormatException occurs at runtime.
E. Compilation fails because of an error in line 9.
F. An IllegalStateException occurs at runtime.
QUESTION NO: 251 DRAG DROP
Click the Task button.

QUESTION NO: 252
Given:
10. public class MyClass {
11.
12. public Integer startingI;
13. public void methodA() {
14. Integer i = new Integer(25);
15. startingI = i;
16. methodB(i);
17. }
18. private void methodB(Integer i2) {
19. i2 = i2.intValue();
20.
21. }
22. }
If methodA is invoked, which two are true at line 20? (Choose two.)
A. i2.equals(startingI) returns true.
B. i2 == startingI returns true.
C. i2.equals(startingI) returns false.
D. i2 == startingI returns false.
QUESTION NO: 253
Given:
11. public class Person {
12. private String name, comment;
13. private int age;
14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
16. }
17. public boolean equals(Object o) {
18. if (! (o instanceof Person)) return false;
19, Person p = (Person)o;
20. return age == p.age && name.equals(p.name);
21. }
22. }
What is the appropriate definition of the hashCode method in class Person?
A. return super.hashCode();
B. return name.hashCode() + comment.hashCode() / 2;
C. return name.hashCode() + age * 7;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;
QUESTION NO: 254
Given a method that must ensure that its parameter is not null:
11. public void someMethod(Object value) {
12. // check for null value
...
20. System.out.println(value.getClass());
21. }
What, inserted at line 12, is the appropriate way to handle a null value?
A. assert value == null;
B. assert value != null, "value is null";
C. if (value == null) {
throw new AssertionException("value is null");
}
D. if (value == null) {
throw new IllegalArgumentException("value is null");
}

QUESTION NO: 255 DRAG DROP
Click the Task button.

QUESTION NO: 256
Given:
1. import java.util.*;
2. public class Old {
3. public static Object get0(List list) {
4. return list.get(0);
5. }
6. }
Which three will compile successfully? (Choose three.)
A. Object o = Old.get0(new LinkedList<Object>());
B. Object o = Old.get0(new LinkedList());
C. String s = (String)Old.get0(new LinkedList<String>());
D. Object o = Old.get0(new LinkedList<?>());
E. String s = Old.get0(new LinkedList<String>());

QUESTION NO: 257
Given:
33. Date d = new Date(0);
34. String ds = "December 15, 2004";
35. // insert code here
36. try {
37. d = df.parse(ds);
38. }
39. catch(ParseException e) {
40. System.out.println("Unable to parse " + ds);
41. }
42. // insert code here too
What creates the appropriate DateFormat object and adds a day to the Date object?
A. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setTime( (60 * 60 * 24) + d.getTime());
B. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());
C. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
QUESTION NO: 258
Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A. This code may throw an InterruptedException.
B. A call to notify() or notifyAll() from another thread may cause this method to complete normally.
C. This code may throw a TimeoutException after ten minutes.
D. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.
E. This code may throw an IllegalStateException.
F. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
QUESTION NO: 259
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. private static void process(byte[] b) {}
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. util.BitUtils.process(bytes);
B. import util.BitUtils.*; process(bytes);
C. process(bytes);
D. app.BitUtils.process(bytes);
E. SomeApp cannot use the process method in BitUtils.
F. BitUtils.process(bytes);
QUESTION NO: 260
Given:
11. public class Commander {
12. public static void main(String[] args) {
13. String myProp = /* insert code here */
14. System.out.println(myProp);
15. }
16. }
and the command line:
java -Dprop.custom=gobstopper Commander
Which two, placed on line 13, will produce the output gobstopper? (Choose two.)
A. System.getProperty("prop.custom");
B. System.getProperties().getProperty("prop.custom");
C. System.property("prop.custom");
D. System.getenv("prop.custom");
E. System.load("prop.custom");
QUESTION NO: 261
Given:
11. Runnable r = new Runnable() {
12. public void run() {
13. System.out.print("Cat");
14. }
15. };
16. Thread t = new Thread(r) {
17. public void run() {
18. System.out.print("Dog");
19. }
20. };
21. t.start();
What is the result?
A. An exception is thrown at runtime.
B. The code runs with no output.
C. Cat
D. Compilation fails.
E. Dog
QUESTION NO: 262
Given:
23. int z = 5;
24.
25. public void stuff1(int x) {
26. assert (x > 0);
27. switch(x) {
28. case 2: x = 3;
29. default: assert false; } }
30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4()); }
34.
35. private boolean stuff4() { z = 6; return false; }
Which statement is true?
A. Only the assert statement on line 31 is used appropriately.
B. All of the assert statements are used appropriately.
C. The assert statements on lines 29 and 33 are used appropriately.
D. The assert statements on lines 29, 31, and 33 are used appropriately.
E. The assert statements on lines 29 and 31 are used appropriately.
F. The assert statements on lines 26 and 29 are used appropriately.
G. The assert statements on lines 26, 29, and 31 are used appropriately.
QUESTION NO: 263
Given a method that must ensure that its parameter is not null:
11. public void someMethod(Object value) {
12. // check for null value
...
20. System.out.println(value.getClass());
21. }
What, inserted at line 12, is the appropriate way to handle a null value?
A. if (value == null) {
throw new IllegalArgumentException("value is null");
}
B. assert value == null;
C. if (value == null) {
throw new AssertionException("value is null");
}
D. assert value != null, "value is null";
QUESTION NO: 264 DRAG DROP
Click the Task button.

QUESTION NO: 265
Given:
11. public class Test {
12. public enum Dogs {collie, harrier, shepherd};
13. public static void main(String [] args) {
14. Dogs myDog = Dogs.shepherd;
15. switch (myDog) {
16. case collie:
17. System.out.print("collie ");
18. case default:
19. System.out.print("retriever ");
20. case harrier:
21. System.out.print("harrier ");
22. }
23. }
24. }
What is the result?
A. shepherd
B. An exception is thrown at runtime.
C. retriever
D. retriever harrier
E. Compilation fails.
F. harrier
QUESTION NO: 266
Given:
d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
C. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
D. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
QUESTION NO: 267
Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError();
13. System.out.print("test ");
14. }
15. public static void main(String[] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }
What is the result?
A. Compilation fails.
B. exception end
C. exception test end
D. end
E. An Exception is thrown by main.
F. A Throwable is thrown by main.
QUESTION NO: 268
Given:
10. abstract class A {
11. abstract void a1();
12. void a2() { }
13. }
14. class B extends A {
15. void a1() { }
16. void a2() { }
17. }
18. class C extends B { void c1() { } }
and:
A x = new B(); C y = new C(); A z = new C();
What are four valid examples of polymorphic method calls? (Choose four.)
A. x.a2();
B. x.a1();
C. z.a2();
D. z.a1();
E. y.c1();
F. z.c1();
QUESTION NO: 269
Given:
10. public class SuperCalc {
11. protected static int multiply(int a, int b) { return a * b;}
12. }
and:
20. public class SubCalc extends SuperCalc{
21. public static int multiply(int a, int b) {
22. int c = super.multiply(a, b);
23. return c;
24. }
25. }
and:
30. SubCalc sc = new SubCalc ();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2));
What is the result?
A. The code runs with no output.
B. 12
4
C. Compilation fails because of an error in line 31.
D. Compilation fails because of an error in line 21.
E. An exception is thrown at runtime.
F. Compilation fails because of an error in line 22.
QUESTION NO: 270
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B "); }
16. }
17. public static void main(String[] args) {
18. A a = new B();
19. a.process();
20. }
What is the result?
A. The code runs with no output.
B. Compilation fails because of an error in line 15.
C. Compilation fails because of an error in line 19.
D. An exception is thrown at runtime.
E. B
F. Compilation fails because of an error in line 18.
QUESTION NO: 271
Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?
A. After line 15, the value of age is 5.
B. An exception is thrown at runtime.
C. Compilation fails.
D. After line 15, the value of age is 3.
QUESTION NO: 272
Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Which three are valid on line 12? (Choose three.)
A. private
B. native
C. public
D. protected
E. abstract
F. final
G. static
QUESTION NO: 273
Given:
10. interface A { public int getValue(); }
11. class B implements A {
12. public int getValue() { return 1; }
13. }
14. class C extends B {
15. // insert code here
16. }
Which three code fragments, inserted individually at line 15, make use of polymorphism? (Choose
three.)
A. public void add(A a, B b) { a.getValue(); }
B. public void add(C c1, C c2) { c1.getValue(); }
C. public void add(C c) { c.getValue(); }
D. public void add(B b) { b.getValue(); }
E. public void add(A a) { a.getValue(); }
QUESTION NO: 274
Given:
11. class Snoochy {
12. Boochy booch;
13. public Snoochy() { booch = new Boochy(this); }
14. }
15.
16. class Boochy {
17. Snoochy snooch;
18. public Boochy(Snoochy s) { snooch = s; }
19. }
And the statements:
21. public static void main(String[] args) {
22. Snoochy snoog = new Snoochy();
23. snoog = null;
24. // more code here
25. }
Which statement is true about the objects referenced by snoog, snooch, and booch immediately
after line 23 executes?
A. Only the object referenced by snooch is eligible for garbage collection.
B. The objects referenced by snooch and booch are eligible for garbage collection.
C. None of these objects are eligible for garbage collection.
D. Only the object referenced by booch is eligible for garbage collection.
E. Only the object referenced by snoog is eligible for garbage collection.
QUESTION NO: 275
Given:
25. int x = 12;
26. while (x < 10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. 12
B. 0
C. Line 29 will never be reached.
D. 10
QUESTION NO: 276
Given:
10. interface Foo { int bar(); }
11. public class Sprite {
12. public int fubar( Foo foo ) { return foo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18. }
Which code, inserted at line 15, allows the class Sprite to compile?
A. new Foo { public int bar() { return 1; }
B. new Foo() { public int bar() { return 1; }
C. Foo { public int bar() { return 1; }
D. new class Foo { public int bar() { return 1; }
QUESTION NO: 277
Given:
10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args ) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?
A. Beta b = (Beta)(Alpha)x;
B. Alpha a = x;
C. Foo f = (Alpha)x;
D. Foo f = (Delta)x;
QUESTION NO: 278
Given:
11. public static void test(String str) {
12. if (str == null | str.length() == 0) {
13. System.out.println("String is empty");
14. } else {
15. System.out.println("String is not empty");
16. }
17. }
And the invocation:
31. test(null);
What is the result?
A. "String is not empty" is printed to output.
B. Compilation fails because of an error in line 12.
C. An exception is thrown at runtime.
D. "String is empty" is printed to output.
QUESTION NO: 279
Given code in separate source files:
10. public class Foo {
11. public int a;
12. public Foo() { a = 3; }
13. public void addFive() { a += 5;}
14. } and: 20. public class Bar extends Foo {
21. public int a;
22. public Bar() { a = 8; }
23. public void addFive() { this.a += 5; }
24. } invoked with:
30. Foo foo = new Bar();
31. foo.addFive();
32. System.out.println("Value: " + foo.a);
What is the result?
A. Value: 8
B. The code runs with no output.
C. Value: 13
D. Compilation fails.
E. Value: 3
F. An exception is thrown at runtime.
QUESTION NO: 280
Click the Exhibit button.
What is the result?
A. Compilation fails because of an error in line 17.
B. go in Sente
go in Goban
C. go in Goban
go in Sente
D. go in Goban
go in Sente
QUESTION NO: 281
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. private static void process(byte[] b) {}
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes);
B. app.BitUtils.process(bytes);
C. import util.BitUtils.*; process(bytes);
D. SomeApp cannot use the process method in BitUtils.
E. BitUtils.process(bytes);
F. util.BitUtils.process(bytes);
QUESTION NO: 282
Given:
11. String test = "a1b2c3";
12. String[] tokens = test.split("\\d");
13. for(String s: tokens) System.out.print(s + " ");
What is the result?
A. a1 b2 c3
B. The code runs with no output.
C. a b c
D. An exception is thrown at runtime.
E. 1 2 3
F. a1b2c3
G. Compilation fails.
QUESTION NO: 283 DRAG DROP
Click the Task button.

QUESTION NO: 284 DRAG DROP
Click the Task button.
 

QUESTION NO: 285
Given:
1. interface DoStuff2 {
2. float getRange(int low, int high); }
3.
4. interface DoMore {
5. float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10. public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13. float getAvg(int a, int b, int c, int d); }
What is the result?
A. The file will compile without error.
B. Compilation fails. Only line 12 contains an error.
C. Compilation fails. Only line 13 contains an error.
D. Compilation fails. Only lines 7 and 13 contain errors.
E. Compilation fails. Only line 7 contains an error.
F. Compilation fails. Lines 7, 12, and 13 contain errors.
G. Compilation fails. Only lines 7 and 12 contain errors.

QUESTION NO: 286
Given a class Repetition:
1. package utils;
2.
3. public class Repetition {
4. public static String twice(String s) { return s + s; }
5. }
and given another class Demo:
1. // insert code here
2.
3. public class Demo {
4. public static void main(String[] args) {
5. System.out.println(twice("pizza"));
6. }
7. }
Which code should be inserted at line 1 of Demo.java to compile and run Demo to print
"pizzapizza"?
A. import static utils.Repetition.twice;
B. static import utils.*;
C. import utils.Repetition.twice();
D. import utils.*;
E. static import utils.Repetition.twice;
F. static import utils.Repetition.*;
G. import utils.Repetition.*;
QUESTION NO: 287
Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
Which three changes must be made to the method sum to use generics? (Choose three.)
A. replace line 13 with "for (Iterator iter : intList) {"
B. remove line 14
C. replace the method declaration with "sum(List<Integer> intList)"
D. replace the method declaration with "sum(List<int> intList)"
E. replace line 14 with "int i = iter.next();"
F. replace line 13 with "for (int i : intList) {"
QUESTION NO: 288
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 13 to:
public Sub() { super(a); }
B. Change line 13 to:
public Sub() { this(5); }
C. Change line 13 to:
public Sub() { super(5); }
D. Change line 2 to:
protected int a;
E. Change line 2 to:
public int a;
QUESTION NO: 289
Click the Exhibit button.
Which statement is true about the set variable on line 12?
A. The set variable contains all six elements from the coll collection, but the order is NOT
guaranteed to be preserved.
B. The set variable contains all six elements from the coll collection, and the order is guaranteed to
be preserved.
C. The set variable contains only three elements from the coll collection, and the order is
guaranteed to be preserved.
D. The set variable contains only three elements from the coll collection, but the order is NOT
guaranteed to be preserved.
QUESTION NO: 290
Given:
11. public class Test {
12. public static void main(String [] args) {
13. int x = 5;
14. boolean b1 = true;
15. boolean b2 = false;
16.
17. if ((x == 4) && !b2 )
18. System.out.print("1 ");
19. System.out.print("2 ");
20. if ((b2 = true) && b1 )
21. System.out.print("3 ");
22. }
23. }
What is the result?
A. 1 2
B. An exception is thrown at runtime.
C. Compilation fails.
D. 2
E. 1 2 3
F. 3
G. 2 3
QUESTION NO: 291
Given:
11. public static void main(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerException npe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
QUESTION NO: 292
Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }
6. });
7. }
8. }
What is the result?
A. Compilation fails because of an error in line 1.
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 5.
E. test
F. Compilation fails because of an error in line 4.
QUESTION NO: 293
Given:
10. public class ClassA {
11. public void count(int i) {
12. count(++i);
13. }
14. }
And:
20. ClassA a = new ClassA();
21. a.count(3);
Which exception or error should be thrown by the virtual machine?
A. IllegalArgumentException
B. ExceptionInInitializerError
C. NumberFormatException
D. NullPointerException
E. StackOverflowError
QUESTION NO: 294 DRAG DROP
Click the Task button.

QUESTION NO: 295
Given:
1. public class Blip {
2. protected int blipvert(int x) { return 0; }
3. }
4. class Vert extends Blip {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)
A. private int blipvert(long x) { return 0; }
B. protected long blipvert(int x, int y) { return 0; }
C. protected long blipvert(long x) { return 0; }
D. protected long blipvert(int x) { return 0; }
E. public int blipvert(int x) { return 0; }
F. private int blipvert(int x) { return 0; }
G. protected int blipvert(long x) { return 0; }
QUESTION NO: 296
Given:
12. public class Test {
13. public enum Dogs {collie, harrier};
14. public static void main(String [] args) {
15. Dogs myDog = Dogs.collie;
16. switch (myDog) {
17. case collie:
18. System.out.print("collie ");
19. case harrier:
20. System.out.print("harrier ");
21. }
22. }
23. }
What is the result?
A. Compilation fails.
B. collie
C. An exception is thrown at runtime.
D. harrier
E. collie harrier
QUESTION NO: 297
Given:
8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. }
12. }
Which two will produce an AssertionError? (Choose two.)
A. java -ea test file1
B. java test file1
C. java -ea:test test file1
D. java -ea test file1 file2
E. java test
F. java -ea test
QUESTION NO: 298
Given:
10. class Line {
11. public static class Point {}
12. }
13.
14. class Triangle {
15. // insert code here
16. }
Which code, inserted at line 15, creates an instance of the Point class defined in Line?
A. Line.Point p = new Line.Point();
B. Point p = new Point();
C. Line l = new Line() ; l.Point p = new l.Point();
D. The Point class cannot be instatiated at line 15.
QUESTION NO: 299
Given:
34. HashMap props = new HashMap();
35. props.put("key45", "some value");
36. props.put("key12", "some other value");
37. props.put("key39", "yet another value");
38. Set s = props.keySet();
39. // insert code here
What, inserted at line 39, will sort the keys in the props HashMap?
A. s = new SortedSet(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. Arrays.sort(s);
QUESTION NO: 300
Which two statements are true about the hashCode method? (Choose two.)
A. The hashCode method is used by the java.util.HashSet collection class to group the elements
within that set into hash buckets for swift retrieval.
B. The only important characteristic of the values returned by a hashCode method is that the
distribution of values must follow a Gaussian distribution.
C. The hashCode method is used by the java.util.SortedSet collection class to order the elements
within that set.
D. The hashCode method for a given class can be used to test for object inequality, but NOT
object equality, for that class.
E. The hashCode method for a given class can be used to test for object equality and object
inequality for that class.
QUESTION NO: 301
Given:
11. public class Commander {
12. public static void main(String[] args) {
13. String myProp = /* insert code here */
14. System.out.println(myProp);
15. }
16. }
and the command line:
java -Dprop.custom=gobstopper Commander
Which two, placed on line 13, will produce the output gobstopper? (Choose two.)
A. System.getProperties().getProperty("prop.custom");
B. System.getenv("prop.custom");
C. System.load("prop.custom");
D. System.property("prop.custom");
E. System.getProperty("prop.custom");

QUESTION NO: 302
Given:
11. rbo = new ReallyBigObject();
12. // more code here
13. rbo = null;
14. /* insert code here */
Which statement should be placed at line 14 to suggest that the virtual machine expend effort
toward recycling the memory used by the object rbo?
A. System.freeMemory();
B. Runtime.getRuntime().freeMemory();
C. Runtime.getRuntime().growHeap();
D. System.gc();
E. Runtime.gc();
QUESTION NO: 303
Which three statements are true? (Choose three.)
A. A protected method in class X can be overridden by any subclass of X.
B. A final method in class X can be abstract if and only if X is abstract.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in class X can be overridden in any subclass of X.
E. A method with the same signature as a private final method in class X can be implemented in a
subclass of X.
F. A protected method in class X can be overridden by a subclass of A only if the subclass is in the
same package as X.
G. A public static method in class X can be called by a subclass of X without explicitly referencing
the class X.
QUESTION NO: 304
Click the Exhibit button.
Which three code fragments, added individually at line 29, produce the output 100? (Choose
three.)
A. n = 100;
B. o.setY( i ); i = new Inner(); i.setX( 100 );
C. i.setX( 100 );
D. i = new Inner(); i.setX( 100 ); o.setY( i );
E. o.getY().setX( 100 );
F. i = new Inner(); i.setX( 100 );
QUESTION NO: 305
Click the Exhibit button.
What is the outcome of the code?
A. Gobstopper
Fizzylifting
B. Scrumdiddlyumptious
Fizzylifting
C. Compilation fails.
D. Gobstopper
Scrumdiddlyumptious   
E. Scrumdiddlyumptious
QUESTION NO: 306
Click the Exhibit button.
Which statement is true about the classes and interfaces in the exhibit?
A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class AImpl will fail because of an error in line 2.
D. Compilation of class C will fail because of an error in line 6.
QUESTION NO: 307
When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in
only one of the two?
A. closing the stream
B. writing to the stream
C. marking a location in the stream
D. flushing the stream
E. writing a line separator to the stream
 QUESTION NO: 308 DRAG DROP

Click the Task button.

QUESTION NO: 309
Given:
13. public static void search(List<String> list) {
14. list.clear();
15. list.add("b");
16. list.add("a");
17. list.add("c");
18. System.out.println(Collections.binarySearch(list, "a"));
19. }
What is the result of calling search with a valid List implementation?
A. 0
B. 1
C. a
D. c
E. 2
F. The result is undefined.
G. b
QUESTION NO: 310
Given:
1. public class Threads3 implements Runnable {
2. public void run() {
3. System.out.print("running");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "runningrunningrunning".
D. The code executes and prints "running".
E. The code executes and prints "runningrunning".
QUESTION NO: 311 DRAG DROP
Click the Task button.


QUESTION NO: 312
Click the Exhibit button.

Which statement is true about the two classes?
A. Compilation of class B will fail. Compilation of class A will succeed.
B. Compilation of both classes will fail.
C. Compilation of both classes will succeed.
D. Compilation of class A will fail. Compilation of class B will succeed.
QUESTION NO: 313 DRAG DROP
Click the Task button.


QUESTION NO: 314
Given:
d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
C. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
D. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
QUESTION NO: 315
Given:
11. public class Ball{
12. public enum Color { RED, GREEN, BLUE };
13. public void foo(){
14. // insert code here
15. { System.out.println(c); }
16. }
17. }
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?
A. for( Color c = Color[0]; c <= Color[2]; c++ )
B. for( Color c ; c.hasNext() ; c.next() )
C. for( Color c = Color.RED; c <= Color.BLUE; c++ )
D. for( Color c = RED; c <= BLUE; c++ )
E. for( Color c : Color.values() )
QUESTION NO: 316
Given:
11. public class Key {
12. private long id1;
13. private long id2;
14.
15. // class Key methods
16. }
A programmer is developing a class Key, that will be used as a key in a standard
java.util.HashMap.
Which two methods should be overridden to assure that Key works correctly as a key? (Choose
two.)
A. public int compareTo(Object o)
B. public boolean equals(Object o)
C. public int hashCode()
D. public boolean equals(Key k)
E. public boolean compareTo(Key k)
QUESTION NO: 317
Given:
13. public class Pass {
14. public static void main(String [] args) {
15. int x = 5;
16. Pass p = new Pass();
17. p.doStuff(x);
18. System.out.print(" main x = " + x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(" doStuff x = " + x++);
23. }
24. }
What is the result?
A. doStuff x = 6 main x = 5
B. doStuff x = 5 main x = 5
C. doStuff x = 6 main x = 6
D. Compilation fails.
E. An exception is thrown at runtime.
F. doStuff x = 5 main x = 6
QUESTION NO: 318
Given:
1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. The code executes normally and prints "sleep".
B. An exception is thrown at runtime.
C. The code executes normally, but nothing is printed.
D. Compilation fails.
QUESTION NO: 319 DRAG DROP
Click the Task button.

QUESTION NO: 320
Given:
11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13. System.out.print("pi is bigger than 3. ");
14. }
15. else {
16. System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }
What is the result?
A. Compilation fails.
B. An exception occurs at runtime.
C. pi is not bigger than 3. Have a nice day.
D. pi is bigger than 3. Have a nice day.
E. pi is bigger than 3.
QUESTION NO: 321
Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will output [1, 2]?
A. Set set = new LinkedHashSet();
B. Set set = new HashSet();
C. List set = new SortedList();
D. Set set = new SortedSet();
E. Set set = new TreeSet();
QUESTION NO: 322
Given:
foo and bar are public references available to many other threads. foo refers to a Thread and bar
is an Object. The thread foo is currently executing bar.wait().
From another thread, what provides the most reliable way to ensure that foo will stop executing
wait()?
A. Object.notify();
B. bar.notifyAll();
C. Thread.notify();
D. foo.notify();
E. foo.notifyAll();
F. bar.notify();
QUESTION NO: 323
Given:
11. public class ItemTest {
12. private final int id;
13. public ItemTest(int id) { this.id = id; }
14. public void updateId(int newId) { id = newId; }
15.
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }
What is the result?
A. The attribute id in the Item object is modified to the new value.
B. Compilation fails.
C. An exception is thrown at runtime.
D. A new Item object is created with the preferred value in the id attribute.
E. The attribute id in the Item object remains unchanged.
QUESTION NO: 324 DRAG DROP
Click the Task button.

QUESTION NO: 325
Given:
1. public class Base {
2. public static final String FOO = "foo";
3. public static void main(String[] args) {
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";}
What is the result?
A. foobarfoofoofoo
B. foofoofoobarbar
C. foobarfoobarfoo
D. foobarfoobarbar
E. foofoofoofoofoo
F. foofoofoobarfoo
G. barbarbarbarbar
QUESTION NO: 326
Given:
11. public abstract class Shape {
12. private int x;
13. private int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle extends Shape {
private int radius;
public void draw();
}
B. public abstract class Circle extends Shape {
private int radius;
}
C. public class Circle implements Shape {
private int radius;
}
D. public abstract class Circle implements Shape {
private int radius;
public void draw() { /* code here */ }
E. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
F. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
QUESTION NO: 327
Given:
11. String test = "a1b2c3";
12. String[] tokens = test.split("\\d");
13. for(String s: tokens) System.out.print(s + " ");
What is the result?
A. a1 b2 c3
B. An exception is thrown at runtime.
C. Compilation fails.
D. The code runs with no output.
E. a b c



F. a1b2c3
G. 1 2 3
QUESTION NO: 328
Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
14. public One foo() { return this; }
15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
A. public int foo() { return 3; }
B. public Two foo() { return this; }
C. public void foo() {}
D. public One foo() { return this; }
E. public Object foo() { return this; }
Answer: B,D
QUESTION NO: 329 DRAG DROP
Click the Task button.



QUESTION NO: 330
Given:
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("It is " + (j==i) + " that j==i.");
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the
command java Converter 12?
A. Compilation fails because of an error in line 13.
B. An exception is thrown at runtime.
C. It is true that j==i.
D. It is false that j==i.
QUESTION NO: 331
Given this method in a class:
21. public String toString() {
22. StringBuffer buffer = new StringBuffer();
23. buffer.append('<');
24. buffer.append(this.name);
25. buffer.append('>');
26. return buffer.toString();
27. }
Which statement is true?
A. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
B. This code is NOT thread-safe.
C. The programmer can replace StringBuffer with StringBuilder with no other changes.
D. This code will perform well and converting the code to use StringBuilder will not enhance the
performance.
QUESTION NO: 332
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. BitUtils.process(bytes);
B. process(bytes);
C. util.BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. import util.BitUtils.*; process(bytes);

QUESTION NO: 333
Click the Exhibit button.
Which three statements are true? (Choose three.)
A. If lines 24, 25 and 26 were removed, compilation would fail.
B. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
C. The code compiles and the output is 2.
D. If lines 16, 17 and 18 were removed, compilation would fail.
E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
F. Compilation fails.
QUESTION NO: 334
Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. A call to notify() or notifyAll() from another thread may cause this method to complete normally.
E. This code may throw an InterruptedException.
F. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".

QUESTION NO: 335
Given:
84. try {
85. ResourceConnection con = resourceFactory.getConnection();
86. Results r = con.query("GET INFO FROM CUSTOMER");
87. info = r.getData();
88. con.close();
89. } catch (ResourceException re) {
90. errorLog.write(re.getMessage());
91. }
92. return info;
Which statement is true if a ResourceException is thrown on line 86?
A. Line 92 will not execute.
B. The enclosing method will throw an exception to its caller.
C. The connection will not be retrieved in line 85.
D. The resource connection will not be closed on line 88.
QUESTION NO: 336
A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure
where the old one is installed. Bob is currently able to run a Java chess program starting from his
home directory /home/bob using the command:
java -classpath /test:/home/bob/downloads/*.jar games.Chess
Bob's CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar
What is a possible location for the Chess.class file?
A. /test/Chess.class
B. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
C. /home/bob/games/Chess.class
D. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)
E. /test/games/Chess.class
F. /usr/lib/games/Chess.class
G. /home/bob/Chess.class
QUESTION NO: 337 DRAG DROP
Click the Task button.


QUESTION NO: 338
Given:
10: public class Hello {
11: String title;
12: int value;
13: public Hello() {
14: title += " World";
15: }
16: public Hello(int value) {
17: this.value = value;
18: title = "Hello";
19: Hello();
20: }
21: }
and:
30: Hello c = new Hello(5);
31: System.out.println(c.title);
What is the result?
A. The code runs with no output.
B. Compilation fails.
C. Hello
D. Hello World
E. An exception is thrown at runtime.
F. Hello World 5
QUESTION NO: 339
Click the Exhibit button.
Which two statements are true if this class is compiled and run? (Choose two.)
A. The code may run with output "A A A B C A B C C ", then exit.
B. The code may run with output "A B C A A B C A B C ", then exit.
C. The code may run with no output, without exiting.
D. The code may run with output "A B C A B C A B C ", then exit.
E. An exception may be thrown at runtime.
F. The code may run with no output, exiting normally.

G. The code may run with output "A B A B C C ", then exit.
QUESTION NO: 340
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }
When the doSomething method is called, after which line does the Object created in line 5
become available for garbage collection?
A. Line 8
B. Line 5
C. Line 6
D. Line 9
E. Line 10
F. Line 7
QUESTION NO: 341 DRAG DROP
Click the Task button.




QUESTION NO: 342
Given:
33. try {
34. // some code here
35. } catch (NullPointerException e1) {
36. System.out.print("a");
37. } catch (RuntimeException e2) {
38. System.out.print("b");
39. } finally {
40. System.out.print("c");
41. }
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. abc
D. bc
E. ac
F. ab
QUESTION NO: 343 DRAG DROP
Click the Task button.

QUESTION NO: 344
Given classes defined in two different files:
1. package packageA;
2. public class Message {
3. String getText() { return "text"; }
4. }
and:
1. package packageB;
2. public class XMLMessage extends packageA.Message {
3. String getText() { return "<msg>text</msg>";}
4. public static void main(String[] args) {
5. System.out.println(new XMLMessage().getText());
6. }
7. }
What is the result of executing XMLMessage.main?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 2 of XMLMessage.
C. text
D. Compilation fails because of an error in line 3 of XMLMessage.
QUESTION NO: 345
Given:
1. class Pizza {
2. java.util.ArrayList toppings;
3. public final void addTopping(String topping) {
4. toppings.add(topping);
5. }
6. }
7. public class PepperoniPizza extends Pizza {
8. public void addTopping(String topping) {
9. System.out.println("Cannot add Toppings");
10. }
11. public static void main(String[] args) {
12. Pizza pizza = new PepperoniPizza();
13. pizza.addTopping("Mushrooms");
14. }
15. }
What is the result?
A. Cannot add Toppings
B. Compilation fails.
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.
QUESTION NO: 346
Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
14. public One foo() { return this; }
15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
A. public Two foo() { return this; }
B. public Object foo() { return this; }
C. public int foo() { return 3; }
D. public One foo() { return this; }
E. public void foo() {}
Answer: A,D
QUESTION NO: 347
Given:
10. int x = 0;
11. int y = 10;
12. do {
13. y--;
14. ++x;
15. } while (x < 5);
16. System.out.print(x + "," + y);
What is the result?
A. 6,6
B. 5,5
C. 6,5
D. 5,6

 236

Answer: B
QUESTION NO: 348 DRAG DROP
Click the Task button.
Answer:

 237

QUESTION NO: 349
Given:
1. public class Score implements Comparable<Score> {
2. private int wins, losses;
3. public Score(int w, int l) { wins = w; losses = l; }
4. public int getWins() { return wins; }
5. public int getLosses() { return losses; }
6. public String toString() {
7. return "<" + wins + "," + losses + ">";
8. }
9. // insert code here
10. }
Which method will complete this class?
A. public int compare(Object o1,Object o2){/*more code here*/}
B. public int compare(Score s1,Score s2){/*more code here*/}
C. public int compareTo(Object o){/*more code here*/}
D. public int compareTo(Score other){/*more code here*/}
Answer: D
QUESTION NO: 350
Given:
11. class Snoochy {
12. Boochy booch;
13. public Snoochy() { booch = new Boochy(this); }
14. }
15.
16. class Boochy {
17. Snoochy snooch;
18. public Boochy(Snoochy s) { snooch = s; }
19. }
And the statements:
21. public static void main(String[] args) {
22. Snoochy snoog = new Snoochy();

 238

23. snoog = null;
24. // more code here
25. }
Which statement is true about the objects referenced by snoog, snooch, and booch immediately
after line 23 executes?
A. Only the object referenced by snooch is eligible for garbage collection.
B. Only the object referenced by snoog is eligible for garbage collection.
C. None of these objects are eligible for garbage collection.
D. The objects referenced by snooch and booch are eligible for garbage collection.
E. Only the object referenced by booch is eligible for garbage collection.
Answer: D
QUESTION NO: 351
Given:
12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)
A. The value of b is 2.
B. The value of a is 3.14.
C. The value of a is 3.1415.
D. The value of a is 3.141.
E. The value of b is 2.0000.
F. The value of a is 3.1416.
G. The value of b is 2.00.
Answer: F,G
QUESTION NO: 352 DRAG DROP
Click the Task button.

 239

Answer:
QUESTION NO: 353
Given:
11. public class Person {
12. private String name;
13. public Person(String name) {

 240

14. this.name = name;
15. }
16. public boolean equals(Object o) {
17. if ( ! o instanceof Person ) return false;
18. Person p = (Person) o;
19. return p.name.equals(this.name);
20. }
21. }
Which statement is true?
A. A HashSet could contain multiple Person objects with the same name.
B. If a HashSet contains more than one Person object with name="Fred", then removing another
Person, also with name="Fred", will remove them all.
C. All Person objects will have the same hash code because the hashCode method is not
overridden.
D. Compilation fails because the hashCode method is not overridden.
Answer: A
QUESTION NO: 354 DRAG DROP
Click the Task button.
Answer:

 241

QUESTION NO: 355
Given
10. class Foo {
11. static void alpha() { /* more code here */ }
12. void beta() { /* more code here */ }
13. }
Which two statements are true? (Choose two.)
A. Method alpha() can directly call method beta().
B. Method beta() can directly call method alpha().
C. Foo.beta() is a valid invocation of beta().
D. Foo.alpha() is a valid invocation of alpha().
Answer: B,D
QUESTION NO: 356
Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x = 1; x < yahoo.length; x++) {

 242

16. System.out.print(yahoo[x] + " ");
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A. a b c
B. a b
C. Compilation fails.
D. b c
E. An exception is thrown at runtime.
Answer: D
QUESTION NO: 357
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }
When the doSomething method is called, after which line does the Object created in line 5
become available for garbage collection?
A. Line 5
B. Line 7
C. Line 6

 243

D. Line 10
E. Line 9
F. Line 8
Answer: F
QUESTION NO: 358
Given:
1. public class MyLogger {
2. private StringBuilder logger = new StringBuuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7. }
The programmer must guarantee that a single MyLogger object works properly for a multithreaded
system.
How must this code be changed to be thread-safe?
A. synchronize the log method
B. replace StringBuilder with StringBuffer
C. No change is necessary, the current MyLogger code is already thread-safe.
D. replace StringBuilder with just a String object and use the string concatenation (+=) within the
log method
Answer: A
QUESTION NO: 359
Given:
23. int z = 5;
24.
25. public void stuff1(int x) {
26. assert (x > 0);
27. switch(x) {
28. case 2: x = 3;
29. default: assert false; } }

 244

30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4()); }
34.
35. private boolean stuff4() { z = 6; return false; }
Which statement is true?
A. Only the assert statement on line 31 is used appropriately.
B. The assert statements on lines 26, 29, and 31 are used appropriately.
C. The assert statements on lines 29 and 31 are used appropriately.
D. The assert statements on lines 29 and 33 are used appropriately.
E. The assert statements on lines 26 and 29 are used appropriately.
F. All of the assert statements are used appropriately.
G. The assert statements on lines 29, 31, and 33 are used appropriately.
Answer: C
QUESTION NO: 360
Click the Exhibit button.
Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?

 245

A. this = s.defaultReadObject();
B. y = s.readInt(); x = s.readInt();
C. x = s.readInt(); y = s.readInt();
D. s.defaultReadObject();
Answer: C
QUESTION NO: 361
Given:
11. public void testIfA() {
12. if (testIfB("True")) {
13. System.out.println("True");
14. } else {
15. System.out.println("Not true");
16. }
17. }
18. public Boolean testIfB(String str) {
19. return Boolean.valueOf(str);
20. }
What is the result when method testIfA is invoked?
A. Compilation fails because of an error at line 12.
B. True
C. Not true
D. Compilation fails because of an error at line 19.
E. An exception is thrown at runtime.
Answer: B
QUESTION NO: 362
A programmer has an algorithm that requires a java.util.List that provides an efficient
implementation of add(0, object), but does NOT need to support quick random access. What
supports these requirements?
A. java.util.LinkedList
B. java.util.Queue
C. java.util.ArrayList

 246

D. java.util.LinearList
Answer: A

 247

No comments:

Post a Comment