Java Quiz
Question 41.
Read the following piece of code carefully.
import java.awt.*;
public class TestFrame extends Frame
{
Button firstOne = new Button("One");
Button secondOne = new Button("Two");
public TestFrame()
{
add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
TestFrame
tf = new TestFrame();
}
}
An Attempt to compile and run the above piece
of code
-
Causes compilation error - a component cannot be added to region which
is already occupied by another component.
-
Causes Runtime Exception - a component cannot be added to region which
is already occupied by another component.
-
Neither i or ii. The Frame comes up and only the button with label "Two"
occupies the entire North region of the Frame.
-
Addition of secondOne causes firstOne to be removed from the container.
-
Addition of the secondOne causes the firstOne to be hidden in the container.
Question 42.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
Button
one = new Button("One");
Button
two = new Button("Two");
Button
three = new Button("Three");
setLayout(new
FlowLayout());
add(one);
add(two);
add(three);
two.setVisible(false);
setSize(1000,1000);
setVisible(true);
validate();
}
public static void main(String args[])
{
TestFrame
tf = new TestFrame();
}
}
-
If the above code runs, the buttons - one and three are laid out in a single
row from left to right with a gap in between .
-
If the above code runs, the buttons - one and three are laid out in a single
row from left to right with no gap in between.
-
Code does not compile - a component can not be hidden after being added
to a container.
-
Code gets compiled successfully but throws runtime Exception - a
component can not be hidden after being added to a container.
Question 43.
Read the code below carefully.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new
GridLayout(2,1));
for(int
i = 1 ; i <= 4 ;++i)
{
add(new Button(Integer.toString(i)));
}
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame
tf = new TestFrame();
}
}
-
The code above will not compile - The number of components added
is more than the magnitude of row * columns of the Grid Layout Manager.
-
The code will throw a runtime Exception - The number of components
added is more than the magnitude of row * columns of the Grid Layout Manager.
-
The code will compile cleanly and when run Four buttons are visible in
2 rows and 2 columns.
-
The code will compile and when run, Four buttons are seen in a single Column.
Question 44.
Read the code below carefully.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new
GridLayout());
for(int
i = 1 ; i <= 4 ;++i)
{
add(new Button(Integer.toString(i)));
}
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame
tf = new TestFrame();
}
}
-
The code will not compile - the grid layout does not have a no-argument
constructor..
-
The code compiles and when run all the buttons are seen in a single column.
-
The code compiles and when run all the buttons are seen in a singe row.
-
The code compiles and when run all button are added one on top or another
and only the last one added is visible.
-
The code compiles , but throws a runtime Exception when components are
added.
Question 45.
Which of the following statements are true
about setLayout() method in java.awt.ScrollPane
-
Does nothing.
-
Throws UnsupportedMethodException when called.
-
It is not overriden in java.awt.ScrollPane.
-
Sets the layout to the specified Layout Manager.
Question 46.
An Outer class which has all its constructors declared as
private
-
Cannot be instantiated by any other Outer class.
-
Cannot be extended.
-
Both i and ii.
-
has to be declared final.
Question 47.
The GridBagConstraints Class
-
Is serializable.
-
Is cloneable.
-
belongs to the java.awt package.
-
extends Object.
Question 48.
Read the following code carefully
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
CheckboxGroup chg = null;
Checkbox ch = new Checkbox("Test",true,chg);
setLayout(new FlowLayout());
add(ch);
pack();
setVisible(true);
}
public static void main(String
args[])
{
TestFrame tf = new TestFrame();
}
}
An Attempt to compile and run the above code
-
will cause a compilation error as the checkbox group is null in the constructor.
-
will compile successfully but throws a runtime exception because the checkbox
group is null in the constructor of the check box
-
will compile and run successfully. The checkbox appears as a single radio
button which is always true.
-
will compile and run successfully. The checkbox bears its original appearence
and does not appear as a radio button.
Question 49.
Read the following code carefully
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
CheckboxGroup chg = new CheckboxGroup();
Checkbox ch = new Checkbox("Test",true,chg);
setLayout(new FlowLayout());
add(ch);
pack();
setVisible(true);
}
public static void main(String
args[])
{
TestFrame tf = new TestFrame();
}
}
An Attempt to compile and run the above code
-
will cause a compilation error as the checkbox group contains only one
checkbox.
-
will compile successfully but throws a runtime exception because the checkbox
group contains only one checkbox.
-
will compile and run successfully. The checkbox appears as a single radio
button which is always true.
-
will compile and run successfull. The checkbox bears its original appearence
and does not appear as a radio button.
Question 50.
Which of the following methods of the java.io.File class throws a checked
Exceptions -
-
getCanonicalPath()
-
getCanonicalFile()
-
getAbsolutePath()
-
getAbsoluteFile()
-
createTempFile()
-
createNewFile()
-
mkdir()
-
mkdirs()
-
toURL()
If
you have any concerns regarding any of the questions in this page, please
mail me NOW!