Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

Which of these method is used to remove all keys/values pair from the invoking map?(a) delete()(b) remove()(c) clear()(d) removeAll()The question was asked in unit test.This interesting question is from Java.util in portion java.util – The Collections Framework of Java

Answer»

The CORRECT ANSWER is (b) REMOVE()

Easy explanation: None.

52.

Which of these classes provide implementation of map interface?(a) ArrayList(b) HashMap(c) LinkedList(d) DynamicListThe question was asked in a national level competition.This intriguing question comes from Java.util in chapter java.util – The Collections Framework of Java

Answer»

Correct ANSWER is (B) HashMap

The EXPLANATION: AbstractMap, WEAKHASHMAP, HashMap and TreeMap provide implementation of MAP interface.

53.

Which of this method is used to change an element in a LinkedList Object?(a) change()(b) set()(c) redo()(d) add()This question was posed to me in an internship interview.This intriguing question comes from Java.util in portion java.util – The Collections Framework of Java

Answer»

The CORRECT answer is (b) set()

EASY explanation: An ELEMENT in a LinkedList object can be changed by first using GET() to obtain the index or location of that object and the PASSING that location to method set() along with its new value.

54.

Which of these object stores association between keys and values?(a) Hash table(b) Map(c) Array(d) StringThe question was posed to me by my college director while I was bunking the class.I need to ask this question from Java.util in section java.util – The Collections Framework of Java

Answer» CORRECT CHOICE is (B) Map

Best EXPLANATION: NONE.
55.

Which of these methods can be used to delete the last element in a LinkedList object?(a) remove()(b) delete()(c) removeLast()(d) deleteLast()The question was posed to me in final exam.Enquiry is from Java.util in division java.util – The Collections Framework of Java

Answer» CORRECT answer is (c) removeLast()

For explanation I would SAY: removeLast() and removeFirst() methods are used to remove elements in END and beginning of a linked LIST.
56.

Which of these method of HashSet class is used to add elements to its object?(a) add()(b) Add()(c) addFirst()(d) insert()This question was addressed to me at a job interview.The origin of the question is Java.util in chapter java.util – The Collections Framework of Java

Answer»

The CORRECT OPTION is (a) ADD()

For EXPLANATION: NONE.

57.

Which of these classes implements Set interface?(a) ArrayList(b) HashSet(c) LinkedList(d) DynamicListI had been asked this question in an online quiz.Query is from Java.util in section java.util – The Collections Framework of Java

Answer»

Correct option is (b) HashSet

The EXPLANATION: HashSet and TreeSet IMPLEMENTS Set INTERFACE where as LinkedList and ARRAYLIST implements List interface.

58.

Which of these method is used to add an element to the start of a LinkedList object?(a) add()(b) first()(c) AddFirst()(d) addFirst()I had been asked this question during an interview for a job.Enquiry is from Java.util topic in chapter java.util – The Collections Framework of Java

Answer»

The CORRECT CHOICE is (d) addFirst()

To ELABORATE: NONE.

59.

Which of these standard collection classes implements a linked list data structure?(a) AbstractList(b) LinkedList(c) HashSet(d) AbstractSetThis question was posed to me during an interview.The question is from Java.util topic in portion java.util – The Collections Framework of Java

Answer» CORRECT CHOICE is (B) LinkedList

Best EXPLANATION: NONE.
60.

What is the difference between TreeSet and SortedSet?(a) TreeSet is more efficient than SortedSet(b) SortedSet is more efficient than TreeSet(c) TreeSet is an interface; SortedSet is a concrete class(d) SortedSet is an interface; TreeSet is a concrete classThe question was posed to me in semester exam.The question is from Data Structures-Set in section java.util – The Collections Framework of Java

Answer»

The correct answer is (d) SortedSet is an INTERFACE; TREESET is a concrete class

Explanation: SortedSet is an interface. It maintains an ORDERED set of ELEMENTS. TreeSet is an implementation of SortedSet.

61.

What happens if two threads simultaneously modify TreeSet?(a) ConcurrentModificationException is thrown(b) Both threads can perform action successfully(c) FailFastException is thrown(d) IteratorModificationException is thrownThe question was posed to me in homework.Query is from Data Structures-Set in portion java.util – The Collections Framework of Java

Answer»

Correct choice is (a) ConcurrentModificationException is thrown

Best explanation: TreeSet PROVIDES fail-fast ITERATOR. Hence when concurrently MODIFYING TreeSet it throws ConcurrentModificationException.

62.

Set has contains(Object o) method.(a) True(b) FalseThe question was posed to me by my school principal while I was bunking the class.My question comes from Data Structures-Set in portion java.util – The Collections Framework of Java

Answer»

Correct choice is (a) True

Easy explanation: SET has CONTAINS(OBJECT o) METHOD instead of get(Object o) method as get is needed for comparing object and GETTING corresponding value.

63.

What are the initial capacity and load factor of HashSet?(a) 10, 1.0(b) 32, 0.75(c) 16, 0.75(d) 32, 1.0The question was asked in exam.This interesting question is from Data Structures-Set topic in section java.util – The Collections Framework of Java

Answer»

Right answer is (C) 16, 0.75

For EXPLANATION I would SAY: We should not set the initial CAPACITY too high and load factor too LOW if iteration performance is needed.

64.

What does Collections.emptySet() return?(a) Immutable Set(b) Mutable Set(c) The type of Set depends on the parameter passed to the emptySet() method(d) Null objectThe question was asked in class test.My question is from Data Structures-Set in portion java.util – The Collections Framework of Java

Answer»

Correct option is (a) Immutable Set

To EXPLAIN I WOULD say: Immutable Set is USEFUL in multithreaded environment. One does not need to DECLARE generic type collection. It is inferred by the context of METHOD call.

65.

Do we have get(Object o) method in HashSet.(a) True(b) FalseThe question was posed to me by my school teacher while I was bunking the class.Query is from Data Structures-Set in portion java.util – The Collections Framework of Java

Answer»

Correct CHOICE is (b) False

To explain: get(Object o) METHOD is useful when we want to compare objects based on the comparison of values. HASHSET does not provide any way to compare objects. It just guarantees UNIQUE objects STORED in the collection.

66.

What is the default clone of HashSet?(a) Deep clone(b) Shallow clone(c) Plain clone(d) Hollow cloneI have been asked this question in an international level competition.The question is from Data Structures-Set topic in division java.util – The Collections Framework of Java

Answer»

The CORRECT OPTION is (b) Shallow clone

Explanation: Default clone() METHOD uses shallow copy. The INTERNAL elements are not cloned. A shallow copy only copies the reference object.

67.

What is the worst case complexity of accessing an element in ArrayList?(a) O(n)(b) O(1)(c) O(nlogn)(d) O(2)This question was posed to me in a job interview.This interesting question is from Data Structures-List in section java.util – The Collections Framework of Java

Answer» RIGHT option is (b) O(1)

To explain I would SAY: ARRAYLIST has O(1) complexity for accessing an ELEMENT in ArrayList. O(N) is the complexity for accessing an element from LinkedList.
68.

Which of the below is not an implementation of List interface?(a) RoleUnresolvedList(b) Stack(c) AttibuteList(d) SessionListI have been asked this question during an interview.This intriguing question originated from Data Structures-List topic in portion java.util – The Collections Framework of Java

Answer»

The correct answer is (d) SESSIONLIST

To explain I WOULD say: SessionList is not an IMPLEMENTATION of LIST interface. The OTHERS are concrete classes of List.

69.

Which class provides thread safe implementation of List?(a) ArrayList(b) CopyOnWriteArrayList(c) HashList(d) ListThis question was posed to me in an international level competition.I'm obligated to ask this question of Data Structures-List topic in section java.util – The Collections Framework of Java

Answer»

Correct CHOICE is (b) COPYONWRITEARRAYLIST

The explanation: CopyOnWriteArrayList is a concurrent collection class. Its very efficient if ARRAYLIST is mostly used for reading purpose because it allows multiple threads to read data without LOCKING, which was not POSSIBLE with synchronized ArrayList.

70.

What is the difference between length() and size() of ArrayList?(a) length() and size() return the same value(b) length() is not defined in ArrayList(c) size() is not defined in ArrayList(d) length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the listThe question was posed to me in an interview.The query is from Data Structures-List in section java.util – The Collections Framework of Java

Answer»

Correct answer is (d) length() returns the capacity of ArrayList and size() returns the actual NUMBER of elements stored in the LIST

Explanation: length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the list which is ALWAYS less than or EQUAL to capacity.

71.

When two threads access the same ArrayList object what is the outcome of the program?(a) Both are able to access the object(b) ConcurrentModificationException is thrown(c) One thread is able to access the object and second thread gets Null Pointer exception(d) One thread is able to access the object and second thread will wait till control is passed to the second oneThis question was addressed to me during a job interview.I need to ask this question from Data Structures-List in chapter java.util – The Collections Framework of Java

Answer»

Right choice is (B) ConcurrentModificationException is thrown

To explain I WOULD say: ArrayList is not SYNCHRONIZED. Vector is the synchronized DATA structure.

72.

How is Arrays.asList() different than the standard way of initialising List?(a) Both are same(b) Arrays.asList() throws compilation error(c) Arrays.asList() returns a fixed length list and doesn’t allow to add or remove elements(d) We cannot access the list returned using Arrays.asList()The question was asked in an interview for job.My question is taken from Data Structures-List in division java.util – The Collections Framework of Java

Answer»

The correct answer is (C) Arrays.asList() returns a FIXED length list and doesn’t allow to add or remove elements

Easiest explanation: List returned by Arrays.asList() is a fixed length list which doesn’t allow us to add or remove ELEMENT from it.add() and remove() METHOD will throw UNSUPPORTEDOPERATIONEXCEPTION if used.

73.

How to sort elements of ArrayList?(a) Collection.sort(listObj);(b) Collections.sort(listObj);(c) listObj.sort();(d) Sorter.sortAsc(listObj);This question was addressed to me in an international level competition.My query is from Data Structures-List in chapter java.util – The Collections Framework of Java

Answer»

Right choice is (b) COLLECTIONS.sort(listObj);

To explain: Collections provides a METHOD to sort the LIST. The order of sorting can be DEFINED USING Comparator.

74.

How to remove duplicates from List?(a) HashSet listToSet = new HashSet(duplicateList);(b) HashSet listToSet = duplicateList.toSet();(c) HashSet listToSet = Collections.convertToSet(duplicateList);(d) HashSet listToSet = duplicateList.getSet();The question was asked in an international level competition.This is a very interesting question from Data Structures-List in portion java.util – The Collections Framework of Java

Answer»

Correct OPTION is (a) HASHSET listToSet = NEW HashSet(duplicateList);

Explanation: Duplicate elements are ALLOWED in List. Set contains UNIQUE objects.

75.

How can we remove an object from ArrayList?(a) remove() method(b) using Iterator(c) remove() method and using Iterator(d) delete() methodThis question was addressed to me in homework.This intriguing question comes from Data Structures-List topic in portion java.util – The Collections Framework of Java

Answer»

The CORRECT option is (c) remove() method and using Iterator

The BEST explanation: There are 2 ways to remove an object from ARRAYLIST. We can use overloaded method remove(int INDEX) or remove(Object obj). We can also use an Iterator to remove the object.

76.

If two threads access the same hashmap at the same time, what would happen?(a) ConcurrentModificationException(b) NullPointerException(c) ClassNotFoundException(d) RuntimeExceptionI had been asked this question in an online interview.Asked question is from Data Structures-HashMap topic in portion java.util – The Collections Framework of Java

Answer»

Right OPTION is (a) ConcurrentModificationException

The best I can explain: The code will THROW ConcurrentModificationException if two THREADS ACCESS the same hashmap at the same TIME.

77.

Is hashmap an ordered collection.(a) True(b) FalseI had been asked this question during an interview.Origin of the question is Data Structures-HashMap topic in chapter java.util – The Collections Framework of Java

Answer»

Right choice is (b) False

Easy explanation: Hashmap outputs in the order of HASHCODE of the keys. So it is UNORDERED but will ALWAYS have same result for same set of keys.

78.

While finding the correct location for saving key value pair, how many times the key is hashed?(a) 1(b) 2(c) 3(d) unlimited till bucket is foundI have been asked this question during an interview for a job.This interesting question is from Data Structures-HashMap in chapter java.util – The Collections Framework of Java

Answer»

The CORRECT choice is (B) 2

Explanation: The key is hashed twice; FIRST by hashCode() of Object CLASS and then by internal hashing method of HASHMAP class.

79.

What happens if we put a key object in a HashMap which exists?(a) The new object replaces the older object(b) The new object is discarded(c) The old object is removed from the map(d) It throws an exception as the key already exists in the mapThe question was asked during a job interview.This intriguing question originated from Data Structures-HashMap in section java.util – The Collections Framework of Java

Answer»

Correct answer is (a) The new object replaces the older object

To explain I would SAY: HASHMAP always CONTAINS unique keys. If same KEY is inserted again, the new object replaces the PREVIOUS object.

80.

What is the premise of equality for IdentityHashMap?(a) Reference equality(b) Name equality(c) Hashcode equality(d) Length equalityThe question was posed to me in an online interview.My question comes from Data Structures-HashMap in portion java.util – The Collections Framework of Java

Answer»

Right option is (a) Reference equality

The explanation is: IdentityHashMap is RARELY used as it violates the BASIC CONTRACT of implementing equals() and HASHCODE() METHOD.

81.

Which of the below does not implement Map interface?(a) HashMap(b) Hashtable(c) EnumMap(d) VectorI got this question in quiz.I'd like to ask this question from Data Structures-HashMap in division java.util – The Collections Framework of Java

Answer»

Right answer is (d) VECTOR

Easy explanation: Vector implements ABSTRACTLIST which internally implements Collection. OTHERS COME from implementing the MAP interface.

82.

Which of these methods can be used to obtain a static array from an ArrayList object?(a) Array()(b) covertArray()(c) toArray()(d) covertoArray()The question was posed to me during a job interview.I need to ask this question from Java.util in portion java.util – The Collections Framework of Java

Answer»

Correct answer is (C) toArray()

The EXPLANATION is: NONE.

83.

Map implements collection interface?(a) True(b) FalseI have been asked this question in an international level competition.Enquiry is from Data Structures-HashMap topic in section java.util – The Collections Framework of Java

Answer»

The correct answer is (B) False

Easiest explanation: COLLECTION INTERFACE provides add, remove, search or ITERATE while map has clear, get, put, remove, etc.

84.

Which of these method of ArrayList class is used to obtain present size of an object?(a) size()(b) length()(c) index()(d) capacity()I had been asked this question by my college professor while I was bunking the class.This question is from Java.util in portion java.util – The Collections Framework of Java

Answer» CORRECT CHOICE is (a) SIZE()

To ELABORATE: NONE.
85.

Which of these method can be used to increase the capacity of ArrayList object manually?(a) Capacity()(b) increaseCapacity()(c) increasecapacity()(d) ensureCapacity()I have been asked this question during an interview.This intriguing question originated from Java.util in division java.util – The Collections Framework of Java

Answer»

Right option is (d) ensureCapacity()

To elaborate: When we add an element, the capacity of ArrayList OBJECT increases automatically, but we can INCREASE it manually to SPECIFIED length X by using FUNCTION ensureCapacity(x);

86.

Which of these class can generate an array which can increase and decrease in size automatically?(a) ArrayList()(b) DynamicList()(c) LinkedList()(d) MallocList()The question was posed to me in a national level competition.This is a very interesting question from Java.util topic in division java.util – The Collections Framework of Java

Answer»

The CORRECT CHOICE is (a) ARRAYLIST()

For EXPLANATION: NONE.

87.

Which of these standard collection classes implements a dynamic array?(a) AbstractList(b) LinkedList(c) ArrayList(d) AbstractSetI got this question during an interview for a job.My enquiry is from Java.util in division java.util – The Collections Framework of Java

Answer» CORRECT answer is (c) ArrayList

Explanation: ArrayList class IMPLEMENTS a dynamic array by EXTENDING ABSTRACTLIST class.