Monday, 25 April 2011

java interview questions

What is garbage collection? What is the process that is responsible for doing that in java?  
Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process
What kind of thread is the Garbage collector thread? 
 It is a daemon thread.
What is a daemon thread? 
These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.
How will you invoke any external process in Java? 
What is the finalize method do? 
Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.
What is mutable object and immutable object? 
If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)
What is the basic difference between string and stringbuffer object? 
String is an immutable object. StringBuffer is a mutable object.
What is the purpose of Void class? 
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.
What is reflection? 
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.
What is the base class for Error and Exception? 
What is the byte range? 
128 to 127
What is the implementation of destroy method in java.. is it native or java code? 
 This method is not implemented.
What is a package? 
 To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.
What are the approaches that you will follow for making a program very efficient? 
By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.
What is a DatabaseMetaData? 
 Comprehensive information about the database as a whole.
What is Locale? 
A Locale object represents a specific geographical, political, or cultural region
How will you load a specific locale? 
 Using ResourceBundle.getBundle(…);
What is JIT and its use? 
Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.
Is JVM a compiler or an interpreter? 
When you think about optimization, what is the best way to findout the time/memory consuming process? 
 Using profiler

 What is immutable object? Can you write immutable object?

You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

 Does all property of immutable object needs to be final?
Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor.

What is the difference between creating String as new () and literal?
When we create string with new () it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.

How does substring () inside String works?
Another good question, I think answer is not sufficient but here it is “Substring creates new object out of string by taking a portion of original string”.

 How do you handle error condition while writing stored procedure or accessing stored procedure from java?
Open for all, my friend didn't know the answer so he didn't mind telling me.

What is difference between Executor.submit() and Executer.execute() method ? 
(Former returns an object of Future which can be used to find result from worker thread)

What is the difference between factory and abstract factory pattern?
Open for all, he explains about factory pattern and how factory pattern saves maintenance time by encapsulating logic of object creation but didn't know exact answer

What is Singleton? is it better to make whole method synchronized or only critical section synchronized ?
see my article 10 Interview questions on Singleton Pattern in Java

Can you write code for iterating over hashmap in Java 4and Java 5 ?
Tricky one but he managed to write using while and for loop.

When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.

Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?
Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)

 What is the difference when String is gets created using literal or new() operator ?
When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.

Does not overriding hashcode() method has any performance implication ?
This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.

1 comment:

  1. Thanks for sharing this amazing interview questions. Thanks for this great help.

    Interview Questions