• 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

Some questions!!!

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i have listed the following doubts ..... pls answer each one in detail.
(Q-1) Choose the correct one about thread:

A. The threads from one class ends at the same time.
B. when JVM exists the main method, it will stop only after all the threads are stopped.
C. if there are multiple threads, reading or writing of data of class is inconsitent.
D. programmer has to write a special program for garbagecollection in multiple threads.

My answer is C but thei answer is B & C. My doubt is that even after JVM exits I think there are daemon(system) threads still running.
(Q-2) Whenever I create a frame it does not close by clicking on the "X" on the top right corner of the window. Why is this happening???
(Q-3) What might cause the current thread to stop executing.

A. An InterruptedException is thrown.
B. The thread executes a wait() call.
C. The thread constructs a new Thread.
D. A thread of higher priority becomes ready.
E. The thread executes a waitforID() call on a MediaTracker.
My answer was B, D & E. their answer was A, B, D & E. I think that an InterruptedException is always thrown on a sleeping thread which is not currently executing so how does it stop execution???
(Q-4) Given the following incomplete method.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}


You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true. Which changes achieve this?

A. Add at line 2: IOException e;
B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type] Exception might be thrown.
My answer was C. Their answer was D & E. I have not understood the reason why to throw the exception on line 6 and do we have to compulsarily declare throws clause in the method declaration if we throw an Exception in the method body.
(Q-5)What kind of reader do you use to handle ASCII code?

A. BufferedReader
B. ByteArrayReader
C. PrintWriter
D. InputStreamReader
E. FilterReader
Their answer is D. But why not A & B because they are also readers and all readers always handle ascii code. Pls clarify..i may be wrong.
(Q-6) Does the interpreter stop when the main method stops?
Their answer is NO. Please relate this question to option B of (Q-1).
Please answer these questions in detail...it would be a great help to me.

Sagar
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I can answer question 2 for you.
Frame doesn't automatically do anything when you click on the X at the top right. In order to get the X to close the Frame, you have to add a WindowListener and then handle the windowClosing event, using code like this:-
public void windowClosing (WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
The other way to get around this is to use the Swing component, JFrame instead - by default, a JFrame is hidden when you click on the X in the top right corner, which is different from closing the JFrame or closing the application. You can change this behaviour by using the setDefaultCloseOperation method of JFrame - for example, setting the DefaultCloseOperation to DISPOSE_ON_CLOSE will actually close and dispose of the JFrame.
Hope this answers one of your questions,
Kathy
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SAGAR,
You have posted too many questions. I will explain only one in detail..

Q-1
ANS:

B. when JVM exists the main method, it will stop only after
all the threads are stopped.
C. if there are multiple threads, reading or writing of data
of class is inconsitent.
You have mentioned that your choice is only (C)
I am explaining you why the ans: is also (B)
Your doubt is that even after JVM exits I think there are
daemon(system) threads still running. Your doubt is NOT CORRECT.
When the JVM exits from main method, it checks that whether any thread is still running, If no thread is running(assume only daemon threads are running)JVM will kill all daemon threads and exits. Here the daemon threads killed by JVM are the daemon threads created by threads running inside main().
Do not come to conclusion that all dameon threads in windows are killed
when your program terminates, that will happen only
when you shutdown your system.
You run this example below:
class Thread1 implements Runnable
{
public void run()
{
while(true)
{
System.out.println("in Run");
try{Thread.sleep(2000);}
catch(Exception e){}
}
}
}
public class test
{
public static void main(String argv[])
{
System.out.println("Entering main");

new Thread(new Thread1()).start();

System.out.println("Leaving main");

}

}
You will see that still JVM is running even though main thread terminates
WHY ??. Because in side main() one thread was created and is still running
How can JVM kill that thread, had it been a Dameon thread it would have been killed intantly.
Now you repalace the following inside main()
"new Thread(new Thread1()).start();"
with
Thread t= new Thread(new Thread1()) ;
t.setDaemon(true);
t.start();

You will see the Daemon thread is killed when JVM exits main(),
ofcourse it was able to run one statement before it was killed.

solaiappan
Happy Diwali to all

 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll take one.
Question 6, does the interpreter stop when main stops. The answer is NO.
This is because there may be user threads still hanging on and they will continue to go until they ended. Once all user threads have stopped, then the JVM will stop. So it is not dependant on main, but dependant on all user threads.
As for question 4, I am interested in other replies. I agree with you on that one. I think that the answer is C. If you want to throw a new exception if the method returns true, then the first block is going to get executed. So you would need to throw the method at line 4 and not 6. Modify the method so it throws exception isn't going to throw a new IOException so I don't know why E would be correct.
[This message has been edited by bill bozeman (edited October 25, 2000).]
 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi BILL & SAGAR,
For question 4, (E) is also correct.
I thing, my following explation is correct to the best of my
knowledge.

You want to make this method throw an IOException .
For (E) you can modi fy the mehod declaration write like this
public void method() throws IOException
{
if (someTestFails())
{

}

}
(But in the above code no code is generating any IOException so that it can be thrown!!!)
Another example to support my ans:
public class Test
{
public static void main (String[] args) throws IOException
{
DataOutputStream dos = new DataOutputStream (new
FileOutputStream("Test.txt"));
os.writeBoolean(true);
.........
}
}
Since the above I/O operation throws IOException the main() is modified. OR we can use our usual try-catch block to trap the
IOException.(In this code I/O operation throw
IOException.)
solaiappan


[This message has been edited by P SOLAIAPPAN (edited October 25, 2000).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Sagar,
For Q4: My answers would be C and E
First declare that the method throws an Exception(E) and in the method at Line 4 insert the statement which actually throws an exception(C)
For Q5: Regarding handling ascii code, I would assume he is asking about converting from ascii to bytes and vice versa in which case only "InputStreamReader" and "OutputStreamWriter" can be used.
Corrections are welcome
Regards
Preethi Chaloori
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sagar,
For Q3, IMHO, A is also correct. For instance, if another thread of higher priority receives this InterruptedException, it will be awaken and the current thread will be stopped.
Cheers,
Beno�t
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic