You are on page 1of 3

Java Quiz 2

By: admin - Tuesday, September 9th, 2008 at 4:08 pm

Java Multiple Choice Questions ( 11 - 20 )

Question 11.

public class AQuestion{


private int i = giveMeJ();
private int j = 10;
private int giveMeJ(){
return j;
}
public static void main(String args[]){
System.out.println((new AQuestion()).i);
}
}

Answers
1. Compiler error complaining about access restriction of private variables of AQuestion.
2. Compiler error complaining about forward referencing.
3. No Compilation error - The output is 0;
4. No Compilation error - The output is 10;

Question 12.

public class AQuestion{


public static void main(String args[]){
System.out.println("Before Try");
try{
}
catch(Throwable t){
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}

1. Compiler error complaining about the catch block, where no Throwable object can
ever be thrown.
2. Compiler error - Throwable Object can not be caught, only Exceptions must be caught.
3. No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

Question 13.

public class AQuestion{


public static void main(String args[]){
System.out.println("Before Try");
try{
}
catch(java.io.IOException t){
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}

1. Compiler error complaining about the catch block where no IOException object can
ever be thrown.
2. Compiler error - IOException not found. It must be imported in the first line of the
code.
3. No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

Question 14.
The class java.lang.Exception
i. Is public
ii. Extends Throwable
iii. Implements Throwable
iv. Is serializable

Question 15.
Read this piece of code carefully

if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");

Answers
1. the code will compile an print “Equal”.
2. the code will compile an print “Not Equal”.
3. the code will cause a compiler error

Question 16.
Read this piece of code carefully

if( "STRING".toUpperCase() == "STRING")


System.out.println("Equal");
else
System.out.println("Not Equal");

Answers
1. the code will compile an print “Equal”.
2. the code will compile an print “Not Equal”.
3. the code will cause a compiler error
Question 17.
The following lines of code
byte b = 0;
b += 1;
1. results in b having the value 1.
2. causes a compiler error.
3. will require a cast (byte) before 1.

Question 18.
The following express
char c = -1;
1. will cause a compiler error as the range of character is between 0 and 2^16 - 1. Will
request for an explicit cast.
2. will not cause a compiler error and c will have the value -1;
3. c will not represent any ASCII character.
4. c will still be a Unicode character.

Question 19.
Which of the following statements are true?
1. A method can throw an Exception
2. A method can return an Exception

Question 20.
All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character)
1. are public
2. are serializable
3. are immutatable
4. extend java.lang.Number
5. are final

You might also like