More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Step 1 - We can create a Registry Class having Map of all created objects as key and References list as value.
Step 2 - Whenever we create an object , we should update the Registry in the constructor to add the new object.
Step 3 - Whenever we assign a new reference to the object , we need to update the entry in Map. Similarly if the reference get's removed ( end of scope etc ), we need to remove the entry of reference from the list.
Step 4 - We can have threaded code to monitor the Map to see if any object looses all it's references and should call the method to destroy object and clean the memory.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Initialize an ArrayList that will hold LinkedLists i.e every element of the arraylist will be a linked list.
Such collection could be used in algorithms that require first random access and then sequential traversal. For example - Storing traversal paths for a graph wherein we can start from any vertex. Implementing dictionary with each arraylist element holding staring with character and then linked list holding duplicate words.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes , a gc method to explicitly call garbage collection immediately on the object. It will provide better flexibility over application that require optimal usage of memory.
Help us improve. Please let us know the company, where you were asked this question :
Ans. We can copy the elements to a Set and then find the difference of count between ArrayList and Set. As Set don't allow duplicates , they will be removed in the set.
Help us improve. Please let us know the company, where you were asked this question :
Ans. ArrayLists aren't synchronized and hence doesn't allow synchronized access. As multiple threads can access an arraylist in parallel, it may result in an inconsistent state.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java Programs are collection of objects that communicates with each other to get a task accomplished. To add to those objects, there are common spaces ( static i.e common for objects belonging to a class ) that are used too.
We can visualize objects as departments of an organization in real world. Just like Task gets initiated in one department and then files are moved across different departments to get work done. In a similar fashion, a task is initiated in one object ( having main method ) and then information ( through POJOs / DTOs ) is moved across objects to accomplish a task.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  objects  Java Programs are collection of objects  real life example of object communication
Q39. What happens when you assign a pre initialized reference some other object ?
Ans. Now the reference points to a new object in memory. If that was the only reference for the previous object , it will be marked for garbage collection.
Foe example -
Object obj = new Object();
obj = new Object();
object created in first line will be eligible for garbage collection after line 2 as it looses all it's handlers.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, doesn't provide exclusive access as we cannot allocate and deallocate memory exclusively as Java internally manages it. The advantage of this is that it relieves the coder for such tasks and helps protect from many bugs that may get introduced with imperfect coding. Moreover as java garbage collector collects all unclaimed memory or objects, it helps the application from memory leaks.
On the flip side , as coder doesn't have extensive excess to memory , it is upto java to decide on the state for programming construct and data storage and hence may introduce some security risks. For example - Java keeps string literals in string pool and there is no exclusive way to remove it and hence may stay and sensitive data in string pool may introduce security issues. Moreover when we overwrite a value or object for a variable / reference, it is upto java to purge those values and hence it may stay in memory for a while till java decide that it is no longer referenced and hence should be removed and hence makes it vulnerable for inappropriate access.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  disadvantages of garbage collection  advantages and disadvantages of java memory management  java for security applications  java with sensitive data  memory management
Q41. When does an object cleaned or destroyed by garbage collection ?
Ans. Synchronized List locks the whole list to provide synchronization and thread safety during the read or write operation, while, CopyOnWriteArrayList doesn’t lock the whole list during these operations.
The CopyOnWriteArrayList class works according to its name i.e. copy-on-write which performs different actions for reading and write operations. For every write operation (add, set, remove, etc), it makes a new copy of the elements in the list. and for the read operations (get, iterator, listIterator, etc), it works on a different copy. So there is no additional overhead during a read operation and its read operation is faster than Collections.SynchronizedList(). Thus, COWAL is better for reading operation than Synchronized List.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.
Enumeration is less safer than Iterator
Enumeration is fail safe whereas Iterator is fail fast
Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.
Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2
Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove
Help us improve. Please let us know the company, where you were asked this question :
Ans. Garbage collection mechanism frees up the memory that is no longer needed. In languages like C / C++ the deallocation needs to be done explicitly by the programmer and hence any leniency may result in memory leak. Garbage collection in java ensures that all unused memory is reclaimed and hence there are no memory leaks.Moreover it relieves the programmer from the hassle of carefully releasing all memory.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Collections in java is a framework of classes that provides an abstracted data structure to store and manipulate the group of objects. Each class is unique in the way it stores , search , sort and modify the elements.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. HashSet doesnt maintain its elements in any specific order and is all random whereas TreeSet maintains elements in natural order 9 order defined by the equals method of TreeSet element type )
2. TreeSet doesnt allow null elements whereas HashMap does.
3. As TreeSet orders elements and is hence insertion is comparatively slower.
4. HashSet performs basic operations like add(), remove(), contains(), size() etc. in a constant size time. A TreeSet performs these operations at the order of log(n) time.
5. HashMap in Java internally backs a HashSet. A NavigableMap backs a TreeSet internally.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Spliterator has better performance potential than iterators but only if the potential is used. Spliterator can iterate streams in parallel as well as in sequence whereas iterator can only iterate in sequence.
Help us improve. Please let us know the company, where you were asked this question :
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.
Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception.
Help us improve. Please let us know the company, where you were asked this question :