Friday, January 29, 2016

Popular Java Interview Questions

Popular Java Interview Questions


1. Which two method you need to implement for key Object in HashMap ?
In order to use any object as Key in HashMap, it must implements equals and hashcode methods in Java.
2. What is immutable object? Can you write immutable object?Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.
3. What is the difference between creating String as new() and literal?
When we create string with new() Operator, 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 PermGen area of heap.
String s = new String(“Test”);
does not  put the object in String pool , we need to call String.intern() method which is used to put  them into String pool explicitly. its only when you create String object as String literal e.g. String s = “Test” Java automatically put that into String pool.
4. What is difference between StringBuffer and StringBuilder in Java ?
StringBuilder in Java is introduced in Java 5 and only difference between both of them is that Stringbuffer methods are synchronized while StringBuilder is non synchronized.
5. What is the difference between ArrayList and Vector ?
Vector is  synchronized while ArrayList is not synchronized. Vector is slow as it is thread safe . ArrayList is fast as it is non synchronized .
6.  When do you override hashcode and equals() ?
if you want to use your object as key in HashMap.
7.  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.
8. Does not overriding hashcode() method has any performance implication ?
Poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.
9. What will happen if you call return statement or System.exit on try or catch block ? will finally block execute?
finally block will execute even if you put return statement in try block or catch block but finally block won’t run if you call System.exit form try or catch.
10. Can you override private or static method in Java ?
you can not override private or static method in Java, if you create similar method with same return type and same method arguments that’s called method hiding.
11. What will happen if we put a key object in a HashMap which is already there ?
if you put the same key again than it will replace the old mapping because HashMap doesn’t allow duplicate keys.
12. When will a ConcurrentModificationException happen?
A ConcurrentModificationException will happen if an iterator of a collection is being traversed while at the time the collection is being modified. For example, if you are looping through an iterator of a set and at the time you try to remove or add element to the set.
13. What is difference between Thread.start() and Thread.run()?
Thread.start() will first call system API to start a new thread and then call the Thread.run() method to run the tasks in the thread. It will create a new AccessControlContext and Stack for the thread.
Thread.run() will execute the tasks in Thread but it will not create a new thread. Instead it will execute the tasks in the same thread whee this method is called. Do not call this method manually. Always call Thread.start().

14. What’s the difference between equals() and ==?

it checks to see if the 2 object names are basically references to the same memory location. 

By default equals() will behave the same as the “==” operator and compare object locations.
But, when overriding the equals() method, you should compare the values of the object instead.

15. What is Java Reflection?

Java Reflection provides ability to inspect and modify the runtime behavior of application. Reflection in Java is one of the advance topic of core java. Using java reflection we can inspect a class, interface, enum, get their structure, methods and fields information at runtime even though class is not accessible at compile time. We can also use reflection to instantiate an object, invoke it’s methods, change field values.
Some of the frameworks that use java reflection are:

JUnit – uses reflection to parse @Test annotation to get the test methods and then invoke it.
Spring – dependency injection, read more at Spring Dependency Injection
Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
Eclipse auto completion of method names
Struts
Hibernate

No comments:

Post a Comment