Friday, August 8, 2008

SCJP 1.5 Dump 01




Sun Certified Programmer for the Java 2 Platform, Standard

Question 1
Given:
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Which three are valid on line 12? (Choose three.)
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
Answer: ABD

Question 2
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. foreach(x) System.out.println(z);
B. for(int z : x) System.out.println(z);
C. while( x.hasNext()) System.out.println( x.next());
D. for( int i=0; i< x ="5;" b1 =" true;" b2 =" false;" x="=" b2 =" true)" x =" new" a =" x;" f=" (Delta)x;" f=" (Alpha)x;" b =" (Beta)(Alpha)x;" loc =" Locale.getLocale();" loc =" Locale.getDefault();" bc =" Locale.getLocale();" loc =" Locale.getDefault();" cardld =" cardlD;" ownername =" ownerName;" limit =" limit;"> }
F. class Man { private BestFriend }
Answer: D

Question 10
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 is true?
A. The time to find the value from HashMap with a Person key depends
on the size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for
all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first
Person object to be removed as a duplicate.
D. 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.
Answer: A

Question 11
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; isetAnchor(10,10);
s->draw();
E. Circle c = new Circle();
c.Shape.setAnchor(10,10);
c.Shape.draw();
Answer: C

Question 22
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. double getSalesAmount() { return 1230.45; }
B. public double getSalesAmount() { return 1230.45; }
C. private double getSalesAmount() { return 1230.45; }
D. protected double getSalesAmount() { return 1230.45; }
Answer: BD

Question 23
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 load() { /*do something*/ }
}
B. public class Employee implements Info extends Data {
public void load() { /*do something*/ }
}
C. public class Employee extends Info implements Data {
public void load() { /*do something */ }
public void Info.load() { /*do something*/ }
}
D. public class Employee implements Info extends Data {
public void Data.load() { /*d something */ }
public void load() { /*do something */ }
}
E. public class Employee implements Info extends Data {
public void load() { /*do something */ }
public void Info.load(){ /*do something*/ }
}
F. public class Employee extends Info implements Data{
public void Data.load() { /*do something*/ }
public void Info.load() { /*do something*/ }
}
Answer: A

Question 24
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 implements Shape {
private int radius;
}
B. public abstract class Circle extends Shape {
private int radius;
}
C. public class Circle extends Shape {
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
E. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
}
F. public abstract class Circle implements Shape {
private int radius;
public void draw() { / code here */ }
}
Answer: BE

Question 25
Which two classes correctly implement both the java.lang.Runnable
and the java.lang.Clonable interfaces? (Choose two.)
A. public class Session
implements Runnable, Clonable {
public void run();
public Object clone();
}
B. public class Session
extends Runnable, Clonable {
public void run() { / do something */ }
public Object clone() { / make a copy */ }
}
C. public class Session
implements Runnable, Clonable {
public void run() { / do something */ }
public Object clone() { /* make a copy */ }
}
D. public abstract class Session
implements Runnable, Clonable {
public void run() { / do something */ }
public Object clone() { /*make a copy */ }
}
E. public class Session
implements Runnable, implements Clonable {
public void run() { / do something */ }
public Object clone() { / make a copy */ }
}
Answer: CD

Question26
Click the Exhibit button.
1. public class GoTest {
2. public static void main(String[] args) {
3. Sente a = new Sente(); a.go();
4. Goban b = new Goban(); b.go();
5. Stone c = new Stone(); c.go();
6. }
7. }
8.
9. class Sente implements Go {
10. public void go() { System.out.println(”go in Sente.”); }
11. }
12.
13. class Goban extends Sente {
14. public void go() { System.out.println(”go in Goban”); }
15. }
16.
17. class Stone extends Goban implements Go { }
18.
19. interface Go { public void go(); }
What is the result?
A. go in Goban
go in Sente
go in Sente
B. go in Sente
go in Sente
go in Goban
C. go in Sente
go in Goban
go in Goban
D. go in Goban
go in Goban
go in Sente
E. Compilation fails because of an error in line 17.
Answer: C

Question 27
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. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at
runtime.
Answer: B

Question 28
Click the Exhibit button.
1. public class Test {
2. int x= 12;
3. public void method(int x) {
4. x+=x;
5. System.out.println(x);
6. }
7. }
Given:
34. Test t = new Test();
35. t.method(5);
What is the output from line 5 of the Test class?
A. 5
B. 10
C. 12
D. 17
E. 24
Answer: B

Question 28
Given:
55. int []x= {1, 2,3,4, 5};
56.int y[] =x;
57. System.out.println(y[2]);
Which is true?
A. Line 57 will print the value 2.
B. Line 57 will print the value 3.
C. Compilation will fail because of an error in line 55.
D. Compilation will fail because of an error in line 56.
Answer: B

Question 30
Given:
35. String #name = “Jane Doe”;
36.int$age=24;
37. Double_height = 123.5;
38. double~temp = 37.5;
Which two are true? (Choose two.)
A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.
Answer: AD

Question 31
Which two code fragments correctly create and initialize a static array
of int elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2] { 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: AB

Question 32
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.values())
B. for( Color c = RED; c <= BLUE; c++) C. for( Color c; c.hasNext() ; c.next()) D. for( Color c = Color[0]; c <= Color[2]; c++) E. for( Color c = Color.RED; c <= Color.BLUE; c++) Answer: A Question 33 Given: 10. public class Fabric 11. public enum Color { 12. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 13. private final int rgb; 14. Color( int rgb) { this.rgb = rgb; } 15. public int getRGB() { return rgb; } 16. }; 17. public static void main( String[] argv) { 18. // insert code here 19. } 20. } Which two code fragments, inserted independently at line 18, allow the Fabric class to compile? (Choose two.) A. Color skyColor = BLUE; B. Color treeColor = Color.GREEN; C. Color purple = new Color( 0xff00ff); D. if( RED.getRGB() < purple =" Color.BLUE" title =" t;" obj ="new" somearray =" (int[])obj;" counter =" 0;" a1 ="new" a2 ="new" a3 ="new" a="new" p =" new" p =" new" 1 =" new" p =" new" p =" Line.getPoint();" p =" Line.getPoint();" p =" (new" p =" (new" name =" “No" name =" nm;" empld =" “0000”;" empid =" id;" e =" new" name =" name;" bootch =" bootch;" snootch =" snootch;" b =" new" mydog =" Dogs.shepherd;" mydog =" Dogs.collie;" i =" uew" str =" “null’;" str ="=" pi =" new">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. pi is bigger than 3.
C. An exception occurs at runtime.
D. pi is bigger than 3. Have a nice day.
E. pi is not bigger than 3. Have a nice day.
Answer: A

Question 58
Given:
10.int x=0;
11.int y 10;
12. do {
l3. y--;
14. ++x;
15. } while (x < intx="12;" x=" 10;" list =" new" sorted =" new" i="0;i<=">6) break;
14. }
15. System.out.println(i);
16. }
What is the result?
A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E

Question 64
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 test
B. java -ea test
C. java test file1
D. java -ea test file1
E. java -ea test file1 file2
F. java -ea:test test file1
Answer: BE

Question 65
Given:
12. public class AssertStuff {
13.
14. public static void main(String [] args) {
15. int x= 5;
16. int y= 7;
17.
18. assert (x> y): “stuff”;
19. System.out.println(”passed”);
20. }
21. }
And these command line invocations:
java AssertStuff
java -ea AssertStuff
What is the result?
A. passed
stuff
B. stuff
passed
C. passed
An AssertionError is thrown with the word “stuff” added to the stack
trace.
D. passed
An AssertionError is thrown without the word “stuff” added to the
stack trace.
E. passed
An AssertionException is thrown with the word “stuff” added to the
stack trace.
F. passed
An AssertionException is thrown without the word “stuff” added to the
stack trace.
Answer: C

Question 66
Click the Exhibit button.
1. public class Test {
2.
3. public static void main(String [] args) {
4. boolean assert = true;
5. if(assert) {
6. System.out.println(”assert is true”);
7. }
8. }
9.
10. }
Given:
javac -source 1.3 Test.java
What is the result?
A. Compilation fails.
B. Compilation succeeds with errors.
C. Compilation succeeds with warnings.
D. Compilation succeeds without warnings or errors.
Answer: C

Question 67
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 < z =" 6;" name ="=" a="new" a="new" con =" resourceFactory.getConnection();" r =" con.query(”GET" info =" r.getData();" b="new" c="new" a="new" b="new" a="new" args="null;" x="null;" value ="=" value ="=" value ="=" classb =" new" value =" “ClassB”;" a =" new" a =" new" x="i+y;" str =" “420”;" i =" args[0];" j =" 12;" j="=" j="=" j="=" j="=" buffer =" new">’);
26. return buffer.toString();
27. }
Which is true?
A. This code is NOT thread-safe.
B. The programmer can replace StringBuffer with StringBuilder with no
other changes.
C. This code will perform well and converting the code to use
StringBuilder will not enhance the performance.
D. This code will perform poorly. For better performance, the code
should be rewritten: return “<“+ this.name + “>”;
Answer: B

Question 90
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 multi-threaded 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 91
Given:
11. public String makinStrings() {
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Answer: C

Question 92
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. String s = “123456789”;
s = (s-”123”).replace(1,3,”24”) - “89”;
B. StringBuffer s = new StringBuffer(”123456789”);
s.delete(0,3).replace( 1,3, “24”).delete(4,6);
C. StringBuffer s = new StringBuffer(”123456789”);
s.substring(3,6).delete( 1 ,3).insert( 1, “24”);
D. StringBuilder s = new StringBuilder(”123456789”);
s.substring(3,6).delete( 1 ,2).insert( 1, “24”);
E. StringBuilder s = new StringBuilder(”123456789”);
s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);
Answer: BE

Question 93
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. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: A

Question 94
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. SHORT LONG
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: C

Question 95
Given:
10. class MakeFile {
11. public static void main(String[] args) {
12. try {
13. File directory = new File(”d”);
14. File file = new File(directory,”f”);
15. if(!file.exists()) {
16. file.createNewFile();
17. }
18. } catch (IOException e) {
19. e.printStackTrace
20. }
21. }
22. }
The current directory does NOT contain a directory named “d.”
Which three are true? (Choose three.)
A. Line 16 is never executed.
B. An exception is thrown at runtime.
C. Line 13 creates a File object named “d.”
D. Line 14 creates a File object named “f.’
E. Line 13 creates a directory named “d” in the file system.
F. Line 16 creates a directory named “d” and a file ‘f’ within it in the
file system.
G. Line 14 creates a file named ‘f’ inside of the directory named “d” in
the file system.
Answer: BCD

Question 96
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. flushing the stream
C. writing to the stream
D. marking a location in the stream
E. writing a line separator to the stream
Answer: E

Question 97
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 exception is thrown at runtime.
C. An instance of Forest is serialized.
D. A instance of Forest and an instance of Tree are both serialized.
Answer: B

Question 98
Click the Exhibit button.
1. import java.io.*;
2. public class Foo implements Serializable {
3. public int x, y;
4. public Foo( int x, int y) { this.x = x; this.y = y; }
5.
6. private void writeObject( ObjectOutputStream s)
7. throws IOException {
8. s.writeInt(x); s.writeInt(y)
9. }
10.
11. private void readObject( ObjectInputStream s)
12. throws IOException, ClassNotFoundException {
13.
14. // insert code here
15.
16. }
17. }
Which code, inserted at line 14, will allow this class to correctly
serialize and deserialize?
A. s.defaultReadObject();
B. this = s.defaultReadObject();
C. y = s.readInt(); x = s.readInt();
D. x = s.readInt(); y = s.readInt();
Answer: D

Question 99
Which three concerning the use of the java.io.Serializable interface are
true? (Choose three.)
A. Objects from classes that use aggregation cannot be serialized.
B. Art object serialized on one JVM can be successfully deserialized on
a different JVM.
C. The values in fields with the volatile modifier will NOT survive
serialization and deserialization.
D. The values in fields with the transient modifier will NOT survive
serialization and deserialization.
E. It is legal to serialize an object of a type that has a supertype that
does NOT implement java.io.Serializable.
Answer: BDE

Question 100
Assuming that the serializeBanana() and the deserializeBanana()
methods will correctly use Java serialization and given:
13. import java.io.*;
14. class Food implemertts Serializable {int good = 3;}
15. class Fruit externds 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 400
B. restore 403
C. restore 453
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: C

Question 101
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. Compilation fails.
B. 1 restored 42
C. 12 restored 42
D. 121 restored 42
E. 1212 restored 42
F. An exception is thrown at runtime.
Answer: D

Question 102
Given:
10. public class Foo implements java.io.Serializable {
11. private int x;
12. public int getX() { return x; }
12.publicFoo(int x){this.x=x; }
13. private void writeObject( ObjectOutputStream s)
14. throws IOException {
15. // insert code here
16. }
17. }
Which code fragment, inserted at line 15, will allow Foo objects to be
correctly serialized and deserialized?
A. s.writeInt(x);
B. s.serialize(x);
C. s.writeObject(x);
D. s.defaultWriteObject();
Answer: D

Question 103
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 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 b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.
Answer: CF

Question 104
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 3 14.159,26?
A. b = nf.parse( input);
B. b = nf.format( input);
C. b = nf.equals( input);
D. b = nf.parseObject( input);
Answer: B

Question 105
Given:
14. DateFormat df;
15. Date date = new Date();
16. //insert code here
17. String s = df.format( date);
Which two, inserted independently at line 16, allow the code to
compile? (Choose two.)
A. df= new DateFormat();
B. df= Date.getFormatter();
C. df= date.getFormatter();
D. df= date.getDateFormatter();
E. df= Date.getDateFormatter();
F. df= DateFormat.getInstance();
G. df = DateFormat.getDateInstance();
Answer: FG

Question 106
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. The value of s is 14-dic-2004.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D

Question 107
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
Which will create the appropriate DateFormat object and add 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.getDateJnstance();
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
C. 35. DateFormat df= DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D. 35. DateFormat df= DateFormat.getDateJnstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());
Answer: B

Question 108
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. d = df.getDate(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) { };
Answer: C

Question 109
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. 0
B. 1
C. 4
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D

Question 110
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. a b c
B. 1 2 3
C. a1b2c3
D. a1 b2 c3
E. Compilation fails.
F. The code runs with no output.
G. An exception is thrown at runtime.
Answer: A

Question 111
Given:
11. String test = “Test A. Test B. Test C.”;
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression inserted at line 12 will correctly split test into
“Test A,” “Test B,” and “Test C”?
A. String regex = “”;
B. String regex = “ “;
C. String regex = “.*“.
D. String regex = “\\s”
E. String regex = “\\.\\s*”;
F. String regex = “\\w[ \.] +“;
Answer: E

Question 112
Given:
12. System.out.format(”Pi is approximately %d.”, Math.PI);
What is the result?
A. Compilation fails.
B. Pi is approximately 3.
C. Pi is approximately 3.141593.
D. An exception is thrown at runtime.
Answer: D

Question 113
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. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.
Answer: D

Question 114
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 start() { doStuff(); }
} .run();
D. new Thread() {
public void run() { doStuff(); }
} .start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
} ).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
Answer: DF

Question 115
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 “running”.
D. The code executes and prints “runningrunning”.
E. The code executes and prints “runningrunningrunning”.
Answer: E

Question 116
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.
Answer: B
Question 117

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 and prints “bar”.
D. The code executes normally, but nothing prints.
Answer: C

Question 118
Given:
11. Runnable r = new Runnable() {
12. public void run() {
13. System.out.print(”Cat”);
14. }
15. };
16. Threadt=new Thread(r) {
17. public void run() {
18. System.out.print(”Dog”);
19. }
20. };
21. t.start();
What is the result?
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: B

Question 119
Click the Exhibit button.
Given:
10. public class Starter extends Thread {
11. private int x= 2;
12. public static void main(String[] args) throws Exception {
13. new Starter().makeItSo();
14. }
15. public Starter() {
16. x=5;
17. start();
18. }
19. public void makeItSo() throws Exception {
20. join();
21. x=x- 1;
22. System.out.println(x);
23. }
24. public void run() { x *= 2; }
25. }
What is the output if the main() method is rum?
A. 4
B. 5
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown at runtime.
G. It is impossible to determine for certain.
Answer: D

Question 120
Given:
1. public class Threads2 implements Runnable {
2.
3. public void nun() {
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. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
Answer: DE

Question 121
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. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints “sleep”.
D. The code executes normally, but nothing is printed.
Answer: C
Question 122
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< resource =" new" t2 =" new" name =" name;" count =" 0;" intx="0;" current =" 0;" int="i=" current =" x;" x =" current" r1 =" new" names =" new" i =" 0;" class="" implementation="" name="“hello”;" x="current" p0="new" t="new" z="new" y="new" vehicle="new" car="new" racer="new" cat="(Cat)animal;" animal="new" p2="new" p1="new" p4="new" p3="new" it="new" i="0;i<2;i++)" obj="new" computations="new" r2="new" num="num;" r1="6," current="x;" o="new" sl="new" rl="“" iscomplete="true;">list = new java.util.ArrayList();
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. The code runs with no output.
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 23.
F. Compilation fails because of an error in line 25.
Answer: F

Question 143
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 is true?
A. Compilation will succeed if A extends B.
B. Compilation will succeed if B extends A.
C. Compilation will always fail because of an error in line 7.
D. Compilation will always fail because of an error in line 8.
Answer: B

Question 144
Given:
1. interface A { public void aMethod(); }
2. interface B { public void bMethod(); }
3. interface C extends A,B { public void cMethod(); }
4. class D implements B {
5. public void bMethod() { }
6. }
7. class E extends D implements C {
8. public void aMethod() { }
9. public void bMethod() { }
10. public void cMethod() { }
11. }
What is the result?
A. Compilation fails because of an error in line 3.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 9.
D. If you define D e = new E(), then e.bMethod() invokes the version
of bMethod() defined in Line 5.
E. If you define D e = (D)(new E()), then e.bMethod() invokes the
version of bMethod() defined in Line 5.
F. If you define D e = (D)(new E()), then e.bMethod() invokes the
version of bMethod() defined in Line 9.
Answer: F

Question 145
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(C c) { c.getValue(); }
B. public void add(B b) { b.getValue(); }
C. public void add(A a) { a.getValue(); }
D. public void add(A a, B b) { a.getValue(); }
E. public void add(C c1, C c2) { c1.getValue(); }
Answer: BCD
Question 146
Given:
1. class ClassA {
2. public int numberOfinstances;
3. protected ClassA(int numberOfinstances) {
4. this.numberOflnstances = numberOfinstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfinstances) {
9. super(numberOflnstances);
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOflnstances);
14. }
15. }
Which is true?
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
Answer: A

Question 147
147. 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. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
Answer: D

Question 148
Which three statements are true? (Choose three.)
A. A final method in class X can be abstract if and only if X is abstract.
B. A protected method in class X can be overridden by any subclass of
X.
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 public static method in class X can be called by a subclass of X
without explicitly referencing the class 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 protected method in class X can be overridden by a subclass of A
only if the subclass is in the same package as X.
Answer: BEF

Question 149
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. Compilation fails.
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.
Answer: A

Question 150
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:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }
Answer: CD
Question 151
Click the Exhibit button.
1. public class SimpleCalc {
2. public int value;
3. public void calculate() { value += 7; }
4. }
And:
1. public class MultiCalc extends SimpleCalc {
2. public void calculate() { value -= 3; }
3. public void calculate(int multiplier) {
4. calculate();
5. super.calculate();
6. value *=multiplier;
7. }
8. public static void main(String[] args) {
9. MultiCalc calculator = new MultiCalc();
10. calculator.calculate(2);
11. System.out.println(”Value is: “+ calculator.value);
12. }
13. }
What is the result?
A. Value is: 8
B. Compilation fails.
C. Value is: 12
D. Value is: -12
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A

Question 152
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. Hello
B. Hello World
C. Compilation fails.
D. Hello World 5
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: C
Question 153

Click the Exhibit button.
1. public class Car {
2. private int wheelCount;
3. private String vin;
4. public Car(String vin) {
5. this.vin = vin;
6. this.wheelCount = 4;
7. }
8. public String drive() {
9. return “zoom-zoom”;
10. }
11. public String getInfo() {
12. return “VIN: “+ vin + “wheels: “+ wheelCount;
13. }
14. }
And:
1. public class MeGo extends Car {
2. public MeGo(String vin) {
3. this.wheelCount = 3;
4. }
5. }
What two must the programmer do to correct the compilation errors?
(Choose two.)
A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
F. change line 3 in the MeGo class to super.wheelCount = 3;
Answer: DE

Question 154
Click the Exhibit button.
1. public class Employee {
2. String name;
3. double baseSalary;
4. Employee(String name, double baseSalary) {
5. this.name = name;
6. this.baseSalary = baseSalary;
7. }
8. }
And:
1. public class Salesperson extends Employee {
2. double commission;
3. public Salesperson(String name, double baseSalary,
4. double commission) {
5. // insert code here
6. }
7. }
Which code, inserted at line 7, completes the Salesperson constructor?
A. this.commission = commission;
B. superb();
commission = commission;
C. this.commission = commission;
superb();
D. super(name, baseSalary);
this.commission = commission;
E. super();
this.commission = commission;
F. this.commission = commission;
super(name, baseSalary);
Answer: D

Question 155
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. public int blipvert(int x) { return 0; }
B. private int blipvert(int x) { return 0; }
C. private int blipvert(long x) { return 0; }
D. protected long blipvert(int x) { return 0; }
E. protected int blipvert(long x) { return 0; }
F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }
Answer: ACEFG

Question156
Given:
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: 3
B. Value: 8
C. Value: 13
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A
Question 157
Given:
10. public class SuperCaic {
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. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 22.
F. Compilation fails because of an error in line 31.
Answer: E

Question 158
Given:
1. public class Team extends java.util.LinkedList {
2. public void addPlayer(Player p) {
3. add(p);
4. }
5. public void compete(Team opponent) { /* more code here */ }
6. }
7. class Player { /* more code here */ }
Which two are true? (Choose two.)
A. This code will compile.
B. This code demonstrates proper design of an is-a relationship.
C. This code demonstrates proper design of a has-a relationship.
D. A Java programmer using the Team class could remove Player
objects from a Team object.
Answer: AD

Question 159
Which four are true? (Choose four.)
A. Has-a relationships should never be encapsulated.
B. Has-a relationships should be implemented using inheritance.
C. Has-a relationships can be implemented using instance variables.
D. Is-a relationships can be implemented using the extends keyword.
E. Is-a relationships can be implemented using the implements
keyword.
F. The relationship between Movie and Actress is an example of an is-a
relationship.
G. An array or a collection can be used to implement a one-to-many
has-a relationship.
Answer: CDEG

Question 160
Which two are true about has-a and is-a relationships? (Choose two.)
A. Inheritance represents an is-a relationship.
B. Inheritance represents a has-a relationship.
C. Interfaces must be used when creating a has-a relationship.
D. Instance variables can be used when creating a has-a relationship.
Answer: AD

Question 161
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. Cat is-a Animal
B. Cat is-a Jumper
C. Dog is-a Animal
D. Dog is-a Jumper
E. Cat has-a Animal
F. Beagle has-a Tail
G. Beagle has-a Jumper
Answer: BCF

Question 162
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(l));
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 TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();
Answer: A

Question 163
Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue pq = new PriorityQueue();
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. carrot:apple
C. apple:banana
D. banana:apple
E. carrot:carrot
F. carrot:banana
Answer: C

Question 164
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. HashSeths = new HashSet ();
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. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D

Question 165
Click the Exhibit button.
1. import java.util.*;
2. public class TestSet {
3. enum Example { ONE, TWO, THREE }
4. public static void main(String[] args) {
5. Collection coll = new ArrayList();
6. coll.add(Example.THREE);
7. coll.add(Example.THREE);
8. coll.add(Example.THREE);
9. coll.add(Example.TWO);
10. coll.add(Example.TWO);
11. coll.add(Example.ONE);
12. Set set = new HashSet(coll);
13. }
14. }
Which statement is true about the set variable on line 12?
A. The set variable contains all six elements from the coll collection,
and the order is guaranteed to be preserved.
B. The set variable contains only three elements from the coll
collection, and the order is guaranteed to be preserved.
C. The set variable contains all six elements from the coil collection,
but the order is NOT guaranteed to be preserved.
D. The set variable contains only three elements from the coil
collection, but the order is NOT guaranteed to be preserved.
Answer: D

Question 166
Given:
1. public class Score implements Comparable {
2. private int wins, losses;
3. public Score(int w, int 1) { wins = w; losses = 1; }
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 compareTo(Object o) {/*mode code here*/}
B. public int compareTo(Score other) {/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/}
D. public int compare(Object o1,Object o2){/*more code here*/}
Answer: B

Question 167
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.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
Question 168
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() + age * 7;
C. return name.hashCode() + comment.hashCode() /2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;
Answer: B

Question 169
Given:
11. public class Key {
12. private long id1;
13. private long 1d2;
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 hashCode()
B. public boolean equals(Key k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k)
Answer: AD

Question 170
Given:
11. public class Person {
12. private name;
13. public Person(String name) {
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 is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same
name.
C. All Person objects will have the same hash code because the
hashCode method is not overridden.
D. If a HashSet contains more than one Person object with
name=”Fred”, then removing another Person, also with name=”Fred”,
will remove them all.
Answer: B

Question 171
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 is true?
A. The equals method does NOT properly override the Object.equals
method.
B. Compilation fails because the private attribute p.name cannot be
accessed in line 5.
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.
Answer: A

Question 172
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.SortedSet collection
class to order the elements within that set.
C. The hashCode method for a given class can be used to test for
object inequality, but NOT object equality, for that class.
D. The only important characteristic of the values returned by a
hashCode method is that the distribution of values must follow a
Gaussian distribution.
E. 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.
Answer: CE
Question 173
Given:
enum Example { ONE, TWO, THREE }
Which is true?
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both
guaranteed to be true.
B. The expression (ONE < i =" i;" i ="="> set = new HashSet();
11. KeyMaster k1 = new KeyMaster(1);
12. KeyMaster k2 = new KeyMaster(2);
13. set.add(k1); set.add(k1);
14. set.add(k2); set.add(k2);
15. System.out.print(set.size() + “:”);
16. k2.i = 1;
17. System.out.print(set.size() + “:”);
18. set.remove(k1);
19. System.out.print(set.size() + “:”);
20. set.remove(k2);
21. System.out.print(set.size());
22. }
23. }
What is the result?
A. 4:4:2:2
B. 4:4:3:2
C. 2:2:1:0
D. 2:2:0:0
E. 2:1:0:0
F. 2:2:1:1
G. 4:3:2:1
Answer: F

Question 175
Given:
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. List strings = new ArrayList();
5. // insert code here
6. }
7. }
Which four, inserted at line 5, will allow compilation to succeed?
(Choose four.)
A. String s = strings.get(0);
B. Iterator i1 = strings.iterator();
C. String[] array1 = strings.toArray();
D. Iterator i2 = strings.iterator();
E. String[] array2 = strings.toArray(new String[1]);
F. Iterator i3 = strings.iterator();
Answer: ABDE

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

Question 177
Given:
11. public static void append(List list) { list.add(”0042”); }
12. public static void main(String[] args) {
13. List intList = new ArrayList();
14. append(intList);
15. System.out.println(intList.get(0));
16. }
‘What is the result?
A. 42
B. 0042
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
E. Compilation fails because of an error in line 14.
Answer: B

Question 178
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. remove line 14
B. replace line 14 with “int i = iter.next();”
C. replace line 13 with “for (int i : intList) {“
D. replace line 13 with “for (Iterator iter : intList) {“
E. replace the method declaration with “sum(List intList)”
F. replace the method declaration with “sum(List intList)”
Answer: ACF

Question 179
Given:
classA {}
class B extends A {}
class C extends A {}
class D extends B {}
Which three statements are true? (Choose three.)
A. The type List
is assignable to List.
B. The type List is assignable to List
.
C. The type List
is assignable to List.
D. The type List is assignable to List.
E. The type List is assignable to List
.
F. The type Listis assignable to any List reference.
G. The type List is assignable to List.
Answer: CDG
Question 180
Given:
11. public void addStrings(List list) {
12. list.add(”foo”);
13. list.add(”bar”);
14. }
What must you change in this method to compile without warnings?
A. add this code after line 11:
list = (List) list;
B. change lines 12 and 13 to:
list.add(”foo”);
list.add(”bar”);
C. change the method signature on line 11 to:
public void addStrings(List list) {
D. change the method signature on line 11 to:
public void addStrings(List list) {
E. No changes are necessary. This method compiles without warnings.
Answer: D

Question 181
Given:
1. public class Test {
2. public T findLarger(T x, T y) {
3. if(x.compareTo(y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
and:
22. Test t = new Test();
23. // insert code here
Which two will compile without errors when inserted at line 23?
(Choose two.)
A. Object x = t.findLarger(123, “456”);
B. int x = t.findLarger(123, new Double(456));
C. int x = t.findLarger(123, new Integer(456));
D. int x = (int) t.findLarger(new Double(123), new Double(456));
Answer: AC

Question 182
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() 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 {
B. public class MinMax {
C. public class MinMax {
D. public class MinMax {
E. public class MinMax {
F. public class MinMax {
Answer: DF
Question 183
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 min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
B. class MinMax> {
E min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
C. class MinMax> {
E min = null;
E max = null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
D. class MinMax> {
E min = null;
E max = null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
Answer: A

Question 184
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. Tea
B. Coffee
C. Coffee
Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: B

Question 185
Given:
11. List list = // more code here
12. Collections.sort(list, new MyComparator());
Which code will sort this list in the opposite order of the sort in line
12?
A. Collections.reverseSort(list, new MyComparator());
B. Collections.sort(list, new MyComparator());
list.reverse();
C. Collections.sort(list, new InverseComparator(
new MyComparator()));
D. Collections.sort(list, Collections.reverseOrder(
new MyComparator()));
Answer: D

Question 186
Given:
int[] myArray=newint[] {1, 2,3,4, 5};
What allows you to create a list from this array?
A. List myList = myArray.asList();
B. List myList = Arrays.asList(myArray);
C. List myList = new ArrayList(myArray);
D. List myList = Collections.fromArray(myArray);
Answer: B


Question 187
Given:
13. public static void search(List 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. 2
D. a
E. b
F. c
G. The result is undefined.
Answer: G

Question 188
Given:
1. import java.util.*;
2.
3. public class LetterASort {
4. public static void main(String[] args) {
5. ArrayList strings = new ArrayList();
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 AAaa AaA
C. AAaa AaA aAa aAaA
D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa
F. An exception is thrown at runtime.
Answer: C

Question 189
Given:
ArrayList a = new ArrayList();
containing the values {“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”}
Which code will return 2?
A. Collections. sort(a, a.reverse());
int result = Collections.binarySearch(a, “6”);
B. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”);
C. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”,c);
D. Comparator c = Collections.reverseOrder(a);
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”,c);
E. Comparator c = new InverseComparator(new Comparator());
Collections.sort(a);
int result = Collections.binarySearch(a, “6”,c);
Answer: C

Question 190
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. Arrays.sort(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. s = new SortedSet(s);
Answer: B

Question 191
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. BitUtils.process(bytes);
C. util.BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. import util.BitUtils.*; process(bytes);
Answer: C

Question
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. BitUtils.process(bytes);
C. app.BitUtils.process(bytes);
D. util.BitUtils.process(bytes);
E. import util.BitUtils. *; process(bytes);
F. SomeApp cannot use the process method in BitUtils.
Answer: F

Question 193
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 “text”; }
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. text
B. text
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 2 of XMLMessage.
E. Compilation fails because of an error in line 3 of XMLMessage.
Answer: E

Question 194
Given a file GrizzlyBear.java:
1. package animals.mammals;
2.
3. public class GrizzlyBear extends Bear {
4. void hunt() {
5. Salmon s = findSalmon();
6. s.consume();
7. }
8. }
and another file, Salmon.java:
1. package animals.fish;
2.
3. public class Salmon extends Fish {
4. void consume() { /* do stuff */ }
5. }
Assume both classes are defined in the correct directories for theft
packages, and that the Mammal class correctly defines the
findSalmon() method. Which two changes allow this code to compile
correctly? (Choose two.)
A. add public to the start of line 4 in Salmon.java
B. add public to the start of line 4 in GrizzlyBear.java
C. add import animals.mammals.*; at line 2 in Salmon.java
D. add import animals.fish.*; at line 2 in GrizzlyBear.java
E. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
F. add import animals.mammals.GrizzlyBear.*;at line 2 in Salmon.java
Answer: AD

Question 195
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 utils.*;
B. static import utils.*;
C. import utils.Repetition.*;
D. static import utils.Repetition. *;
E. import utils.Repetition.twice();
F. import static utils.Repetition.twice;
G. static import utils.Repetition.twice;
Answer: F

Question 196
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 14
B. s 16
C. s 10
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D

Question 197
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 7 contains an error.
C. Compilation fails. Only line 12 contains an error.
D. Compilation fails. Only line 13 contains an error.
E. Compilation fails. Only lines 7 and 12 contain errors.
F. Compilation fails. Only lines 7 and 13 contain errors.
G. Compilation fails. Lines 7, 12, and 13 contain errors.
Answer: A

Question 198
Given:
11. public class Counter {
12. public static void main(String[] args) {
13. int numArgs = /* insert code here */;
14. }
15. }
and the command line:
java Counter one fred 42
Which code, inserted at line 13, captures the number of arguments
passed into the program?
A. args.count
B. args.length
C. args.count()
D. args.length()
E. args.getLength()
Answer: B

Question 199
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. sim. sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander
Answer: B

Question 200
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(” doStuffx = “+ x++);
25. }
26. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 6 main x = 7
E. doStuffx = 7 main x = 6
F. doStuffx = 7 main x = 7
Answer: B

Question 201
Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < x=" 1;" myprop =" /*" custom="gobstopper" weight =" wt;" weight =" w;" p =" new" p =" new" p =" Payload.setWeight(420);" p =" new" desc =" d;" item =" new" it =" new" it2 =" new" id =" id;" id =" newId;" fa =" new" x =" x;" y =" y;" o =" new" i =" new" n="10;" n =" 100;" i =" new" i =" new" i =" new" x="x;" x =" x;" foo =" new" foo =" new" foofoo =" fooBar(" foo =" fooBar(" numbers =" new" i="0;" value =" i" intobj =" new" rbo =" new" rbo =" null;" booch =" new" snooch =" s;" snoog =" new" snoog =" null;" o =" obj;" o =" new" o =" new" o="null;" str ="=" check =" 4;" check =" str.length())" i =" new" startingi =" i;" i2 =" i2.intValue();" i2 ="=" i2 ="=" cup =" new" elements =" {" 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 null.
D. The variable first is set to elements[0].
Answer: D

Question 224
Given:
42. public class ClassA {
43. public int getValue() {
44.int value=0;
45. boolean setting = true;
46. String title=”Hello”;
47. if (value (setting && title == “Hello”)) { return 1; }
48. if (value == 1 & title.equals(”Hello”)) { return 2; }
49. }
50. }
And:
70. ClassA a = new ClassA();
71. a.getValue();
What is the result?
A. 1
B. 2
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: C

3 comments:

Manisha said...

Just wanted to ask you if these are the questions attempted by you? As the answer to the question no 10
public class Bar {
static void foo(int...x) {
seems to be wrong if its D as I think the B will be the correct answer
for(int z : x) System.out.println(z);
will allow the code to compile.

Manisha said...

sorry I meant Question 2

jacks said...

Yes, it is correct. the answer to the Qn 2 is B. But i think D is also correct, (NOTE: this Question has said :CHOOSE TWO).

According to you id D is incorrect then Please tell me how is it incorrect.....