• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Test Question

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Thx
--------------------------------------------------------------------------------
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
tt.start();

Thread tt1 = new Thread(t);
tt1.start();
}
}
class th implements Runnable
{
int i = 0;
public void run()
{
i++;
if(i==1)
loop();
else
loop1();
}

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--)
System.out.println(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.
Regards,
Hemant
 
reply
    Bookmark Topic Watch Topic
  • New Topic