Question
public class WaitTest
{
public static void main(String [] args)
{
System.out.print("1 ");
synchronized(args)
{
System.out.print("2 ");
try
{
args.wait(); /* Line 11 */
}
catch(InterruptedException e){ }
}
System.out.print("3 ");
}
}
What will be the output of the program?
public class WaitTest
{
public static void main(String [] args)
{
System.out.print("1 ");
synchronized(args)
{
System.out.print("2 ");
try
{
args.wait(); /* Line 11 */
}
catch(InterruptedException e){ }
}
System.out.print("3 ");
}
}
Answer: Option D
Was this answer helpful ?
1 and 2 will be printed, but there will be no return from the wait call because no other thread
will notify the main thread, so 3 will never be printed. The program is essentially frozen at line
11.
A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be
dealt with explicitly.
B and C are incorrect; 3 will never be printed, since this program will never terminate because
it will wait forever.
Was this answer helpful ?
More Questions on This Topic :
Question 10.
Which will contain the body of the thread?
....
Submit Solution