Hi Friends,
In this post I am sharing core java interview questions and answers.
Question 1:
What is the point of having a private constructor?
Answer:
We need a private constructor in two cases:
- When we nee singleton pattern
- When we have only static methods, then we do not need any object.
Question 2:
What is difference between ClassNotFoundException and NoClassDefFoundError.
Answer:
ClassNotFoundException occurs when:
- The forName() method of class tries to load a class.
- The findSystemClass() method in ClassLoader tries to load a class.
- The loadClass() method in ClassLoader tries to load a class.
But no class is found.
NoClassDefFoundError occurs when:
- The class was compiled successfully but couldn't be integrated in JAR file, so at runtime , class is not found.
- When ClassLoader tries to load a class , it ran into an error reading the class definition. This typically happens when the class in question has static blocks and members which use a class that is not found by ClassLoader. So, to find the culprit, look at the code of your class in question and look for code using static blocks or static members.
- When code is compiled on one machine and run on another, so in this case, we may forget to set the classpath properly and this error occurs.
- Any start-up script is overriding classpath environment variable.
- This error comes under Throwable -> Error -> LinkageError -> NoClassDefFoundError
Question 3:
What is a copy constructor?
Answer:
A copy constructor is a constructor which takes a single parameter and which is of type class.
Use case:
It is used to create another object which is a copy of the object that it takes as a parameter.
But, the newly created copy is actually independent of the original object.
Question 4:
What is a WeakHashMap?
Answer:
It is defined in java.util.WeakHashMap.
It is a java collection that contains weak references. This class stores keys [not values] as weak references. If the key is garbage collected, then the entry from the WeakHashMap is also removed.
Question 5:
Is WeakHashMap synchronized?
Answer:
No, it is not synchronized. But we can have synchronized version using Collections.synchronizedMap() method.
Question 6:
Does WeakHashMap allow null keys and values?
Answer:
Yes, it allows null keys and values.
Question 7:
How does key in WeakHashMap work?
Answer:
Key stored in WeakHashMap works as a referent of a weak reference. When the weak reference are garbage collected, then the key and it's value are removed from WeakHashMap.
Question 8:
How WeakHashMap is implemented?
Answer:
The value objects in WeakHashMap are held by ordinary strong references. Tus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly., since that will prevent the keys from being discarded. Note that a value object may refer indirectly to it's key via the WeakHashMap itself, that is, a value object may strongly refer to some other key object whose associated value object, in turn strongly refers to the key of the first value object.
One way to deal with is to wrap values themselves within weak references before inserting and then unwrapping upon each get.
Question 9:
What are the differences between Heap and Stack?
Answer:
There are multiple differences between heap and stack. These are as described below:
- Heap is used to store objects [Objects created anywhere in the class, inside method or in any block of code]. Only exception is the string literal which are stored mainly in permgen area until java 7 in which string pool was moved to heap. Stack space is used to store local variables, method frames and call stack.
- Heap space in java is much bigger than stack space.
- Heap space is shared by threads. But each thread has it's own stack space. And local variables created in thread are not visible to other threads.
- We can resize heap space using -Xms and -Xmx to specify starting and maximum heap memory in java. Similarly, we can use -Xss to specify stack size of individual threads in JVM.
- Order: In heap, objects can be create and stored in any order. But stack memory is structured as LIFO, where method calls are stored as Last In First out order. That is why we can use recursion in java.
- When a heap gets filled, we get java.lang.OutOfMemoryError: java Heap space. When a stack memory gets filled up, then java.lang.StackOverflowError is thrown.
Question 10:
How we can get total memory, free memory and maximum memory in java?
Answer:
Using Runtime class methods , we can get total memory, free memory and maximum memory in java.
- Runtime.getRuntime().freeMemory()
- Runtime.getRuntime().maxMemory()
- Runtime.getRuntime().totalMemory()
Question 11:
What are loading, linking and initialization mechanism in class loader in java?
Answer:
Loading: This component handles the loading of the .class files from the hardware system into the JVM memory and stores the binary data (such as fully classified class name, immediate parent class-name, information about the methods, variables and constructors etc.) in the method areas.
For every loaded .class file, JVM immediately creates an object on the heap memory of type java.lang.class. Do remember, even though the developers call a class multiple times, only one class object is always created. There are three main types of classloaders:
- Bootstrap ClassLoader
- Extension ClassLoader
- Application or System ClassLoader
Linking: This process performs the linking of a class or interface. As this component involves the allocation of new data structure, it may throw the OutOfMemoryError and performs three important activities:
- Verification
- Preparation
- Resolution
Initialization: This component performs the final phase of the class loading where all the static variables are assigned the original values and the static blocks are executed from the parent to the child class.
This process requires careful synchronization as JVM is multithreaded and some threads may try to initialize the same class or interface at the same time.
Question 12:
Where to use ClassLoader in java?
Answer:
Popular example of classloader is Applet ClassLoader which is used to load class by Applet. Since applets are loaded mostly from internet rather than local file system, by using separate ClassLoader , we can load same class from multiple sources and they will be treated as different class in JVM.
Question 13:
What is the maximum size of java array?
Answer:
The maximum size of java array is VM dependent.
Though array size will be Integer.MAX_VALUE - num.
Where num can e anything from 2 to 5.
e.g.:
If maximum size of java array for a VM is Integer.MAX_VALUE - 5, then creating a java array with size Integer.MAX_VALUE - 4 will throw java.lang.OutOfMemoryError.
That's all for this post.
Thanks for reading!!
No comments:
Post a Comment