The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abhilash Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abhilash Mock Exam" Watch "Abhilash Mock Exam" New topic

Abhilash Mock Exam

Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

hello friends,
This is from Abhilash's mock exam
Question 39.

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());
setLayout(new BorderLayout());
public static void main(String args[])
TestFrame tf = new TestFrame();

Attemping to compile and run the above code

1.Will cause a compilation error - a Layout cannot be set after a component has been added with a preset
Layout Manager.
2.Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset
Layout Manager.
3.Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and
occupies the whole screen.
4.Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other
component added in future will follow the rules of the BorderLayout Manager.
5.Will compile and run cleanly, but no component is visible.
6.Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Button Label Position

Center Center

North North

South South

East East

West West
The given answer is 5
Can somebody tell me how?
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi karthik,
The answer is right. u can run it and see.
now the explanation.
actually in case of borderlayout u need to first set the layout and then add the components. if u do the reverse then u don't see anything.
while u can test that if u add the components first and then set the layout later to FlowLayout/GridLayout then u don't see any abnormal behaviour. but if u try to change the layout to borderlayout after adding components then u see nothing.
the explanation to this may lie in the fact that the borderlayout extends from class LayoutManager2 while FlowLayout/GridLayout extend from class LayoutManager.

I agree. Here's the link:
subject: Abhilash Mock Exam
It's not a secret anymore!