Code: Select all
class A extends Thread
{
public void run()
{
System.out.println("Starting loop");
while(true){}; //1
System.out.println("Ending loop");
}
}
public class TestClass
{
public static void main(String args[]) throws Exception
{
A a = new A();
a.start();
Thread.sleep(1000);
a.interrupt();
}
}
This may be true but I selected this answer because I understand that a call to sleep() should be placed in a Try/Catch block, which this code does not have. Can you please confirm this?This will not compile.
Because System.out.println("Ending loop"); is unreachable.
The other correct answer is:
According to the SCJP Study Guide by Sierra and Bates, interrupt() and isinterrupted() are not included in a list of methods that one needs to know for this exam.It will run and end cleanly if //1 is replaced with while(!isInterrupted()) { };