1. What is the difference between transient and volatile variable in Java?
Transient: In Java, it is used to specify the variable is not being serialized. Serialization is a process of saving an object’s state in Java. When we want to persist and the object’s state by default, all instance variables in the object are stored. In some cases, if we want to avoid persisting few variables because we don’t have the necessity to transfer across the network. So, declare those variables as transient. If the variable is confirmed as transient, then it will not be persisted. Transient keyword is used with that instance variable which will not participate in the serialization process. We cannot use static with a transient variable as they are part of the instance variable.
Volatile : Volatile keyword is used with only variable in Java and it guarantees that the value of the volatile variable will always be read from main memory and not from Thread’s local cache, it can be static.
2. Why does Map interface not extend the Collection interface in the Java Collections Framework?
The map interface is not compatible with the Collection interface. Because Map requires key as well as value, for example: if we want to add key-value pair so we will use put(Object key , Object value). There are two parameters required to add an element to HashMap object. In Collection interface add(Object o) has only one parameter. The other reasons are Map supports valueSet, keySet as well as other suitable methods which have just different views from the Collection interface.
3. Can we import same package/class two times? Will the JVM load the package twice at runtime?
Yes, we can import the same package/class multiple times. Compiler and JVM are not complaining about it. And the JVM will internally load the class only once no matter how many times we import the same class.
4. Define Abstract class?
1. abstract is a keyword, when we declared with this keyword, the class is called abstract.
2. An abstract class may include an abstract method and also includes n numbers of the concrete method.
3. This class can have public, private, protected or constants and default variables.
4. Abstract classes cannot be instantiated.
5. Java doesn’t use pointers. Why?
Pointers are susceptible and slight carelessness in their use may result in memory problems and hence Java basically manages their use.
6. Distinguish between static loading and dynamic class loading?
Static loading – Classes are loaded statically with operator “
Dynamic class loading – It is a technique for programmatically invoking the functions of a class loader at run time. The syntax is
Class.forName (Test className);
To read more click here
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 = new String("Tuturself");
String s = "
"Java automatically put that into String pool.
8. What is the difference between ArrayList and Vector ?
This question is mostly used as a start up question in Technical interviews on the topic of Collection framework . Answer is explained in detail here Difference between ArrayList and Vector.
9. What is the difference between factory and abstract factory pattern?
Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.
10. What is Singleton? is it better to make whole method synchronized or only critical section synchronized ?
Singleton in Java is a class with just one instance in whole Java application, for example
java.lang.Runtime is a Singleton class. Creating Singleton was tricky prior Java 4 but once Java 5 introduced Enum its very easy. see my article How to create thread-safe Singleton in Java for more details on writing Singleton using enum and double checked locking which is purpose of this Java interview question.