aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some Interesting Qs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some Interesting Qs" Watch "Some Interesting Qs" New topic
Author

Some Interesting Qs

Rahul
Greenhorn

Joined: Sep 22, 2000
Posts: 6
Can somebody help me on this
import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
Attemping to compile and run the above code
causes no component to be visible.
Why - ?

And more
public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Why does this give an Output = 0

1 more..........
Assume that th is an instance holding a thread object. th.start() causes the thread to start running and eventually complete its execution. The object reference by th is not accessable any more and is garbage collected when the garbage collecter runs.

True
False
Ans given is False
Please Explain why

Njoy
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Rahul,
(2) The seemingly strange behavior is something to do with your initialization of i with the method.
The procedure of constructing an object is (1) construct static variables in the order they appear, (2) execute static initializers in the order they appear and (3) then construct instance variables. In this case there are no static variables/initializers so we can keep them aside.
When the object is being constructed, both i and j are created and assigned a default value(0). then any assignments take place. So, when i is initialized with giveMej(), both i and j are zero. You can verify this by inserting print messages. It is natural for the method to return current value of j (0) and that is what assigned to i. Later j is initialized to 10.
Rahul
Greenhorn

Joined: Sep 22, 2000
Posts: 6
Thanks for ur reply, Ramesh
But what about Question 1 and Question 3
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Rahul ...
In (1) the components are added to FlowLayout. You then create a brand new BorderLayout and assign it to Frame. The BorderLayout is the one that is displayed, but, it has no components associated with it ... result, blank screen.
Sorry, can't help you on threads; still reviewing them.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
Hi!
If you add the buttons after assigning the layout as BorderLayout ,it will work fine ,displaying only the Center button on the screen.It was not displaying as per your code 'coz u have not added buttons to the BorderLayout.It does not recognize the buttons added earlier in another Layout.

Ira

------------------
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
Sorry Jane,saw your post after I had posted my reply.
As per your 3rd question,IMO once a thread completes its execution reaches the dead state.The JVM on finding that the object reference th does not point to anything ,garbage collects it.
Please correct me if I am wrong.
Ira
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some Interesting Qs