Java Sample Questions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

MULTIPLE CHOICE.

Choose the one alternative that best completes the statement or answers the
question.

1.What is the name of the method used to start a thread execution?


A.init();
C.run();

B.start();
D.resume();

2. Which two are valid constructors for Thread?


1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority)
A.1 and 3
C.1 and 2

B.2 and 4
D.2 and 5

3. Which three are methods of the Object class?


1. notify();
2. notifyAll();
3. isInterrupted();
4. synchronized();
5. interrupt();
6. wait(long msecs);
7. sleep(long msecs);
8. yield();
A.1, 2, 4
C.1, 2, 6

B.2, 4, 5
D.2, 3, 4

4.
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}

Which of the following line of code is suitable to start a thread ?


A.Thread t = new Thread(X);
B.Thread t = new Thread(X); t.start();
C.X run = new X(); Thread t = new Thread(run); t.start();
D.Thread t = new Thread(); x.run();
5. Which cannot directly cause a thread to stop executing?
A.Calling the SetPriority() method on a Thread object.
B.Calling the wait() method on an object.
C.Calling notify() method on an object.
D.Calling read() method on an InputStream object.

6. Which two of the following methods are defined in class Thread?


1. start()
2. wait()
3. notify()
4. run()
5. terminate()
A.1 and 4
C.3 and 4
7.

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

8. Which of the following will directly stop the execution of a Thread?


A.wait()
C.notifyall()

B.notify()
D.exits synchronized code

9. Which method must be defined by a class implementing the java.lang.Runnable


interface?
A.void run()
C.public void start()

B.public void run()


D.void run(int priority)

10. Which will contain the body of the thread?


A.run();
C.stop();

B.start();
D.main();

11. What is the numerical range of char?


A.0 to 32767
C.-256 to 255

B.0 to 65535
D.-32768 to 32767

12. Which of the following are Java reserved words?


1. run
2. import
3. default
4. implement
A.1 and 2
B.2 and 3
C.3 and 4
D.2 and 4
13.Which is a valid declarations of a String?
A.String s1 = null;
B.String s2 = 'null';
C.String s3 = (String) 'abc';
D.String s4 = (String) '\ufeed';
14. What is the numerical range of a char?
A.-128 to 127
C.0 to 32767
15.
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */

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

17.What allows the programmer to destroy an object x?


A.x.delete()
B.x.finalize()
C.Runtime.getRuntime().gc()
D.Only the garbage collection system can destroy an object.

18.Which is true about an anonymous inner class?


A.It can extend exactly one class and implement exactly one interface.
B.It can extend exactly one class and can implement multiple interfaces.
C.It can extend exactly one class or implement exactly one interface.
D.It can implement multiple interfaces regardless of whether it also extends a class.

19.class Boo
{
Boo(String s) { }
Boo() { }
}

class Bar extends Boo


{
Bar() { }
Bar(String s) {super(s);}
void zoo()
{
// insert code here
}
}

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) { };

20. Which is true about a method-local inner class?


A.It must be marked final.
B.It can be marked abstract.
C.It can be marked public.
D.It can be marked static.
21. Which statement is true about a static nested class?
A.
B.
C.
D.
22.Which constructs an
anonymous inner class
instance?
Runnable r = new
Runnable() { };
Runnable r = new
B. Runnable(public void
run() { });
Runnable r = new
C. Runnable { public void
run(){}};
System.out.println(new
D.Runnable() {public
void run() { }});
A.

You must have a reference to an instance of the enclosing class


in order to instantiate it.
It does not have access to nonstatic members of the enclosing
class.
It's variables and methods must be static.
It must extend the enclosing class.

23.
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}

which statement, inserted at line 10, creates an instance of Bar?


A.Foo.Bar b = new Foo.Bar();
B.Foo.Bar b = f.new Bar();
C.Bar b = new f.Bar();
D.Bar b = f.new Bar();
24.
public class MyOuter
{
public static class MyInner
{
public static void foo() { }
}
}

which statement, if placed in a class other than MyOuter or MyInner, instantiates an


instance of the nested class?
A.MyOuter.MyInner m = new MyOuter.MyInner();
B.MyOuter.MyInner mi = new MyInner();
C.

MyOuter m = new MyOuter();


MyOuter.MyInner mi = m.new MyOuter.MyInner();

D.MyInner mi = new MyOuter.MyInner();


25.

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

You might also like