Oracle
1Z0-851 · Question #70
Given: public class PingPong implements Runnable { synchronized void hit(long n) { for(int i = 1; i < 3; i++) System.out.print(n + "-" + i + " "); } public static void main(String[] args) { new…
The correct answer is C. The output could be 8-1 7-1 7-2 8-2 D. The output could be 8-1 8-2 7-1 7-2. See the full explanation below for the reasoning.
Question
Given: public class PingPong implements Runnable { synchronized void hit(long n) { for(int i = 1; i < 3; i++) System.out.print(n + "-" + i + " "); } public static void main(String[] args) { new Thread(new PingPong()).start(); new Thread(new PingPong()).start(); } public void run() { hit(Thread.currentThread().getId()); } } Which two statements are true? (Choose two.)
Options
- AThe output could be 8-1 7-2 8-2 7-1
- BThe output could be 7-1 7-2 8-1 6-1
- CThe output could be 8-1 7-1 7-2 8-2
- DThe output could be 8-1 8-2 7-1 7-2
How the community answered
(22 responses)- A9% (2)
- B9% (2)
- C82% (18)
Community Discussion
No community discussion yet for this question.