Java Sample Questions
Java Sample Questions
Java Sample Questions
Choose the one alternative that best completes the statement or answers the
question.
B.start();
D.resume();
B.2 and 4
D.2 and 5
B.2, 4, 5
D.2, 3, 4
4.
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
B.2 and 3
D.2 and 4
Which three guarantee that a thread will leave the running state?
1. yield()
2. wait()
3. notify()
4. notifyAll()
5. sleep(1000)
6. aLiveThread.join()
7. Thread.killThread()
A.1, 2 and 4
C.3, 4 and 7
B.2, 5 and 6
D.4, 5 and 7
B.notify()
D.exits synchronized code
B.start();
D.main();
B.0 to 65535
D.-32768 to 32767
B.-(215) to (215) 1
D.0 to 65535
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
When is the Float object, created in line 3, eligible for garbage collection?
A.just after line 5
B.just after line 6
C.just after line 7
D.just after line 8
16. class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
A.0
B.1
C.2
D.3
19.class Boo
{
Boo(String s) { }
Boo() { }
}
which one create an anonymous inner class from within class Bar?
A.Boo f = new Boo(24) { };
B.Boo f = new Bar() { };
C.Bar f = new Boo(String s) { };
D.Boo f = new Boo.Bar(String s) { };
23.
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
You want subclasses in any package to have access to members of a superclass. Which is
the most restrictive access that accomplishes this objective?
A.public
C.protected
B.private
D.transient
26) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are
available by
A) implementing interfaces.
B) creating aliases.
C) importing classes.
D) using public rather than protected or private modifiers.
E) overriding parent class methods.
27) In order to add a new item to a JComboBox once the JComboBox has been constructed, you would
A) use the addItem method.
B) have the user add the item through the GUI.
C) add the item as you would add a new value to an array, such as by doing jlist[num+1] = new String(...);.
D) use the add method.
E) destroy the current object and reconstruct it with the new item.
28) Consider a subclass of JFrame that implements MouseListener. Assume that the class has five instance
data, int x1, x2, y1, y2, and boolean inside. The four int values represent the two end-points of a box (x1,
y1 is the upper left hand point and x2, y2 is the lower right hand point). Which of the following properly
defines code that will determine whenever the mouse button is clicked if the mouse is currently inside this
box or not. If the mouse is inside the box, inside is set to true, otherwise it is set to false.
A) public void mouseMoved(MouseEvent me)
{
if(me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
B) public void mousePressed(MouseEvent me)
{
if(me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
C) public void mouseReleased(MouseEvent me)
{
if(me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) <= y1 && me.getY( ) >= y2)
inside = true;
else
inside = false;
}
D) public void mouseEntered(MouseEvent me)
{
if(me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
E) Requires both of the following methods:
public void mouseEntered(MouseEvent me)
{
inside = true;
}
public void mouseExited(MouseEvent me)
{
inside = false;
}
29) In order to determine the type that a polymorphic variable refers to, the decision is made by the
A) operating system when the program is loaded into memory.
B) Java run-time environment at run time.
C) compiler at compile time.
D) programmer at the time the program is written.
E) user at run time.
30) The relationship between a parent class and a child class is referred to as a(n) __________ relationship.
A) is-a
B) was-a
C) has-a
D) instance-of
E) alias