Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Test Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Test Question" Watch "Mock Test Question" New topic

Mock Test Question

Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
When does the JVM exit?
1)After the main method returns.
2)After all the non demons threads created by the application complete.
3)After all the demon threads created by the application complete
4)When a thread executes System.exit();
5)When an uncaught exception is thrown in a non demon thread.
6)When an uncaught exception is thrown in a demon thread.

The anwser is 2),4),5),6)
I understand 2),but I have doubt in 4).
I am not clear about 5) and 6)
Could anyone explain this for me?
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
well system.exit(0) does just that exits so thats done
an uncaught exception always stops the program since it can not go any further as you didnt tel it what to do
Hemant Bhaiya

Joined: Nov 01, 2000
Posts: 21
Hello Bin,
Only answers 2 and 4 are correct.
I have written a code to demonstrate this:
public class Wrap
public static void main(String[] args)
th t = new th();

Thread tt = new Thread(t);

Thread tt1 = new Thread(t);
class th implements Runnable
int i = 0;
public void run()

public void loop()

for(int i=0;i<500;i++)<br /> {<br /> if(i==150)<br /> //throw new NumberFormatException();<br /> System.exit(0);<br /> System.out.println(i);<br /> }<br /> <br /> }<br /> <br /> public void loop1()<br /> {<br /> System.out.println("In the second loop");<br /> <br /> for(int i=500;i>0;i--)

About 4 :As shown in the code,when you call System.exit(0) in any thread,the JVM exits (Check the output stops there).
About 5 & 6 : They are not correct,if an uncaught exception arises in any thread then only that thread throws the exceotion and stops processing but other thread are still working(To check this,in above code,comment the System.exit(0) line and uncomment the originally commented line).You will see an exception is raised and that thread stops processing but other thread still shows output.
Hope you are clear.
I agree. Here's the link:
subject: Mock Test Question
It's not a secret anymore!