Location: ... Section 1: Declarations, Initialization and Scoping Objective 1.1 Q
Location: ... Section 1: Declarations, Initialization and Scoping Objective 1.1 Q
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.1 > Q
uestion 1
w gs-prex-j055-fo
Given:
10. class EnumModify{
11.
12. public static enum Colors1{RED, GREEN, BLUE};
13. protected enum Colors2{RED, GREEN, BLUE};
14. private enum Colors3{RED, GREEN, BLUE};
15. static enum Colors4{RED, GREEN, BLUE};
16. public enum Colors5{RED, GREEN, BLUE};
17. enum Colors6{RED, GREEN, BLUE};
18. }
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.1 > Qu
estion 2
w gs-prex-j055-fo
And:
1. import com.sun.PkgAccess;
2.
3. public class PkgAccess2 {
4.
5. int x1 = PkgAccess.tiger;
6. int x2 = tiger;
7. int x3 = com.sun.PkgAccess.tiger;
8. int x4 = sun.PkgAccess.tiger;
9. }
Close
Options C and E are correct. Line 6 fails because the package name is missing. Line 8 fails
because the entire package name is required.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.2 > Qu
estion 3
w gs-prex-j055-fo
Given:
1. abstract class Color2 {
2. // insert code here
3. }
4.
5. public class Blue2 extends Color2 {
6. public String getRGB() { return "blue"; }
7. }
A 0
B 1
C 2
D 3
E 4
Option D is correct. The only illegal declaration is the private declaration. Normal override
rules apply.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.2 > Qu
estion 4
w gs-prex-j055-fo
Given:
1. interface Altitude {
2. // insert code here
3. }
A 0
B 1
C 2
D 3
E 4
Option C is correct. The abstract and strictfp modifiers cannot be applied to variables.
req=standalone& req=standalone& req=standalone& req=standalone& req=exit req=action&type=
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.3 > Qu
estion 5
w gs-prex-j055-fo
Given:
11. class Other{
12. enum Colors{RED, GREEN, BLUE, YELLOW};
13. }
14.
15. class UseEnums{
16. public static void main(String [] args) {
17. for( Other.Colors c : Other.Colors.values()) {
18. if (Other.Colors.RED.equals(c))
19. System.out.print("red ");
20. if (c == Other.Colors.GREEN)
21. System.out.print("green ");
22. if (c.equals("BLUE"))
23. System.out.print("blue ");
24. } } }
A red blue
B red green
C green blue
E Compilation fails.
Option B is correct. Lines 18 and 20 are correct enum syntax.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.3 > Qu
estion 6
w gs-prex-j055-fo
Given:
1. class Test {
2. public static void main(String [] args) {
3. for(int x = 0; x < 7; ++x) {
4. int y = 2;
5. x = ++y;
6. }
7. System.out.println("y = " + y);
8. }
9. }
A y=5
B y=6
C y=7
D y=8
E Compilation fails.
Option E is correct. Line 7 is illegal because y is declared within the for block.
w gs-prex-j055-fo
Given:
1. class DoStuff {
2. public static void main(String [] args) {
3. doIt(1);
4. doIt(1,2);
5. }
6. // insert code here
7. }
A 0
B 1
C 2
D 3
E 4
Option D is correct. The second fragment is illegal because a method can have no more
than one vararg argument
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.4 > Question 8
Given:
1. class Banana2 {
2. static int x = 2;
3. public static void main(String [] args) {
4. int x = 2;
5. Banana2 b = new Banana2();
6. b.go(x);
7. }
8. static { x += x; }
9. void go(int x) {
10. ++x;
11. System.out.println(x);
12. }
13. }
A 2
B 3
C 5
D 7
E Compilation fails.
Option B is correct. The x in the static block is a different variable than the x in main.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.5 > Question 9
Given:
1. class SuperFoo {
2. SuperFoo doStuff(int x) {
3. return new SuperFoo();
4. }
5. }
6.
7. class Foo extends SuperFoo {
8. // insert code here
9. }
Options A, C, and E are correct. Options B and D are incorrect because an override cannot
change the return type unless it's a valid covariant return. Option C is a valid covariant
return.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.5 > Question 10
Given:
1. class FWD {
2. int doMud(int x) { return 1; }
3. }
4. class Subaru extends FWD {
5. int doMud(int... y) { return 2; }
6. int doMud(int z) { return 3; }
7. }
8. class Race {
9. public static void main(String [] args) {
10. FWD f = new Subaru();
11. System.out.println(f.doMud(7));
12. }
13. }
A 1
B 2
C 3
D 7
E Compilation fails.
Option C is correct. The JVM chooses the non-vararg method when the polymorphic call to
doMud is made.
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.6 > Qu
estion 11
Given:
1. class Top {
2. static int x = 1;
3. public Top(int y) { x *= 3; }
4. }
5. class Middle extends Top {
6. public Middle() { x += 1; }
7. public static void main(String [] args) {
8. Middle m = new Middle();
9. System.out.println(x);
10. }
11. }
A 1
B 2
C 3
D 4
E 6
F Compilation fails.
Option F is correct. Middle needs to invoke Top's int argument constructor
Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.6 > Question 12
Given:
1. class Pastry {
2. public static class Filling {
3. public void berry() { System.out.println("yum "); }
4. }
5. }
6. class Bakery {
7. public static void main(String [] args) {
8. // insert code here
9. pf.berry();
10. }
11. }
Option B is correct. This is the correct syntax to instantiate this inner class.
Location: ... > Section 2: Flow Control > Objective 2.1 > Question 13
Given:
1. enum Days {MONDAY, TUESDAY, WEDNESDAY, THURSDAY}
2.
3. class Test {
4. public static void main(String [] args) {
5. int x = 0;
6. Days d = Days.TUESDAY;
7. switch(d) {
8. case MONDAY: x++;
9. case TUESDAY: x = x + 10;
10. case WEDNESDAY: x = x + 100;
11. case THURSDAY: x = x + 1000;
12. }
13. System.out.println("x = " + x);
14. }
15. }
A x = 10
B x = 110
C x = 1110
D Compilation fails.
Option C is correct. This is the standard switch fall-through logic, and as of Java 1.5 enums
can be used in a switch.
Location: ... > Section 2: Flow Control > Objective 2.1 > Question 14
Given:
1. class Test4 {
2. public static void main(String [] args) {
3. boolean x = true;
4. boolean y = false;
5. short z = 42;
6.
7. if((z++ == 42) && (y = true)) z++;
8. if((x = false) || (++z == 45)) z++;
9.
10. System.out.println("z = " + z);
11. }
12. }
A z = 42
B z = 44
C z = 45
D z = 46
E Compilation fails.
Option D is correct. Line 7 does NOT test y, it sets it to true. Line 8 pre-increments z in the
if test
Location: ... > Section 2: Flow Control > Objective 2.2 > Question 15
Given:
3. import java.util.*;
4. class ForInTest {
5. static List list = new ArrayList();
6.
7. public static void main(String [] args) {
8. list.add("a"); list.add("b"); list.add("c");
9. // insert code here
10. System.out.print(o);
11. }
12. }
A for(Object o : list)
B for(Iterator o : list)
C for(Object o : list.iterator())
Option A is correct. This is the correct syntax to iterate through the List.
Location: ... > Section 2: Flow Control > Objective 2.2 > Question 16
Given:
23. int x = 7;
24. switch (x) {
25. case 8: System.out.print("8");
26. case 7: System.out.print("7");
27. case 6: System.out.print("6");
28. default: System.out.print("def");
29. case 9: System.out.print("9");
30. }
A 7
B 789
C 76def
D 76def9
E Compilation fails.
Option D is correct. This is an example of standard switch fall-through logic with a default
case
Location: ... > Section 2: Flow Control > Objective 2.3 > Question 17
Given:
1. class TestAssert {
2. public static void main(String [] args) {
3. assert(false): "more info ";
4. System.out.println("after assert ");
5. }
6. }
Which is true?
B The command-line invocation java -ea TestAssert will produce no error and
the output "more info ".
C The command-line invocation java -ea TestAssert will produce no error and
the output "after assert ".
D The command-line invocation java -ea TestAssert will produce no error and
the output "more info after assert ".
E The command-line invocation java -ea TestAssert will produce no error and
the output "after assert more info".
Option A is correct. The command-line enables assertions, and more info is added to the
stack trace before the program ends at line 3 with an AssertionError
Location: ... > Section 2: Flow Control > Objective 2.3 > Question 18
Given:
1. class MoreAsserts {
2. static int x = 5;
3. public static void main(String [] args) {
4. assert(doStuff(42));
5. if(x < 40) ;
6. else assert(false);
7. }
8. public static boolean doStuff(int arg) {
9. assert(arg < x++);
10. return false;
11. }
12. }
Which is true?
Option A is correct. The assert statement at line 9 throws an AssertionError
Location: ... > Section 2: Flow Control > Objective 2.4 > Question 19
Given:
1. class Parser extends Utils {
2. public static void main(String [] args) {
3. try { System.out.print(new Parser().getInt("42"));
4. } catch (NumberFormatException n) {
5. System.out.println("NFExc "); }
6. }
7. int getInt(String arg) throws NumberFormatException {
8. return Integer.parseInt(arg);
9. }
10. }
11. class Utils {
12. int getInt(String arg) { return 42; }
13. }
A 42
B NFExc
C 42NFExc
D Compilation fails.
Option A is correct. It is legal to add a runtime exception, but not a checked exception, to
an overridden method.
Location: ... > Section 2: Flow Control > Objective 2.4 > Question 20
Given:
1. class Parser extends Utils {
2. public static void main(String [] args) {
3. try { System.out.print(new Parser().getInt("42"));
4. } catch (Exception e) {
5. System.out.println("Exc"); }
6. }
7. int getInt(String arg) {
8. return Integer.parseInt(arg);
9. }
10. }
11. class Utils {
12. int getInt(String arg) throws Exception { return 42; }
13. }
A 42
B Exc
C 42Exc
D Compilation fails.
Option A is correct. It is legal for an overriding method to NOT throw the overridden
method's exception.
Location: ... > Section 2: Flow Control > Objective 2.5 > Question 21
Given:
1. class Flow {
2. public static void main(String [] args) {
3. try {
4. System.out.print("before ");
5. doRiskyThing();
6. System.out.print("after ");
7. } catch (Exception fe) {
8. System.out.print("catch ");
9. }
10. System.out.println("done ");
11. }
12. public static void doRiskyThing() throws Exception {
13. // this code returns unless it throws an Exception
14. } }
A before
B before catch
Options C and D are correct. C is correct when no exception is thrown and D is correct
when an exception is thrown.
Location: ... > Section 2: Flow Control > Objective 2.5 > Question 22
Given:
1. class Birds {
2. public static void main(String [] args) {
3. try {
4. throw new Exception();
5. } catch (Exception e) {
6. try {
7. throw new Exception();
8. } catch (Exception e2) { System.out.print("inner "); }
9. System.out.print("middle ");
10. }
11. System.out.print("outer ");
12. }
13. }
A inner
B inner outer
C middle outer
F Compilation fails.
Option D is correct. It is legal to nest try/catches and normal flow rules apply
Location: ... > Section 2: Flow Control > Objective 2.6 > Question 23
Given:
1. class ArrayCalculator {
2. int [] holder = {1,2,3,4,5};
3. public static void main(String [] args) {
4. new ArrayCalculator().go(1);
5. }
6. void go(int x) {
7. holder[x%5] = x++;
8. go(x);
9. }
10. }
A Compilation fails.
C A java.lang.StackOverflowError is thrown.
D A java.lang.IllegalStateException is thrown.
E A java.lang.ArrayIndexOutOfBoundsException is thrown.
Option C is correct. Line 8 is a recursive call to the go method.
Location: ... > Section 2: Flow Control > Objective 2.6 > Question 24
Given:
1. class Calc {
2. public static void main(String [] args) {
3. try {
4. int x = Integer.parseInt("42a");
5. // insert code here
6. System.out.print("oops ");
7. }
8. }
9. }
Which two, inserted independently at line 5, cause the output to be "oops "? (Choose
two.)
A } catch (ClassCastException c) {
B } catch (IllegalStateException c) {
C } catch (NumberFormatException n) {
D } catch (IllegalArgumentException e) {
E } catch (ExceptionInInitializerError e) {
Options C and D are correct. NumberFormatException extends IllegalArgumentException
Location: ... > Section 3: API Contents > Objective 3.1 > Question 25
Given:
21. class Beta {
22. public static void main(String [] args) {
23.
24. Integer x = new Integer(6) * 7;
25. if (x != 42) {
26. System.out.print("42 ");
27. } else if (x.equals(42)) {
28. System.out.print("dot = ");
29. } else {
30. System.out.print("done");
31. } } }
A 42
B done
C dot =
D Compilation fails.
Option C is correct. The boxing syntax is correct.
Location: ... > Section 3: API Contents > Objective 3.1 > Question 26
Given:
1. class WideLoad {
2. public static void main(String [] args) {
3. float f = 3.14f;
4. new WideLoad().doIt(f);
5. }
6. void doIt(Float f) {
7. System.out.println("Float");
8. }
9. void doIt(double d) {
10. System.out.println("double");
11. }
12. }
A Float
B double
C Compilation fails.
Option B is correct. The JVM will widen before it boxes, so the method on line 9 is
invoked
Location: ... > Section 3: API Contents > Objective 3.2 > Question 27
Given:
- f is a reference to a valid java.io.File instance
- fr is a reference to a valid java.io.FileReader instance
- br is a reference to a valid java.io.BufferedReader instance
And:
34. String line = null;
35.
36. // insert code here
37. System.out.println(line);
38. }
Which code, inserted at line 36, will loop through a text file and output a line at a
time from the text field?
Option F is correct. Of the three classes, only BufferedReader has a readLine method, and
read is for characters, not lines.
Location: ... > Section 3: API Contents > Objective 3.3 > Question 28
Given:
10. class Car implements Serializable {
11. Wheels w = new Wheels();
12. }
13.
14. class Wheels { }
Option D is correct. Since Car has an instance of Wheels, Wheels must also be serializable.
Location: ... > Section 3: API Contents > Objective 3.3 > Question 29
When defined in a serializable class, which is called by the JVM when an object of
that class is serialized?
Location: ... > Section 3: API Contents > Objective 3.4 > Question 30
Given:
1. import java.text.*;
2.
3. class DateFormatter {
4. public static void main(String [] args) {
5. DateFormat df = new DateFormat();
6. DateFormat df2 = DateFormat.getInstance();
7. DateFormat df3 = DateFormat.getDateInstance();
8. NumberFormat nf = DateFormat.getNumberInstance();
9. NumberFormat nf2 = DateFormat.getNumberFormat();
10. NumberFormat nf = df.getNumberFormat();
11. }
12. }
B line 6
C line 7
D line 8
E line 9
F line 10
Options A, D, and E are correct. Line 5 fails because DateFormat is abstract. Line 8 fails
because there is no such method. Line 9 fails because getNumberFormat is not a static
method.
Location: ... > Section 3: API Contents > Objective 3.4 > Question 31
Given:
1. import java.text.*;
2. import java.util.*;
3.
4. class ParseTest {
5. public static void main(String [] args) {
6. DateFormat df = DateFormat.getDateInstance(
DateFormat.MEDIUM, Locale.US);
7. Date d = new Date(0L);
8. String date = "Java 3, 2005";
9. // insert code here
...
13. }
14. }
B
try {
d = df.parse(date);
} catch (ParseException e) { }
System.out.println(d.getTime());
C
try {
d = df.parseDate(date);
} catch (ParseException e) { }
System.out.println(d);
D
try {
d = df.parseDate(date);
} catch (ParseException e) { }
System.out.println(d.getTime());
Option B is correct. The DateFormat class has a parse method and the Date class has a
getTime method.
Location: ... > Section 3: API Contents > Objective 3.5 > Question 32
Given:
1. import java.io.PrintWriter;
2.
3. class DoFormat {
4. public static void main(String [] args) {
5. int x = 42;
6. int y = 12345;
7. float z = 7;
8. System.out.format("-%4d- ", x);
9. System.out.format("-%4d- ", y);
10. System.out.format("-%4.1d- ", z);
11. }
12. }
Option E is correct. A d in the format string is for integers, NOT floats.
Location: ... > Section 3: API Contents > Objective 3.5 > Question 33
Given:
1. class StringSplit {
2. public static void main(String [] args) {
3.
4. String s = "x1234 y56 z7 a";
5. String [] sa = s.split("\\d");
6. int count = 0;
7. for( String x : sa)
8. count++;
9. System.out.println("total: " + count);
10. }
11. }
B total: 4
C total: 7
D total: 8
E Compilation fails.
Option D is correct. The \d means that every digit is a terminator, which creates 7 entries,
and then the end of the String creates the eighth terminator.
Location: ... > Section 4: Concurrency > Objective 4.1 > Question 34
Given:
1. class Thread2 implements Runnable {
2. void run() {
3. System.out.print("go ");
4. }
5.
6. public static void main(String [] args) {
7. Thread2 t2 = new Thread2();
8. Thread t = new Thread(t2);
9. t.start();
10. }
11. }
A go
B Compilation fails.
Option B is correct. The run method must be public.
Location: ... > Section 4: Concurrency > Objective 4.1 > Question 35
A 0
B 1
C 2
D 3
Option A is true. None of these are required.
Location: ... > Section 4: Concurrency > Objective 4.2 > Question 36
Given:
1. class Work implements Runnable {
2. Thread other;
3. Work(Thread other) { this.other = other; }
4. public void run() {
5. try { other.join(); } catch (Exception e) { }
6. System.out.print("after join ");
7. } }
8.
9. class Launch {
10. public static void main(String [] args) {
11. new Thread(new Work(Thread.currentThread())).start();
12. System.out.print("after start ");
13. } }
B after start
C Compilation fails.
Option E is correct. It is legal to join main.
Location: ... > Section 4: Concurrency > Objective 4.2 > Question 37
Given:
1. class Order3 implements Runnable {
2. public static void main(String [] args) {
3. new Thread(new Order3()).start();
4. for(int x = 0; x < 10; x++) System.out.print("m");
5. }
6. public void run() {
7. for(int x = 0; x < 10; x++) {
8. // insert code here
9. System.out.print("r");
10. }
11. }
12. }
And that:
"before" output is created when the code is compiled and run as is,
and "after" output is created when the following is added at line 8:
if (x > 3 && x < 7) Thread.yield();
When you compare the "before" output to the "after" output, which is true?
C The character "m" is less likely to appear early in the "after" output.
D The character "m" is more likely to appear early in the "after" output.
Option D is correct. yield can temporarily pause the current thread, in this case the thread
that outputs r's. The possibility of temporarily pausing the creation of r's means m's are
more likely to appear early in the output.
Location: ... > Section 4: Concurrency > Objective 4.3 > Question 38
Given:
5. class NoGo implements Runnable {
6. private int i;
7. public void run() {
8. if (i%10 != 0) { i++; }
9. for(int x=0; x<10; x++, i++)
10. { if (x == 4) Thread.yield(); }
11. System.out.print(i + " ");
12. }
13. public static void main(String [] args) {
14. NoGo n = new NoGo();
15. for(int x=0; x<100; x++) { new Thread(n).start(); }
16. }
17. }
Which is true?
A The output can never contain the value 10.
Option E is correct. With or without the yield, and with no synchronization in place, any
value up to 1100 can be displayed.
Location: ... > Section 4: Concurrency > Objective 4.4 > Question 39
Close
Exhibit
1. class Waiting implements Runnable {
2. boolean flag = true;
3. public synchronized void run() {
4. if (flag) {
5. flag = false;
6. System.out.print("1 ");
7. try { this.wait(); } catch (Exception e) { }
8. System.out.print("2 ");
9. }
10. else {
11. flag = true;
12. System.out.print("3 ");
13. try { Thread.sleep(2000); } catch (Exception e) { }
14. System.out.print("4 ");
15. notify();
16. }
17. }
18. public static void main(String [] args) {
19. Waiting w = new Waiting();
20. new Thread(w).start();
21. new Thread(w).start();
22. }
23. }
Options D and F are correct. The if block puts the first thread in a wait state. The else block
sleeps for two seconds then notifies the first thread, which then completes.
Location: ... > Section 4: Concurrency > Objective 4.4 > Question 40
Given:
3. class Waiting3 implements Runnable {
4. int state;
5. public synchronized void run() {
6. if (state++ < 3) {
7. System.out.print(" " + Thread.currentThread().getId());
8. try { this.wait(); } catch (Exception e) { }
9. System.out.print(" " + Thread.currentThread().getId());
10. }
11. else {
12. try { Thread.sleep(2000); } catch (Exception e) { }
13. notify();
14. notifyAll();
15. }
16. }
17. public static void main(String [] args) {
18. Waiting3 w1 = new Waiting3();
19. Waiting3 w2 = new Waiting3();
20. new Thread(w1).start();
21. new Thread(w1).start();
22. new Thread(w2).start();
23. new Thread(w2).start();
24. }
25. }
Options A and D are correct. Because there are two runnables, neither one's state ever
equals 3, so the else block is never executed and the threads wait forever. C is incorrect
because four different threads are started and each must have a unique Id.
Location: ... > Section 5: OO Concepts > Objective 5.1 > Question 41
Given:
1. class VetUtility {
2. private String petName;
3. static String taxCode;
4. void setTaxCode(String tc) { taxCode = tc);
5. void displayPetInfo() {
6. System.out.println("pet name is " + petName);
7. }
8. int calculateMedCosts(String Owner) {
9. // do complex calculations
10. }
11. }
Which is true?
Option B is correct, the taxCode variable is not private, and the method functions are
unrelated to each other.
Location: ... > Section 5: OO Concepts > Objective 5.1 > Question 42
Given:
12. class Customer {
13. private String address;
14. void setAddress(String addr) { address = addr; }
15. void checkInventory(int sku) { /* check inv. */ }
16. }
Options B and C are correct. The setAddress method makes sense in terms of a customer,
however a customer would be unlikely to check inventory.
Location: ... > Section 5: OO Concepts > Objective 5.2 > Question 43
Given:
1. class Dog { }
2. class Harrier extends Dog { }
3.
4. class DogTest {
5. public static void main(String [] args) {
6. Dog d1 = new Dog();
7. Harrier h1 = new Harrier();
8. Dog d2 = h1;
9. Harrier h2 = (Harrier) d2;
10. Harrier h3 = d2;
11. }
12. }
Which is true?
A Compilation fails.
Location: ... > Section 5: OO Concepts > Objective 5.2 > Question 44
Given:
1. class Alpha { void m1() {} }
2. class Beta extends Alpha { void m2() { } }
3. class Gamma extends Beta { }
4.
5. class GreekTest {
6. public static void main(String [] args) {
7. Alpha [] a = {new Alpha(), new Beta(), new Gamma() };
8. for(Alpha a2 : a) {
9. a2.m1();
10. if (a2 instanceof Beta || a2 instanceof Gamma)
11. // insert code here
12. }
13. }
14. }
Which code, inserted at line 11, will compile but cause an exception to be thrown at
runtime?
A a2.m2();
B ((Beta)a2).m2();
C ((Alpha)a2).m2();
D ((Gamma)a2).m2();
Option D is correct. Options A and C will NOT compile, option B will compile and run.
Option D throws an exception because type Alpha has no m2 method.
Location: ... > Section 5: OO Concepts > Objective 5.3 > Question 45
Given:
1. class High {
2. // insert code here
3. }
4. class Low extends High {
5. public Low () { System.out.print("low const "); }
6. public static void main(String [] args) {
7. Low l = new Low();
8. }
9. }
How many code fragments, inserted independently at line 2, allow the code to
compile and run without exception?
A 0
B 1
C 2
D 3
E 4
Option D is correct. When there is a private constructor in class High, class Low cannot
compile
Location: ... > Section 5: OO Concepts > Objective 5.3 > Question 46
Given:
1. class Tree {
2. private static String tree = "tree ";
3. String getTree() { return tree; }
4. }
5. class Elm extends Tree {
6. private static String tree = "elm ";
7. public static void main(String [] args) {
8. new Elm().go(new Tree());
9. }
10. void go(Tree t) {
11. String s = t.getTree() + Elm.tree + tree + (new Elm().getTree());
12. System.out.println(s);
13. } }
E Compilation fails.
Location: ... > Section 5: OO Concepts > Objective 5.4 > Question 47
Given:
2. class Cat {
3. Cat(int c) { System.out.print("cat" + c + " "); }
4. }
5. class SubCat extends Cat {
6. SubCat(int c) { super(5); System.out.print("cable "); }
7. SubCat() { this(4); }
8. public static void main(String [] args) {
9. SubCat s = new SubCat();
10. }
11. }
A cat5
B cable
C cable cat5
D cat5 cable
E Compilation fails.
Location: ... > Section 5: OO Concepts > Objective 5.4 > Question 48
Given:
2. class Mineral {
3. static String shiny() { return "1"; }
4. }
5. class Granite extends Mineral {
6. public static void main(String [] args) {
7. String s = shiny() + getShiny();
8. s = s + super.shiny();
9. System.out.println(s);
10. }
11. static String getShiny() { return shiny(); }
12. }
A 3
B 12
C 111
D Compilation fails.
Option D is correct. Line 8 will cause a compiler error because of the call to super
Location: ... > Section 5: OO Concepts > Objective 5.5 > Question 49
Given:
1. class A extends B implements X { }
2. class B { C c; }
3. interface X { void go(); }
4. class C { }
Which is true?
A Class C is-a X.
B Class B is-a A.
C Class A has-a X.
D Compilation fails.
E Interface X has-a C.
Option D is correct, The code will NOT compile because A does NOT properly implement
X (no go method implementation).
Location: ... > Section 5: OO Concepts > Objective 6.1 > Question 50
A programmer wants to create a collection into which she can insert and find
key/value pairs.
A HashSet
B Hashtable
C SortedMap
D PriorityQueue
E LinkedHashMap
Options B and E are correct. SortedMap is an interface
Location: ... > Section 5: OO Concepts > Objective 6.1 > Question 51
Given:
5. import java.util.*;
6. class AddStuff2 {
7. public static void main(String [] args) {
8. TreeSet<String> t = new TreeSet<String>();
9. if(t.add("one "))
10. if(t.add("two "))
11. if(t.add("three "))
12. t.add("four ");
13. for(String s : t)
14. System.out.print(s);
15. }
16. }
A one
F Compilation fails.
Option D is correct. The TreeSet is sorted alphabetically
Location: ... > Section 5: OO Concepts > Objective 6.2 > Question 52
A Impossible to determine
B
public boolean equals(Object o) {
Sock s = (Sock) o;
return size.equals(s.size);
}
C
public boolean equals(Object o) {
Sock s = (Sock) o;
return color.equals(s.color);
}
D
public boolean equals(Object o) {
Sock s = (Sock) o;
return size.equals(s.size) && color.equals(s.color);
}
Option D is correct because the equals method must test both size and color, since the
hashCode method uses them both in its hash formula
Location: ... > Section 5: OO Concepts > Objective 6.2 > Question 53
Given:
1. class Sock2 {
2. String color;
3. public boolean equals(Object o) {
4. return color.equals(((Sock2)o).color);
5. } }
6. class TestSocks {
7. public static void main(String [] args) {
8. Sock2 s1 = new Sock2(); s1.color = "blue";
9. Sock2 s2 = new Sock2(); s2.color = "blue";
10. if (s1.equals(s2)) System.out.print("equals ");
11. if (s1 == s2) System.out.print("== ");
12. }
13. }
A ==
B equals
C equals ==
D No output is produced.
Option B is correct. String's equals method is overridden to test for the same value. == fails
because it tests for the same object
Location: ... > Section 5: OO Concepts > Objective 6.3 > Question 54
Given:
1. import java.util.*;
2. public class Gen3 {
3. public static void go(Set<Dog> d) { }
4. public static void main(String [] args) {
5. // insert code here
6. go(t);
7. }
8. }
9. class Animal { }
10. class Dog extends Animal { }
A only s1
B only s2
C only s1 and s2
D only s1 and s3
Option C is correct. The go method can only take a Set of Dogs.
Location: ... > Section 5: OO Concepts > Objective 6.3 > Question 55
w gs-prex-j055-fo
Given:
1. import java.util.*;
2. class SubGen {
3. public static void main(String [] args) {
4. // insert code here
5. }
6. }
7. class Alpha { }
8. class Beta extends Alpha { }
9. class Gamma extends Beta { }
A Only s1
B Only s3
C Only s1 and s3
D Only s1 and s4
Location: ... > Section 5: OO Concepts > Objective 6.4 > Question 56
Given:
2. import java.util.*;
3. class Beta extends Alpha {
4. public static void go(Set<? super Alpha> set) { }
5. public static void main(String [] args) {
6. Set<Alpha> setA = new TreeSet<Alpha>();
7. Set<Beta> setB = new TreeSet<Beta>();
8. Set<Object> setO = new TreeSet<Object>();
9. // insert code here
10. }
11. }
12. class Alpha { }
A Only s1
B Only s2
C Only s3
D Only s1 and s2
E Only s1 and s3
F Only s2 and s3
Option E is correct. s2 will NOT compile because go takes only Sets of superclasses of
Alpha.
Location: ... > Section 5: OO Concepts > Objective 6.5 > Question 57
w gs-prex-j055-fo
Given:
1. import java.util.*;
2. class Stuff implements Comparator {
3. int x;
4. Stuff(int x) { this.x = x; }
5. public int compareTo(Object o) { return this.x - ((Stuff)o).x; }
6. }
7. class AddStuff {
8. public static void main(String [] args) {
9. TreeSet<Stuff> ts = new TreeSet<Stuff>();
10. ts.add(new Stuff(1));
11. ts.add(new Stuff(2));
12. System.out.println(ts.size());
13. } }
A 0
B 1
C 2
D Compilation fails.
Location: ... > Section 5: OO Concepts > Objective 6.5 > Question 58
w gs-prex-j055-fo
Given:
1. import java.util.*;
2. class SearchArray {
3. public static void main(String [] args) {
4. int [] a = {9,7,5,3,1};
5. Arrays.sort(a);
6. System.out.println(Arrays.binarySearch(a,3) + " "
+ Arrays.binarySearch(a,8));
7. }
8. }
A 1 -1
B 1 -5
C 3 -1
D 3 -2
Option B is correct. The search for 8 returns a negative number indicating the
insertion point you would use to insert 8.
Location: ... > Section 7: Fundamentals > Objective 7.1 > Question 59
w gs-prex-j055-fo
And:
1. import static x.StaticStuff.*;
2. class FindStatic {
3. public static void doStuff() {
4. Color c = Color.BLUE;
5. s = "hi"; i = 7;
6. go();
7. } }
When class StaticStuff has been compiled, what is the result when you attempt
to compile class FindStatic?
Option A is correct. All of these static imports are declared and used correctly.
Location: ... > Section 7: Fundamentals > Objective 7.1 > Question 60
w gs-prex-j055-fo
And:
1. class Find {
2. public static void main(String [] args) {
3. // insert code here
4. }
5. }
Which two, inserted independently at line 3 in class Find, will compile and produce
the output "doX"? (Choose two.)
A doX();
B X.doX();
C x.X.doX();
Options C and E are correct. Fully qualified names require no import statements.
Location: ... > Section 7: Fundamentals > Objective 7.2 > Question 61
w gs-prex-j055-fo
Given:
1. class TestMain {
2. static int x = 2;
3. static { x = 4; }
4. public static void main(String... args) {
5. int y = x + 1;
6. System.out.println(y);
7. }
8. }
A 3
B 5
C Compilation fails.
Location: ... > Section 7: Fundamentals > Objective 7.2 > Question 62
w gs-prex-j055-fo
Given:
1. class java {
2. public static void main(String [] java) {
3. for (int Java = 1; Java < java.length; Java++)
4. System.out.print("java ");
5. }
6. }
And the command line:
java java java java java
A java
B java java
D Compilation fails.
Option B is correct. There are three command-line arguments, but the for loop starts at
index 1.
Location: ... > Section 7: Fundamentals > Objective 7.3 > Question 63
w gs-prex-j055-fo
Given:
5. class Wrench2 {
6. int size;
7. public static void main(String [] args) {
8. Wrench2 w = new Wrench2();
9. w.size = 9;
10. Wrench2 w2 = go(w, w.size);
11. System.out.print(w2.size);
12. }
13. static Wrench2 go(Wrench2 wr, int s) {
14. s = 7;
15. return wr;
16. }
17. }
A 7
B 9
C Compilation fails.
Location: ... > Section 7: Fundamentals > Objective 7.3 > Question 64
w gs-prex-j055-fo
Given:
1. class Passer2 {
2. // insert code here
3. static int bigState = 42;
4. public static void main(String [] args) {
5. bigState = p2.go(bigState);
6. System.out.print(bigState);
7. }
8. int go(int x) {
9. return ++x;
10. }
11. }
A 0
B 1
C 2
D 3
E 4
Option E is correct. Final indicates that the reference variable cannot be assigned to a
different object, but the object's instances variables can still be mutated.
Location: ... > Section 7: Fundamentals > Objective 7.4 > Question 65
w gs-prex-j055-fo
Given:
5. class Rubbish {
6. Rubbish r;
7. public static void main(String [] args) {
8. Rubbish r1 = new Rubbish();
9. Rubbish r2 = new Rubbish();
10. Rubbish r3 = new Rubbish();
11. r1.r = r2; r2.r = r3; r3.r = r1;
12. r3 = null;
13. r2 = null;
14. r1 = null;
15. // do stuff
16. }
17. }
After which line does the first object become eligible for garbage collection?
A after line 11
B after line 12
C after line 13
D after line 14
E Compilation fails.
Option D is correct. NOT until line 14 are any objects inaccessible from a live thread.
Location: ... > Section 7: Fundamentals > Objective 7.4 > Question 66
w gs-prex-j055-fo
Given:
13. void go() {
14. Gc2 gc2 = new Gc2();
15. go2(gc2);
16. gc2 = null;
17. // do stuff
18. }
19. Gc2 go2(Gc2 g) {
20. return go3(g);
21. }
At what point does the object referred to be gc2 become eligible for garbage
collection?
A line 15
B line 16
C line 20
Option E is correct. The go3 method might save a copy of the gc2 variable.
Location: ... > Section 7: Fundamentals > Objective 7.5 > Question 67
w gs-prex-j055-fo
Which two, inserted independently, allow the code to compile? (Choose two.)
A package com;
B import com.x;
C package com.x;
D import com.Alpha;
E package com.Gamma;
F import com.x.Alpha;
G import com.x.y.Beta;
Options C and F are correct. Line 5 locates Gamma and line 6 locates Beta.
Location: ... > Section 7: Fundamentals > Objective 7.5 > Question 68
w gs-prex-j055-fo
Which two are places that a JAR file can be located so that it can be found
automatically by the compiler? (Choose two.)
Options C and G are correct. JAVA_HOME and CLASSPATH are the two environment
variables to use.
Location: ... > Section 7: Fundamentals > Objective 7.6 > Question 69
w gs-prex-j055-fo
Given:
1. class Ifs {
2. public static void main(String [] args) {
3. boolean state = false;
4. int i = 1;
5. if((++i > 1) && (state = true))
6. i++;
7. if((++i > 3) || (state = false))
8. i++;
9. System.out.println(i);
10. }
11. }
A 3
B 4
C 5
D Compilation fails.
Option C is correct. Line 5 does a pre-increment and sets state to true. Line 7 does a pre-
increment and sets state to false.
Location: ... > Section 7: Fundamentals > Objective 7.6 > Question 70
w gs-prex-j055-fo
Given:
3. public class Tester {
4. public static void main (String[] args) {
5. int x = 5;
6. Integer x1 = x; Integer x2 = x;
7. int x3 = new Integer(5);
8. System.out.print(x1.equals(x));
9. System.out.print(x1 == x);
10. System.out.print(x2.equals(x1));
11. System.out.print(x2 == x1);
12. System.out.print(x2 == x3);
13. System.out.print(x2.equals(x3));
14. }
15. }
A Compilation fails.
B truetruetruetruetruetrue
C falsefalsetruetruetruetrue
D falsefalsetruetruetruefalse
E truefalsetruefalsefalsetrue
Option B is correct. Autoboxing makes all these comparisons true.