java_interview_questions

Java Multiple Choice Questions

Q. Which of the follwing below live on the heap in java?

Q. What is the output of the following?

public static void main(String… args) { String chair, table = “metal”; chair = chair + table; System.out.println(chair); } A. metal
B. metalmetal
C. nullmetal
D. The code does not compile

D. The code does not compile 

Explanation: The local variable chair may not have been initialized

Q. Which is correct about an instance variable of type String?

A. It defaults to an empty string.
B. It defaults to null.
C. It does not have a default value.
D. It will not compile without initializing on the declaration line

B. It defaults to null.  

Q. How many of the following methods compile?

public class Test 
{
	public String convert(int value) {
		return value.toString();
	}
	public String convert(Integer value) {
		return value.toString();
    }
	public String convert(Object value) {
		return value.toString();
	}
		
	public static void main(String... args) {
		Test obj = new Test();
		System.out.println(obj.convert(10));
	}
}

A. None
B. One
C. Two
D. Three

C. Two

Explanation: Objects have instance methods while primitives do not. Since int is a primitive, you cannot call instance methods on it. Integer and String are both objects and have instance methods.

Q. Which of the following does not compile?

A. int num = 999;
B. int num = 9_9_9;
C. int num = _9_99;
D. None of the above; they all compile.

C. int num = _9_99; 

Q. Which is the first line to trigger a compiler error?

double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4

A. p1
B. p2
C. p3
D. p4

D. P4

Explanation: Type mismatch: cannot convert from double to float

Q. How many instance initializers are in this code?

public class Bowling {
 { 
     System.out.println(); 
 }
 public Bowling () {
    System.out.println();
 }
 static { 
    System.out.println(); 
 }
 { 
     System.out.println(); 
 }
}

A. None
B. One
C. Two
D. Three

C. Two

Q. What is true of the finalize() method?

A. It may be called zero or one times.
B. It may be called zero or more times.
C. It will be called exactly once.
D. It may be called one or more times.

A. It may be called zero or one times.

Explanation: The finalize() method may not be called, such as if your program crashes. However, it is guaranteed to be called no more than once.

Q. Which of the following is true about primitives?

A. You can call methods on a primitive.
B. You can convert a primitive to a wrapper class object simply by assigning it.
C. You can convert a wrapper class object to a primitive by calling valueOf().
D. You can store a primitive directly into an ArrayList.

B. You can convert a primitive to a wrapper class object simply by assigning it.

Q. What is the output of the following?

Integer integer = new Integer(4);
System.out.print(integer.byteValue());
System.out.print("-");
int i = new Integer(4);
System.out.print(i.byteValue());

A. 4-0
B. 4-4
C. The code does not compile.
D. The code compiles but throws an exception at runtime

C. The code does not compile.

Q. Which two primitives have wrapper classes that are not merely the name of the primitive with an uppercase letter?

A. byte and char
B. byte and int
C. char and int
D. None of the above

C. char and int

Explanation: The wrapper class for int is Integer and the wrapper class for char is Character. All other primitives have the same name. For example, the wrapper class for boolean is Boolean.

Q. How do you force garbage collection to occur at a certain point?

A. Call System.forceGc()
B. Call System.gc()
C. Call System.requireGc()
D. None of the above

D. None of the above

Explanation: While you can suggest to the JVM that it might want to run a garbage collection cycle, the JVM is free to ignore your suggestion.

Q. How many of the String objects are eligible for garbage collection right before the end of the main method?

public static void main(String[] fruits) {
  String fruit1 = new String("apple");
  String fruit2 = new String("orange");
  String fruit3 = new String("pear");
  fruit3 = fruit1;
  fruit2 = fruit3;
  fruit1 = fruit2;
}

A. None
B. One
C. Two
D. Three

C. Two 

Explanation: All three references point to the String apple. This makes the other two String objects eligible for garbage collection.

Q. Which of the following does not compile?

A. double num = 2.718;
B. double num = 2._718;
C. double num = 2.7_1_8;
D. None of the above; they all compile

B. double num = 2._718;

Explanation: Underscores are allowed between any two digits in a numeric literal. Underscores are not allowed adjacent to a decimal point

Q. Which of the following is the output of this code, assuming it runs to completion?

public class Toy {
  public void play() {
  	System.out.print("play-");
  }
  public void finalizer() {
  	System.out.print("clean-");
  }
  public static void main(String[] fun) {
  	Toy car = new Toy();
  	car.play();
  	System.gc();
  	Toy doll = new Toy();
  	doll.play();
  }
}

A. play-
B. play-play-
C. play-clean-play-
D. play-play-clean-clean-

B. play-play-  

Explanation: If there was a finalize() method, this would be a different story. However, the method here is finalizer. Tricky! That’s just a normal method that doesn’t get called automatically. Therefore clean is never output.

Q. What is the value of tip after executing the following code snippet?

int meal = 5;
int tip = 2;
int total = meal + (meal>6 ? ++tip : --tip);

A. 1
B. 2
C. 3
D. 6

A. 1

Explanation: In ternary expressions, only one of the two right-most expressions are evaluated. Since meal>6 is false, ––tip is evaluated and ++tip is skipped. The result is that tip is changed from 2 to 1.

Q. What is the output of the following application?

String john = "john";
String jon = new String(john);

System.out.println((john==jon) + " "+ (john.equals(jon)));

A. true true
B. true false
C. false true
D. false false

C. false true