0% found this document useful (0 votes)
28 views2 pages

Collection Question and Answer

The Object class in Java serves as the superclass for all classes, providing essential methods like toString(), equals(), and hashCode(). It also includes methods for thread management (wait, notify) and object manipulation (clone, finalize). The document explains the differences between various methods and classes related to collections and iteration, emphasizing the importance of overriding hashCode when equals is overridden.

Uploaded by

krishna das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Collection Question and Answer

The Object class in Java serves as the superclass for all classes, providing essential methods like toString(), equals(), and hashCode(). It also includes methods for thread management (wait, notify) and object manipulation (clone, finalize). The document explains the differences between various methods and classes related to collections and iteration, emphasizing the importance of overriding hashCode when equals is overridden.

Uploaded by

krishna das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Q: What is the Object class and what is the purpose of it?

A: Object is the parent (superclass) of all classes in Java. Every class inherits it. It provides basic
methods like toString(), equals(), hashCode(), etc.

Q: What is the purpose of the toString() method in the Object class?


A: Returns a string representation of an object. Useful for printing objects.

Q: What is the purpose of the equals() method in the Object class?


A: Compares contents of two objects, not memory addresses.

Q: What is the purpose of the hashCode() method in the Object class?


A: Returns an integer (hash value) for the object. Used in hash-based collections (HashMap,
HashSet).

Q: Why is it recommended to override hashCode() method on overriding the equals() method?


A: Because collections use both equals() and hashCode() to find objects. If only equals() is
overridden, objects may not work properly in collections.

Q: What is the purpose of the clone() method and when can it throw a CloneNotSupportedException?
A: Creates a copy of an object. Throws CloneNotSupportedException if class does not implement
Cloneable interface.

Q: Explain the purpose and working of the finalize() method.


A: Called by Garbage Collector before destroying an object. Rarely used.

Q: What is the purpose and working of wait(), notify() and notifyAll() methods?
A: wait(): makes a thread pause until notified. notify(): wakes up one waiting thread. notifyAll():
wakes up all waiting threads.

Q: Why wait(), notify() and notifyAll() methods are present in Object class instead of Threads class?
A: Because threads wait on object locks (monitors), not on other threads.

Q: What is the difference between == and equals() method?


A: == compares memory addresses (references). equals() compares object contents.

Q: What is the difference between Object and Objects class?


A: Object: Base class of all classes. Objects: Utility/helper class with static methods like equals(),
hash().

Q: Explain Hashcode and equals method relation.


A: If equals() is true, hashCode must be same. If hashCode is same, equals() may or may not be
true.

Q: Explain Equals method and == difference.


A: == checks reference (same memory). equals() checks value/content.

Q: What is the difference between Iterator and Iterable?


A: Iterable: Interface that says object can be iterated. Iterator: Actually used to iterate through
elements.

Q: Why does the Collection interface inherit from the Iterable interface?
A: So that all collections can be looped using for-each or Iterator.

Q: Explain how iteration happens with the help of Iterator and why it is called universal iterator.
A: Iterator works for all collection types (List, Set, Queue), so it's called universal iterator.

Q: Explain how the remove() method in the Iterator interface works.


A: Removes the last element returned by next(). Safe removal while iterating.

Q: What is the use of forEachRemaining(Consumer action) Iterator interface?


A: Runs an action on all remaining elements of iterator.

Q: What is the use of forEach(Consumer action) in the Iterable interface and how can it be used?
A: Runs an action on each element of a collection.

Q: What is the purpose of spliterator() in the Iterable interface and how can it be used?
A: Used for parallel iteration (splitting work). Mostly used with Streams API.

Q: What is the difference between contains(Object o) and containsAll(Collection c) method in the


Collection interface?
A: contains(o): checks if single object is present. containsAll(c): checks if all objects in collection are
present.

Q: What is the difference between remove(Object o) and removeAll(Collection c) method in the


Collection interface?
A: remove(o): removes one object. removeAll(c): removes all objects of another collection.

Q: What is the use of the iterator() method in the Collection interface?


A: Returns an Iterator to loop over elements.

Q: How can we convert a Collection type object to an array?


A: By using toArray(). Example: String[] arr = list.toArray(new String[0]);

You might also like