Difficulty: Easy
Correct Answer: This code will not compile.
Explanation:
Introduction / Context:
The snippet uses Thread.currentThread().sleep(1000) to pause after printing each line. The key point is not whether sleep applies to the current thread (it does) but that sleep(long) throws InterruptedException, which must be handled or declared.
Given Data / Assumptions:
Concept / Approach:
Checked exceptions in Java must be caught or declared. Because sleep throws InterruptedException, the compiler requires a try/catch around the call or a throws clause in printAll (and in main if not handled there). Absent either, compilation fails regardless of whether a thread is explicitly created.
Step-by-Step Solution:
Verification / Alternative check:
Fix by writing: try { Thread.sleep(1000); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting that calling a static method through an expression does not change its checked-exception behavior.
Final Answer:
This code will not compile.
Discussion & Comments