1Z0-851 | 10 Tips For Latest 1Z0-851 exam


Q31. Given:

1. public class LineUp {

2. public static void main(String[] args) {

3. double d = 12.345;

4. // insert code here

5. }

6. }

Which code fragment, inserted at line 4, produces the output | 12.345|?

A. System.out.printf("|%7d| n", d);

B. System.out.printf("|%7f| n", d);

C. System.out.printf("|%3.7d| n", d);

D. System.out.printf("|%3.7f| n", d);

E. System.out.printf("|%7.3d| n", d);

F. System.out.printf("|%7.3f| n", d);

Answer: F

Q32. Given a method that must ensure that its parameter is not null:

11. public void someMethod(Object value) {

12. // check for null value ...

20. System.out.println(value.getClass());

21. }

What, inserted at line 12, is the appropriate way to handle a null value?

A. assert value == null;

B. assert value != null, "value is null";

C. if (value == null) {

throw new AssertionException("value is null");

}

D. if (value == null) {

throw new IllegalArgumentException("value is null"); }

Answer: D

Q33. Given:

1. public class Threads2 implements Runnable {

2.

3. public void run() {

4. System.out.println("run.");

5. throw new RuntimeException("Problem");

6. }

7. public static void main(String[] args) {

8. Thread t = new Thread(new Threads2());

9. t.start();

10. System.out.println("End of method.");

11. }

12. }

Which two can be results? (Choose two.)

A. java.lang.RuntimeException: Problem

B. run.

java.lang.RuntimeException: Problem

C. End of method.

java.lang.RuntimeException: Problem

D. End of method.

run.

java.lang.RuntimeException: Problem

E. run.

java.lang.RuntimeException: Problem

End of method.

Answer: DE

Q34. Given:

3. interface Fish { }

4. class Perch implements Fish { }

5. class Walleye extends Perch { }

6. class Bluegill { }

7. public class Fisherman {

8. public static void main(String[] args) {

9. Fish f = new Walleye();

10. Walleye w = new Walleye();

11. Bluegill b = new Bluegill();

12. if(f instanceof Perch) System.out.print("f-p ");

13. if(w instanceof Fish) System.out.print("w-f ");

14. if(b instanceof Fish) System.out.print("b-f ");

15. }

16. }

What is the result?

A. w-f

B. f-p w-f

C. w-f b-f

D. f-p w-f b-f

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Q35. Given:

1. class ClassA {

2. public int numberOfInstances;

3. protected ClassA(int numberOfInstances) {

4. this.numberOfInstances = numberOfInstances;

5. }

6. }

7. public class ExtendedA extends ClassA {

8. private ExtendedA(int numberOfInstances) {

9. super(numberOfInstances);

10. }

11. public static void main(String[] args) {

12. ExtendedA ext = new ExtendedA(420);

13. System.out.print(ext.numberOfInstances);

14. }

15. }

Which statement is true?

A. 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

Q36. Given:

11. public void genNumbers() {

12. ArrayList numbers = new ArrayList();

13. for (int i=0; i<10; i++) {

14. int value = i * ((int) Math.random());

15. Integer intObj = new Integer(value);

16. numbers.add(intObj);

17. }

18. System.out.println(numbers);

19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for

garbage collection?

A. Line 16

B. Line 17

C. Line 18

D. Line 19

E. The object is NOT a candidate for garbage collection.

Answer: D

Q37. Given that Triangle implements Runnable, and:

31. void go() throws Exception {

32. Thread t = new Thread(new Triangle());

33. t.start();

34. for(int x = 1; x < 100000; x++) {

35. //insert code here

36. if(x%100 == 0) System.out.print("g");

37. } }

38. public void run() {

39. try {

40. for(int x = 1; x < 100000; x++) {

41. // insert the same code here

42. if(x%100 == 0) System.out.print("t");

43. }

44. } catch (Exception e) { }

45. }

Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to

temporarily pause and allow the other thread to execute? (Choose two.)

A. Thread.wait();

B. Thread.join();

C. Thread.yield();

D. Thread.sleep(1);

E. Thread.notify();

Answer: CD

Q38. Given:

21. abstract class C1 {

22. public C1() { System.out.print(1); }

23. }

24. class C2 extends C1 {

25. public C2() { System.out.print(2); }

26. }

27. class C3 extends C2 {

28. public C3() { System.out.println(3); }

29. }

30. public class Ctest {

31. public static void main(String[] a) { new C3(); }

32. }

What is the result?

A. 3

B. 23

C. 32

D. 123

E. 321

F. Compilation fails.

G. An exception is thrown at runtime.

Answer: D

Q39. 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

Q40. 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