Questions# 161-180
Question 161)
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 162)
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 163)
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 164)
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 165)
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 166)
A class which has all its constructors declared as private
- Cannot be instantiated by any other class.
- Cannot be extended.
- Both i and ii.
- has to be declared final.
Question 167)
The GridBagConstraints Class
- Is serializable.
- Is cloneable.
- belongs to the java.awt package.
- extends Object.
Question 168)
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 169)
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 170)
Which of the following methods of the java.io.File class throws a checked
Exceptions -
- getCanonicalPath()
- getCanonicalFile()
- getAbsolutePath()
- getAbsoluteFile()
- createTempFile()
- createNewFile()
- mkdir()
- mkdirs()
- toURL()
Question 171)
Read the code below carefully.
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces
- True
- False
Question 172)
Read the code below carefully.
if( "String".startsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces
- True
- False
Question 173)
Read the code below carefully.
public class TestClass
{
public static void
main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
//lots of code
}
}
}
Is the line
marked "lots of code" ever reached?
Question 174)
Read the code below carefully.
public class NiceThreads implements Runnable
{
public void run()
{
while(true)
{
}
}
public static void main(String args[])
{
NiceThreads nt1
= new NiceThreads();
NiceThreads nt2
= new NiceThreads();
NiceThreads nt3
= new NiceThreads();
nt1.run();
nt2.run();
nt3.run();
}
}
- The code does not compile - "nt2.run() is never reached"
- The code compiles and runs 3 non ending non demon threads.
- The code compiles but runs only 1 non ending, non demon thread.
Question 175)
When does the JVM exit?
- After the main method returns.
- After all the non demons threads created by the application complete.
- After all the demon threads created by the application complete
- When a thread executes System.exit();
- When an uncaught exception is thrown in a non demon thread.
- When an uncaught exception is thrown in a demon thread.
Question 176)
Read the following code, which is a part of a synchronized
method of a monitor.
public synchronized void someMethod()
{
//lots of code
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
//do some crap here.
}
//more and more
code here
}
- The code causes compilation error - sleep cannot be called
inside synchronized methods.
- The code causes compilation error - sleep is not a static method of
java.lang.Thread
- The Thread sleeps for at least 500 milliseconds in this method if not
interrupted.
- When the thread "goes to sleep" it releases the lock on the object.
- The "sleeping" Threads always have the lock on the Object.
Question 177)
The no-argument constructor provided by the compiler
when no constructor is explicitly provided in the code
- is always public
- is always "friendly"
- always defaults to the access modifier provided for the class.
- depends on the compilation options of javac
Question 178)
Which of the following is the direct base class of
java.awt.AWTEvent.
- java.lang.Object.
- java.util.EventObect
Question 179)
Interface methods can be declared with the following
modifiers
- public
- none (i.e., no access modifier).
- private.
- static
- native
- synchronized.
Question 180)
Which of the following are true about the class defined
inside an interface
- it is not possible in the java Laungage.
- The class is always public.
- The class is always static.
- the class methods cannot call the methods declared in the interface.
- the class methods can call only the static methods declared in the
interface.
Back
Answers
Next
Index
Home