JAVA Programming :: Basic Concepts - Discussion
Home > JAVA Programming > Basic Concepts > MCQs Questions Discussion
8 / 64
What will happen when you try to compile and run this code?
public class TGo implements Runnable{
public static void main(String argv[]){
TGo tg = new TGo();
Thread t = new Thread(tg);
t.start();
}
public void run() {
while(true) {
Thread.currentThread().sleep(1000);
System.out.println("looping while");
}
}
}
public class TGo implements Runnable{
public static void main(String argv[]){
TGo tg = new TGo();
Thread t = new Thread(tg);
t.start();
}
public void run() {
while(true) {
Thread.currentThread().sleep(1000);
System.out.println("looping while");
}
}
}
ACompilation and no output
BCompilation and repeated output of "looping while"
CCompilation and single output of "looping while"
DCompile time error
Answer: Option (Login/Signup)
Show Explanation
The sleep() method is responsible for generating the exception, and hence should be declared under try-catch block.
Hence, correct option is D.
Asked In ::
TRICK
We will get a compile-time error in line no. 9, saying unreported exception InterruptedException; must be caught or declared to be thrown.
The sleep() method is responsible for generating the exception, and hence should be declared under try-catch block.
Hence, correct option is D.
Read Full Answer
Report Error
Please Login First Click Here