nerdexam
Oracle

1Z0-851 · Question #55

1Z0-851 Question #55: Real Exam Question with Answer & Explanation

Sign in or unlock 1Z0-851 to reveal the answer and full explanation for question #55. The question stem and answer options stay visible for context.

Question

Given: 1. public class TestFive { 2. private int x; 3. 4. public void foo() { 5. int current = x; 6. x = current + 1; 7. } 8. 9. public void go() { 10. for(int i = 0; i < 5; i++) { 11. new Thread() { 12. public void run() { 13. foo(); 14. System.out.print(x + ", "); 15. } 16. }.start(); 17. } 18. } 19.} Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)

Options

  • Amove the line 12 print statement into the foo() method
  • Bchange line 7 to public synchronized void go() {
  • Cchange the variable declaration on line 2 to private volatile int x;
  • Dwrap the code inside the foo() method with a synchronized( this ) block
  • Ewrap the for loop code inside the go() method with a synchronized block synchronized(this)

Unlock 1Z0-851 to see the answer

You've previewed enough free 1Z0-851 questions. Unlock 1Z0-851 for full answers, explanations, the timed quiz mode, progress tracking, and the master PDF. Question stem and options stay visible so you can still see what's on the exam.

Full 1Z0-851 Practice