Question 1
Given:
public class Person {
private name;
public Person(String name) {
this.name = name;
}
public int hashCode() {
return 420;
}
}
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.
Question 2
Given:
interface TestA { String toString(); }
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return “test”; }
});
}
}
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
Question 3
Given:
String #name = “Jane Doe”;
int$age=24;
Double_height = 123.5;
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 4
A JavaBeans component has the following field:
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 void setEnabled( boolean enabled)
public void isEnabled()
C. public void setEnabled( boolean enabled)
public boolean isEnabled()
D. public boolean setEnabled( boolean enabled)
public boolean getEnabled()
Answer: AC
Question 5
Given:
public class Plant {
private String name;
public Plant(String name) { this.name = name; }
public String getName() { return name; }
}
public class Tree extends Plant {
public void growFruit() { }
public void dropLeaves() { }
}
Which is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this(”fern”); } is added to the Plant class.
E. The code will compile if public Plant() { Plant(”fern”); } is added to the Plant class.
Answer: D
Question 6
Given:
int x= 10;
do {
x--;
} while(x< 10);
How many times will line 37 be executed?
A. ten times
B. zero times
C. one to me times
D. more than ten times
Answer: D
Question 7
Given:
try {
// some code here
} catch (NullPointerException e1) {
System.out.print(”a”);
} catch (RuntimeException e2) {
System.out.print(”b”);
} finally {
System.out.print(”c”);
}
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. ab
D. ac
E. bc
F. abc
Answer: D
Question 8
Given:
static void test() {
try {
String x=null;
System.out.print(x.toString() +“ “);
}
finally { System.out.print(“finally “); }
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print(”exception “); }
}
What is the result?
A. null
B. finally
C. null finally
D. Compilation fails.
E. finally exception
Answer: E
Question 9
Given:
public class MyLogger {
private StringBuilder logger = new StringBuuilder();
public void log(String message, String user) {
logger.append(message);
logger.append(user);
}
}
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 10
Assuming that the serializeBanana() and the deserializeBanana()
methods will correctly use Java serialization and given:
import java.io.*;
class Food implemertts Serializable {int good = 3;}
class Fruit externds Food {int juice = 5;}
public class Banana extends Fruit {
int yellow = 4;
public static void main(String [] args) {
Banana b = new Banana(); Banana b2 = new Banana();
b.serializeBanana(b); // assume correct serialization
b2 = b.deserializeBanana(); // assume correct
System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);
}
// more Banana methods go here
}
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