Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some Interesting Qs

 
Rahul
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur reply, Ramesh
But what about Question 1 and Question 3
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic