nerdexam
Oracle

1Z0-809 · Question #257

1Z0-809 Question #257: Real Exam Question with Answer & Explanation

The correct answer is A. Line 3: public synchronized void run() {. See the full explanation below for the reasoning.

Question

Given:
1. class MyClass implements Runnable {
2. public int value;
3. public void run() {
4. while (value < 100) {
5. value++;
6. System.out.println("value: " + value);
7. }
8. }
9. }
10. public class TestThread {
11. public static void main(String[] args) {
12. MyClass mc = new MyClass();
13. Thread a = new Thread(mc);
14. a.start();
15. Thread b = new Thread(mc);
16. b.start();
17. }
18. }
What change should you make to guarantee a single order of execution (printed values 1 -100 in order)?

Options

  • ALine 3: public synchronized void run() {
  • BLine 1: class MyClass extends Thread {
  • CLine 2: public volatile int value;
  • DLine 2: public synchronized int value;

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice