Java Interview Questions
Most Frequently Asked Questions On List Interface
We require N+1 pointers, Where N is the size of your linked list. N pointers in each of the nodes and one head pointer that will allow you to perform any operation.
The only difference between them is that slow pointer travels the linked list one node at a time whereas a fast pointer travels the linked list two nodes at a time.
A doubly linked list would be a better choice because they allow fast and easy sequential access to the list in both directions.
Most Frequently asked questions on Access Specifiers
No, because private members of superclass are not visible to subclasses
If we will make the constructor private then only members of your class will be able to create its object
If two Threads(suppose T1 and T2) are accessing the same object and updating a volatile variable then it means T1 and T2 can make their own local cache of the Object except for the volatile variable. So the volatile variable will have only one main copy which will be updated by different threads and updating by one thread to the volatile variable will immediately reflect the other Thread.
No, we cannot reduce overridden method accessibility.
Most Frequently Asked Questions On Exception Handling
An Error indicates serious problems that an application should not try to catch. while An Exception indicates conditions that an application might want to catch.
The throws keyword is used to specify that a method may raise an exception during its execution while the throw keyword allows us to throw an exception object to interrupt the normal flow of the program.
final: Any variable declares along with final modifier then those variables treated as a final variable. Methods declared as final is not overridden. Final classes are not extended. While finally blocks are followed by a try or catch and they are compulsorily executable blocks. While in finalize( ) before an object is garbage collected, the garbage collector calls this finalize() method of the object. If that object having any connections with a database or anything, it removes the connections and calls finalize() of the object that destroy the object.
In Java 7, if you use a resource like input or output streams or any database connection logic you always need to close it after using. If it is not closed, it can throw exceptions so it has to be in a try & catch block.
Exceptions which are raised in the try block are handled in the catch block. If it is unable to handle that exception, it can re-throw that exception using throw keyword. It is called re-throwing an exception.
Most Frequently Asked Questions On Java Collection
A collection is a topmost interface in the java collection hierarchy while Collections is a utility class.
whenever we create a HashSet, it internally creates a HashMap and when we insert an element into this HashSet using add() method here it actually calls put() method on internally created HashMap object with an element you have specified as it’s key and constant Object called “PRESENT” as it’s value. So we can say that a Set achieves uniqueness internally through HashMap.
It is a unique collection type which not only stores elements but also supports flow control by introducing blocking if either BlockingQueue is full or empty.
Fail-fast iterator Throw ConcurrentModification Exception while for Fail-safe iterator it is "NO". Fail-fast iterator doesn't clone object while Fail-safe iterator does. Example of fail-fast iterator is HashMap, Vector, ArrayList, HashSet Example of Fail-safe iterator is CopyOnWriteArrayList, ConcurrentHashMap
You can create read-only Collection by using Collections.unmodifiableCollection() utility method. it returns an unmodifiable or read-only view of Collection in which you can not perform any operation
No, Set Interface does allow null value only its implementation i.e. TreeSet doesn't allow null value because it stores element in sorted order so every time one adds a new element it compares value and then it sorts. so internally what happens is it compares the new added null value with existing values, so it throws NullPointerException.