iEntry 10th Anniversary Java Faqs RSS Feed

Java Quiz 1

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

Java Multiple Choice Questions ( 1 – 20 )

Question 1.
Read this piece of code carefully

if("String".toString() == "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 2.
Read this piece of code carefully

if("String".trim() == "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 3.
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion {
	public void method(Object o){
		System.out.println("Object Verion");
	}
	public void method(String s){
		System.out.println("String Version");
	}
	public static void main(String args[]){
		AQuestion question = new AQuestion();
		question.method(null);
	}
}

Answers
1. The code does not compile.
2. The code compiles cleanly and shows “Object Version”.
3. The code compiles cleanly and shows “String Version”
4. The code throws an Exception at Runtime.

Question 4.
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion{
	public void method(StringBuffer sb){
		System.out.println("StringBuffer Verion");
	}
	public void method(String s){
		System.out.println("String Version");
	}
	public static void main(String args[]){
		AQuestion question = new AQuestion();
		question.method(null);
	}
}

Answers
1. The code does not compile.
2. The code compiles cleanly and shows “StringBuffer Version”.
3. The code compiles cleanly and shows “String Version”
4. The code throws an Exception at Runtime.

Question 5.
Read the following code below.

public interface AQuestion{
	public abstract void someMethod() throws Exception;
}

A Class implementing this interface should
1. Necessarily be an abstract class.
2. Should have the method public abstract void someMethod();
3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4. Should have the method public void someMethod() which need not throw an Exception.

Question 6.
An Interface can never be private or protected.

Answers
True
False

Question 7.
A Vector class in jdk 1.2

1. is public
2. is final
3. implements java.util.List
4. is serializable
5. has only One constructor

Question 8.
A String Class

1. is final
2. is public
3. is serializable
4. has a constructor which takes a StingBuffer Object as an Argument

Question 9.

public interface AQuestion{
	void someMethod();
}

The class which implements AQuestion
1. Should have someMethod which must necessarily be public.
2. Should have someMethod which could be “friendly” or public
3. Should have someMethod which should not throw any checked exceptions.
4. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface defination

Question 10.

public class AQuestion{
	private int i = j;
	private int j = 10;
	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 error - The output is 0; 

4. No error - The output is 10;

Stumble It!
 Digg!

Leave a Reply