1.Which of these is the correct declaration for main?

A.public static void main()

B.public void static main(String args[ ])

C.public static void main(String args)

D.public static void main(String arg[ ])

E.public static void main(String[ ] args)

Answer:D,E

In D, we declare arg to be an array of String types. In E, we declare args to be of type ‘array of String types’, which is the same.

2.What will happen when you attempt to compile and run the following code?

import java.awt.*;

import java.awt.event.*;

public class MClick extends Frame implements MouseListener{

public static void main(String arg[]){

MClick m = new MClick();

}

MClick() {

this.addMouseListener(this);

}

public void mouseClicked (MouseEvent me){

System.out.println("Event occured");

}

}

A.Compile time error

B.Run time Error

C.Compile and at runtime "Event occurred" will be the output

D.Compile and at runtime no action is performed

Answer: A

Because this code uses the Event Listener, bodies must be created for each method in the Listener. It is an abstract class and cannot be instantiated.

3.What code can you write to ensure that the int variables are garbage collected at a particular point in this code?

public class Rub{

int i = 1;

int j = 2;

int k = 3;

public static void main(String args[ ]){

Rub r = new Rub();

r.amethod();

}

public void amethod (){

i = 0;

j = 0;

k = 0;

}

}

A.System.gc();

B.System.free();

C.Set the value of each int to null.

D.None of the above

Answer: D

You can only suggest garbage collection, therefore you cannot be certain that it will run at any particular point in your code.

4.Given the following main method in a class Bike and a command line of

Java Bike one two

what will be the output?

public static void main (String bikes[ ]){

System.out.println(bikes[1]);

}

A.None of these options

B.bikes

C.one two

D.one

E.two

Answer: E

Second element in the bikes array is printed.

5.Which of the following are Java key words?

A.double

B.Switch

C.then

D.instanceof

Answer: A, D

6.What will happen when you attempt to compile and run the following code?

public class MyParm{

public static void main (String arg[ ]){

String s1 = "One";

String s2 = "One";

if (s1.equals(s2)) {

System.out.println("Strings are equal");

boolean b1 = true;

boolean b2 = true;

}

if (b1.equals(b2)) {

System.out.println("true");

}

}

}

A.Compile time error

B.Run time error

C.Only "Strings are equal"

D."Strings are equal" followed by "true"

Answer: A

The line b1.equals(b2) will cause an error because b1 is a primitive and primitives do not have any methods.

7.What will happen when you attempt to compile and run the following code?

Object o1 = new Object();

Object o2 = new Object();

o1= o2;

if (o1.equals(o2))

System.out.println("Equals");

}

A.Compile time error

B.Run time error

C."Equals"

D.NullPointerException will be thrown

Answer:C

Because the one Instance of Object has been assigned to the other with the line

o1=o2;

They now point to the same memory address and the test with the equals method will return true.

8.What will happen when you attempt to compile and run the following code?

class Base {

int i = 99;

public void amethod () {

System.out.print("Base.amethod()");

}

Base() {

amethod();

}

}

public class Ret extends Base {

int i = -1;

public static void main (String args[ ]) {

Base b = new Ret();

System.out.print(b.i);

b.amethod();

}

public amethod() {

System.out.print("Ret.amethod()");

}

}

A.Ret.amethod() -1 Ret.amethod()

B.Ret.amethod() 99 Ret.amethod()

C.99 Ret.amethod()

D.Compile Time error

Answer: B

Notice that the instance created is of type Ret, but is assigned to a reference of a Base class. In this case, a reference to any of the fields such as I will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle.

9.Which are the three characteristics of Object Oriented programming?

A.encapsulation, dynamic binding, polymorphism

B.polymorphism, overloading, overriding

C.encapsulation, inheritance, polymorphism

D.encapsulation, inheritance, dynamic binding

Answer: C

10.Which of the following statements are ture?

A.Constructors are not inherited

B.Constructors can be overridden

C.A parental constructor can be invoked using this

D.Any method may contain a call to this or super

Answer:A

Parental constructors can be invoked using super, not this.

1.What will happen when you attempt to compile and run the following code?

class Mammal{

Mammal(){

System.out.println("Four");

}

public void ears() {

System.out.println("Two");

}

}

class Dog extends Mammal{

Dog() {

super.ears();

System.out.println("Three");

}

}

public class Scottie extends Dog{

public static void main (String args[ ]){

System.out.println("One");

Scottie d = new Scottie();

}

}

A.One, Two, Three, Four

B.One, Four, Three, Two

C.One, Four, Two, Three

D.Compile time error

E.None of the above

Answer: C

The classes are created from the root of the hierarchy downwards, Thus One is output first as it comes before the instantiation of the Scottie d. Then JVM moves to the base of the hierarchy and runs the constructor for the grandparent Mammal. This outputs "Four". Then the constructor for Dog runs. The constructor for Dog calls ears() method in Mammal and thus "Two" is output. Finally the constructor for Dog completes and outputs "Three".

2.What will happen when you attempt to compile and run the following code?

public class RunT implements Runnable {

public static void main (String args[ ]) {

RunT r = new RunT();

Thread t = new Thread(r);

t.start();

}

public void start () {

for(int i = 0; i< 100; i++)

System.out.println(i);

}

}

A.Compilation and output of count from 0 to 99

B.Compilation and no output

C.Compile time error: class RunT is an abstract class. It can’t be instantiated

D.Compile time error, method start cannot be called directly

Answer: C

The class implements Runnable but does not define the run method.

3.What will happen when you attempt to compile and run the following code?

public class RunT extends Thread {

public static void main (String args[ ]) {

RunT r = new RunT();

r.run();

}

public void run () {

for(int i = 0; i< 100; i++)

System.out.println(i);

}

}

A. Compilation and output of count from 0 to 99

B.Compilation and no output

C.Compile time error: class RunT is an abstract class. It can’t be instantiated

D.Compile time error, method start has not been defined

Answer:A

However, note that this code does not start the execution of the Thread and the run method should not be called in this way.

4.Which of the following are true?

A.Java uses a time-slicing scheduling system for determining which Thread will execute

B.Java uses pre-emptive, co-operative system for determining which Thread will execute

C.Java scheduling is platform dependent and may vary from one implementation to another

D.You can set the priority of a Thread in Java

Answer: C, D

5.How do you indicate where a component will be positioned in FlowLayout?

A.North, South, East, West

B.Assign a row/column grid reference

C.Pass a X/Y percentage parameter to add method

D.Do nothing, the FlowLayout will position the component

Answer: D

FlowLayout by itself positions the components. It starts from top- left corner and goes from left-to-right and top-to-bottom adding all the components.

6.What will happen when you add a vertical scroll bar to the North of a Frame?

A.The frame will enlarge to allow the scrollbar to become its preferred size

B.It will be wide, fat and not very useful

C.You cannot add a vertical scrollbar to the North of a frame, only the East or West

D.The scrollbar will stretch from the top to the bottom of the Frame

Answer: B.

It will be wide, fat and not very useful. You can add a vertical scrollbar anywhere in the Frame.

7.Which of the following Components are not Containers?

A.Frame

B.Window

C.Applet

D.List

E.None of the above

Answer:D

All others are subclasses of Container.

8.Which of the following are not Components?

A.Frame

B.Window

C.Applet

D.List

E.None of the above

Answer: E

Container is a subclass of Component, so all the Containers are Components.

9.Which of the following will compile correctly?

A.boolean b = -1;

B.int i = 19;

C.char c = 99;

D.char c = "a";

Answer: B,C

10.Which of the following is not compile?

A.String s = "abcd" ;

B.String s = new String ("abcd");

C.String s = ‘ 99’;

D.String s = new String (char); where char is an array of char types

Answer: C

In C, 99 is specified in single ‘’.