Saturday, April 21, 2012

Core Java Tough Interview Questions

You can read part1 and part2 for more java questions. This section covers some tricky/not usual/tough interview questions. These are the questions which may make us think for a moment in interview.


Question: What will happen if an exception is thrown from the finally block?
Answer: The program will exit if the exception is not catched in the finally block.

Question: What is the purpose of intern() method in the String class?
Answer: It helps in moving the normal string objects to move to the String literal pool. When we use a string literal the string can be interned but when you use new String("...") you get a new string object.
In this example both string literals refer the same object:
String a = "abc";
String b = "abc";
System.out.println(a == b);  // True
Here two different objects are created and they have different references:
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d);  // False
In general you should use the string literal notation when possible. It is easier to read and it gives the compiler a chance to optimize your code.

Question: We have two integer objects:
Integer i=100,j=200;

If we do (j-i) then will it evaluate to Integer Wrapper or a primitive int? Will it consume memory to store one more wrapper instance?
Answer: It will return value 100 in int primitive as un-boxing will take place for i and j. It wont be using memory for storing (i-j) as one new wrapper as shown by this quick test:
@Test
public void test() {
    Integer i=100, j=200;
    System.out.println("i: " + System.identityHashCode(i));
    System.out.println("j: " + System.identityHashCode(j));

    Integer sub = j-i;
    System.out.println("j-i: " + System.identityHashCode(sub));
}

Output:

i: 1494824825
j: 109647522
j-i: 1494824825 <-- same as i


Question: As of JDK 1.6 we can use wrapper directly to perform arithmetic operations? Then if we have a choice between primitives and wrappers which one shall we prefer?
Answer: Actually creating a wrapper for every boxing occurrence is quite expensive, so when used at a single scope of method AutoBoxing uses a pool of common wrappers (see previous question). It is still not recommended to use autoboxing for scientific calculations. As wrappers always involve certain overhead it is always better to perform arithmetic operations on primitives.
http://stackoverflow.com/questions/239560/when-should-i-use-primitives-instead-of-wrapping-objects
http://www.javaspecialists.co.za/archive/Issue090.html



Question: How can we convert float to int in Java?

Answer: Looks pretty easy!! Right? Wrong!!!

float a=8.61f;
 int b;

  b=(int)a;
The result is : 8 [ It should be 9 ]
When a=-7.65f, the result is :  -7 [ It should be -8 ]

So casting it to int directly is not a way to go. Better way is to use Math.round() before using (int) to typecast it.
http://stackoverflow.com/questions/1295424/how-to-convert-float-to-int-with-java


Question: How can we provide our own implementation of HashMap in Java?

Answer:

Question: What is non blocking HashMap?

Good Links: