Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 341–350 of 476
Topics in Java Programming
What happens when you add a null key to a HashMap?
A NullPointerException is thrown
B The null key is stored at index 0
C The null key is ignored
D The insertion fails silently
Correct Answer:  B. The null key is stored at index 0
EXPLANATION

HashMap allows one null key which is handled specially and stored at index 0. TreeMap does not allow null keys.

Take Test
What is the initial capacity of a HashMap in Java?
A 8
B 16
C 32
D 64
Correct Answer:  B. 16
EXPLANATION

HashMap has an initial capacity of 16 and doubles its capacity when load factor (0.75) is reached.

Take Test
Which collection allows duplicate keys in Java?
A HashMap
B HashSet
C TreeSet
D None of these
Correct Answer:  D. None of these
EXPLANATION

No collection allows duplicate keys. When a duplicate key is added to a Map, it overwrites the previous value. HashSet and TreeSet don't allow duplicates.

Take Test
What does Collections.unmodifiableList() return?
A A list that cannot be modified
B An empty list
C A synchronized list
D A sorted list
Correct Answer:  A. A list that cannot be modified
EXPLANATION

Collections.unmodifiableList() returns a read-only view of the list. Any modification attempts throw UnsupportedOperationException.

Take Test
Which interface should be implemented to define custom sorting in Collections.sort()?
A Comparable
B Collection
C Iterator
D Serializable
Correct Answer:  A. Comparable
EXPLANATION

The Comparable interface allows objects to define their natural ordering by implementing compareTo() method.

Take Test
What is the key difference between TreeSet and HashSet?
A TreeSet allows null elements
B TreeSet maintains sorted order, HashSet does not
C HashSet is faster than TreeSet
D TreeSet is thread-safe
Correct Answer:  B. TreeSet maintains sorted order, HashSet does not
EXPLANATION

TreeSet implements SortedSet and maintains elements in sorted order with O(log n) operations. HashSet has O(1) average operations but no order guarantee.

Take Test
Which method is used to remove all elements from a Collection that satisfy a given predicate?
A remove(Object o)
B removeAll(Collection c)
C removeIf(Predicate filter)
D clear()
Correct Answer:  C. removeIf(Predicate filter)
EXPLANATION

The removeIf() method was introduced in Java 8 to remove elements based on a given Predicate condition.

Take Test
What is the advantage of using LinkedList over ArrayList for frequent insertions at the beginning?
A LinkedList has O(1) insertion at beginning, ArrayList has O(n)
B ArrayList is always faster
C LinkedList uses less memory
D LinkedList supports null values while ArrayList doesn't
Correct Answer:  A. LinkedList has O(1) insertion at beginning, ArrayList has O(n)
EXPLANATION

LinkedList provides O(1) insertion at the beginning (head), while ArrayList requires O(n) time to shift elements. Both support nulls and LinkedList uses more memory due to node pointers.

Take Test
Which method in NavigableMap returns a view of the map in descending order?
A descendingMap()
B reverseMap()
C inverseMap()
D sortedMap()
Correct Answer:  A. descendingMap()
EXPLANATION

descendingMap() method of NavigableMap returns a reverse-ordered view. reverseMap() doesn't exist in Collections Framework.

Take Test
Consider: Set set = new HashSet(Arrays.asList(1,2,3,2,1)); System.out.println(set.size()); What is the output?
A 5
B 3
C 2
D 1
Correct Answer:  B. 3
EXPLANATION

HashSet removes duplicates. The list [1,2,3,2,1] is converted to a set containing only unique elements {1,2,3}, so size is 3.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips