6)
What will be the output of following program?
class ThreadEx extends Thread
{
public void run()
{
Thread.sleep(100);
System.out.println("Hello");
}
public static void main(String args[])
{
ThreadEx T1=new ThreadEx();
T1.start();
}
}
- Hello [Will print instant]
- Hello [Will print after 100 milliseconds]
- HelloHelloHello... Infinite times
- Error
Correct Answer: 4
Error
unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Thread.sleep(100);
Statement Thread.sleep(100); should be written in try catch block.
7)
What will be the output of following program?
class ThreadEx extends Thread
{
public void run()
{
System.out.print("Hello...");
}
public static void main(String args[])
{
ThreadEx T1=new ThreadEx();
T1.start();
T1.stop();
T1.start();
}
}
- Run Time Exception
- Compile Time Error
- Hello...Hello...
- Hello...
Correct Answer: 1
Run Time Exception
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start
Thread cannot be started twice.