Java Questions And Answers III

 

Q. 20

Under what situations do you obtain a default constructor?

A.  When you define any class

B.  When the class has no other constructors

C.  When you define at least one constructor

Select the most appropriate answer.

Ans.B

 

Q. 21

Given the following code:

 

public class Test {

}

 

Which of the following can be used to define a constructor for this class:

A.  public void Test() {…}

B.  public Test() {…}

C.  public static Test() {…}

D.  public static void Test() {…}

Select the most appropriate answer.

Ans:B

Q. 22

Which of the following are acceptable to the Java compiler:

A.  if (2 == 3) System.out.println("Hi");

B.  if (2 = 3) System.out.println("Hi");

C.  if (true) System.out.println("Hi");

D.  if (2 != 3) System.out.println("Hi");

E.  if (aString.equals("hello")) System.out.println("Hi");

Select all correct answers.

Ans: A,C,D,E

Q. 23

Assuming a method contains code which may raise an Exception (but not a Runtime Exception), what is the correct way for a method to indicate that it expects the caller to handle that exception:

A.  throw Exception

B.  throws Exception

C.  new Exception

D.  Don't need to specify anything

Select the most appropriate answer.

Ans:B

 

Q. 24

What is the result of executing the following code, using the parameters 4 and 0:

 

public void divide(int a, int b) {

try {

int c = a / b;

} catch (Exception e) {

System.out.print("Exception ");

} finally {

System.out.println("Finally");

}

 

A.  Prints out: Exception Finally

B.  Prints out: Finally

C.  Prints out: Exception

D.  No output

Select the most appropriate answer.

Ans.A

 

Q.25

Which of the following is a legal return type of a method overloading the following method:

 

public void add(int a) {…}

 

A.  void

B.  int

C.  Can be anything

Select the most appropriate answer.

Ans:C

 

Q.26

Which of the following statements is correct for a method which is overriding the following method:

 

public void add(int a) {…}

 

A.  the overriding method must return void

B.  the overriding method must return int

C.  the overriding method can return whatever it likes

Select the most appropriate answer.

Ans:A

 

Q. 27

Given the following classes defined in separate files:

 

class Vehicle {

public void drive() {

System.out.println("Vehicle: drive");

}

}

 

class Car extends Vehicle {

public void drive() {

System.out.println("Car: drive");

}

}

 

public class Test {

public static void main (String args []) {

Vehicle v;

Car c;

v = new Vehicle();

c = new Car();

v.drive();

c.drive();

v = c;

v.drive();

}

}

 

What will be the effect of compiling and running this class Test?

A.  Generates a Compiler error on the statement v= c;

B.  Generates runtime error on the statement v= c;

C. Prints out:

Vehicle: drive

Car: drive

Car: drive

D.  Prints out:

Vehicle: drive

Car: drive

Vehicle: drive

Select the most appropriate answer.

Ans:C

 

Q. 28

Where in a constructor, can you place a call to a constructor defined in the super class?

A.  Anywhere

B.  The first statement in the constructor

C.  The last statement in the constructor

D.  You can't call super in a constructor

Select the most appropriate answer.

 

Ans:B

Q. 31

In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:

 

1. public class Test {

2. public static void main (String args []) {

3. Employee e = new Employee("Bob", 48);

4. e.calculatePay();

5. System.out.println(e.printDetails());

6. e = null;

7. e = new Employee("Denise", 36);

8. e.calculatePay();

9. System.out.println(e.printDetails());

10.    }

11.    }

 

A.  Line 10

B.  Line 11

C.  Line 7

D.  Line 8

E.   Never

Select the most appropriate answer.

Ans:C

 

Q. 32

What is the name of the interface that can be used to define a class that can execute within its own thread?

A.  Runnable

B.  Run

C.  Threadable

D.  Thread

E.   Executable

Select the most appropriate answer.

Ans:A