Image

 

What is the result?

A. true true

B. false true

C. true false

D. false false

E. Compilation fails

F. An exception is thrown at runtime

6. Given:

Image

 

What is the result?

A. pre b1 b2 r3 r2 hawk

B. pre b2 b1 r2 r3 hawk

C. pre b2 b1 r2 r3 hawk r1 r4

D. r1 r4 pre b1 b2 r3 r2 hawk

E. r1 r4 pre b2 b1 r2 r3 hawk

F. pre r1 r4 b1 b2 r3 r2 hawk

G. pre r1 r4 b2 b1 r2 r3 hawk

H. The order of output cannot be predicted

I. Compilation fails

7. Given:

Image

 

Which are true? (Choose all that apply.)

A. The output could contain 30

B. The output could contain @bf73fa

C. The output could contain DIAMONDS

D. Compilation fails due to an error on line 6

E. Compilation fails due to an error on line 7

F. Compilation fails due to an error on line 8

G. Compilation fails due to an error on line 9

H. Compilation fails due to an error within lines 12 to 14

8. Given:

Image

 

What is the result?

A. 5 7

B. 5 8

C. 8 7

D. 8 8

E. Compilation fails

F. An exception is thrown at runtime

9. Given:

Image

 

What is the result?

A. 212

B. 232

C. 234

D. 312

E. 332

F. 334

G. Compilation fails

10. Given:

Image

 

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?

A. Three objects were created

B. Four objects were created

C. Five objects were created

D. Zero objects are eligible for GC

E. One object is eligible for GC

F. Two objects are eligible for GC

G. Three objects are eligible for GC

11. Given:

Image

 

When line 16 is reached, how many objects will be eligible for garbage collection?

A. 0

B. 1

C. 2

D. 3

E. 4

F. 5

12. Given:

Image

 

What is the result?

A. 4 4

B. 5 4

C. 6 4

D. 4 5

E. 5 5

F. Compilation fails

13. Given:

Image

 

What is the result?

A. 2

B. 3

C. 4

D. 5

E. Compilation fails

F. An exception is thrown at runtime

SELF TEST ANSWERS

1. Given:

Image

 

When // doStuff is reached, how many objects are eligible for GC?

A. 0

B. 1

C. 2

D. Compilation fails

E. It is not possible to know

F. An exception is thrown at runtime

Answer:

 

Image C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

 

Image A, B, D, E, and F are incorrect based on the above. (Objective 7.4)

 

2. Given:

Image

 

What is the result?

A. many

B. a few

C. Compilation fails

D. The output is not predictable

E. An exception is thrown at runtime

Answer:

 

Image C is correct, compilation fails. The var-args declaration is fine, but invade takes a short, so the argument 7 needs to be cast to a short. With the cast, the answer is B , 'a few'.

 

Image A, B, D, and E are incorrect based on the above. (Objective 1.3)

 

3. Given:

Image

 

What is the result?

A. 2

B. 4

C. An exception is thrown at runtime

D. Compilation fails due to an error on line 4

E. Compilation fails due to an error on line 5

F. Compilation fails due to an error on line 6

G. Compilation fails due to an error on line 7

Answer:

 

Image C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int[][] not an int[]. If line 7 was removed, the output would be 4.

 

Image A, B, D, E, F, and G are incorrect based on the above. (Objective 1.3)

 

4. Given:

Image

 

What is the result?

A. hi

B. hi hi

C. hi hi hi

D. Compilation fails

E. hi, followed by an exception

F. hi hi, followed by an exception

Answer:

 

Image F is correct. The m2 object's m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.

 

Image A, B, C, D, and E are incorrect based on the above. (Objective 7.3)

 

5. Given:

Image

 

What is the result?

A. true true

B. false true

C. true false

D. false false

E. Compilation fails

F. An exception is thrown at runtime

Answer:

 

Image A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing.

 

Image B, C, D, E, and F are incorrect based on the above. (Objective 7.3)

 

6. Given:

Image

 

What is the result?

A. pre b1 b2 r3 r2 hawk

B. pre b2 b1 r2 r3 hawk

C. pre b2 b1 r2 r3 hawk r1 r4

D. r1 r4 pre b1 b2 r3 r2 hawk

E. r1 r4 pre b2 b1 r2 r3 hawk

F. pre r1 r4 b1 b2 r3 r2 hawk

G. pre r1 r4 b2 b1 r2 r3 hawk

H. The order of output cannot be predicted

I. Compilation fails

Answer:

 

Image D is correct. Static init blocks are executed at class loading time, instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type occur in a class, they run in order, from the top down.

 

Image A, B, C, E, F, G, H, and I are incorrect based on the above. Note: you'll probably never see this many choices on the real exam! (Objective 1.3)

 

7. Given:

Image

 

Which are true? (Choose all that apply.)

A. The output could contain 30

B. The output could contain @bf73fa

C. The output could contain DIAMONDS

D. Compilation fails due to an error on line 6

E. Compilation fails due to an error on line 7

F. Compilation fails due to an error on line 8

G. Compilation fails due to an error on line 9

H. Compilation fails due to an error within lines 12 to 14

Answer:

 

Image A and B are correct. The code compiles and runs without exception. The values() method returns an array reference, not the contents of the enum, so DIAMONDS is never printed.

 

Image C, D, E, F, G, and H are incorrect based on the above. (Objective 1.3)

 

8. Given:

Image

 

What is the result?

A. 5 7

B. 5 8

C. 8 7

D. 8 8

E. Compilation fails

F. An exception is thrown at runtime

Answer:

 

Image E is correct. The parameter declared on line 9 is valid (although ugly), but the variable name ouch cannot be declared again on line 11 in the same scope as the declaration on line 9.

 

Image A, B, C, D, and F are incorrect based on the above. (Objective 1.3)

 

9. Given:

Image

 

What is the result?

A. 212

B. 232

C. 234

D. 312

E. 332

F. 334

G. Compilation fails

Answer:

 

Image A is correct. It's legal to autobox and then widen. The first call to doStuff() boxes the int to an Integer then passes two objects. The second call cannot widen and then box (making the Long method unusable), so it boxes the int to an Integer. As always, a var-args method will be chosen only if no non-var-arg method is possible. The third call is passing two objects—they are of type 'short array.'

 

Image B, C, D, E, F, and G are incorrect based on the above. (Objective 3.1)

 

10. Given:

Image

 

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?

A. Three objects were created

B. Four objects were created

C. Five objects were created

D. Zero objects are eligible for GC

E. One object is eligible for GC

F. Two objects are eligible for GC

G. Three objects are eligible for GC

Answer:

 

Image C and F are correct. da refers to an object of type "Dozens array," and each Dozens object that is created comes with its own "int array" object. When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.

 

Image A, B, D, E, and G are incorrect based on the above. (Objective 7.4)

 

11. Given:

Image

 

When line 16 is reached, how many objects will be eligible for garbage collection?

A. 0

B. 1

C. 2

D. 3

E. 4

F. 5

Answer:

 

Image B is correct. It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta object through the static variable a2.b1—because it's static.

 

Image A, C, D, E, and F are incorrect based on the above. (Objective 7.4)

 

12. Given:

Image

 

What is the result?

A. 4 4

B. 5 4

C. 6 4

D. 4 5

E. 5 5

F. Compilation fails

Answer:

 

Image A is correct. Although main()'s b1 is a different reference variable than go() 's b1, they refer to the same Box object.

 

Image B, C, D, E, and F are incorrect based on the above. (Objective 7.3)

 

13. Given:

Image

 

What is the result?

A. 2

B. 3

C. 4

D. 5

E. Compilation fails

F. An exception is thrown at runtime

Answer:

 

Image E is correct. In go1() the local variable x is not initialized.

 

Image A, B, C, D, and F are incorrect based on the above. (Objective 1.3)

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.215.1