Saturday, January 18, 2014

12 Important Core Java Interview Questions & Answers

1.What is the difference between Encapsulation and Abstraction?

Answer:  Encapsulation talks about hiding business logic (implementation) details of an object. This is achieved by packaging data members and methods into a single unit. Once the object is packaged, its accessible indirectly through interface of the object controlled by access modifiers.

Whereas Abstraction talks about showing the essential and hiding the non essential. Interfaces are good examples of abstraction as they only define the general behavior of the list leaving it open how it is implemented

2.  What gets stored in Heap and Non Heap memories of JVM?

Answer: All Java Objects created using new() operator are stored in Heap Memory and the classes and method objects are stored in Non Heap memory. Non Heap is also required for compilation and for storing native code.

3. Explain what is OutOfPermGenSpace Java Error and when does it occur.

Answer:  This error is seen when the java program tries to load more classes into memory than limited by JVM PermSpace limit. This error can also occur if the java program has memory leaks or memory leaks in the ClassLoaders themselves.

4.  What is DeadLock and how do you find if your program has run into deadlock?

Answer:  DeadLock is a situation where two or more threads get blocked forever waiting for each other. We can find deadlocks by analyzing thread dump of the program. JDK comes with an utility called jConsole which helps finding deadlocks faster.

5.  How will you improve the performance of a java application? 

Answer: Some of the things we can do to improve performance given below.

(A)  Use primitives wherever possible. Note that it may not be possible to use primitives always with respect to object oriented design. Using primitive types avoids object handling overhead by JVM.

(B) Reuse the objects than recreating them. The object you require may already be loaded into memory in different place in the code. It can just be referenced and used. May be using Helper classes.

(C) Create Immutable Classes considering your design. Usually developers forget this fact while creating a class. Immutable classes thread safe, no need for synchronizing.

(D) Find memory leaks and fix them. Memory leaks can be found using profiling tools. Once the business logic is written, list down the number objects that your code should create and check the how many objects are created for your program in the memory. If it’s not expected number of objects then there leaks in the code and should be fixed.

6. What is the difference between classnotfoundexception & NoClassDefFoundError?

Answer: NoClassDefFoundError arises when JVM cannot find a class at runtime that was available during compile time whereas classnotfoundexception is thrown when .class (byte code) file does not exist in the file system while JVM tries to load the class. Classpath can be updated to include the missing classes to resolve these.
  
7.  What are the best practices to follow during Java Exception Handling?

Answer:  Following are few best practices dealing with Java Exception handling 

(A)   Do not consume an exception: This means that do not have empty try/catch block, handle the exception or throw the exception instead. The java programs with empty try/catch are difficult to debug.

(B)   Do not handle generic exception such as Exception class itself.

(C)   Test the exception handling code. This means the code in catch block should be covered in a test case, if this cannot be tested, and then probably exception handling is not required. Remember exception handling is costlier.

(D)   Create custom exception classes only if none of the existing exceptions hold good. The built in exceptions are tested and improve the code readability.

(E)    Log the exception message. Printing exception trace on the console always not advisable; instead the messages can be logged as trace messages.

(F)    Do not forget to do the clean in finally block such as closing a file, release resources etc.
 
10. When is ConcurrentModificationException thrown?
 Answer: This exception is thrown when you try to modify a collection which you are iterating.

11.  When are the classes unloaded from the memory?
 
Answer: When the class loader that has loaded the class is garbage collected.

12.  What is serialVersionUID that is declared as static final long?
 
Answer: If a class implements serializable interface, it has to declare serialVersionUID. This field represents the version number of the class which is used during deserialization to verify the class that serialized is being deserialized. If this field is not declared explicitly, then JVM will allocate one which may vary between compiler implementations. If this field does not match, JVM throws InvalidClassException. Hence it is recommended to declare  serialVersionUID explicitly for serializable classes.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...