Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 661–670 of 958
Topics in Java Programming
Which collection interface should be used if you need both key-value pairing and need to iterate in the order of keys' natural ordering?
A HashMap
B TreeMap
C Hashtable
D WeakHashMap
Correct Answer:  B. TreeMap
EXPLANATION

TreeMap implements NavigableMap and SortedMap, storing key-value pairs in sorted order of keys using a Red-Black tree structure.

Take Test
What is the time complexity of get() operation on a LinkedHashMap with n elements?
A O(1) average case
B O(n)
C O(log n)
D O(n log n)
Correct Answer:  A. O(1) average case
EXPLANATION

LinkedHashMap extends HashMap and uses the same hash table for storage, maintaining O(1) average-case get operation. The doubly-linked list only maintains insertion order.

Take Test
Which of the following statements is true about PriorityQueue?
A Elements are stored in insertion order
B It implements the sorted set interface
C Elements are ordered based on their natural ordering or a comparator
D It is thread-safe by default
Correct Answer:  C. Elements are ordered based on their natural ordering or a comparator
EXPLANATION

PriorityQueue is a heap-based implementation where elements are ordered based on their priority determined by natural ordering or a custom comparator provided during initialization.

Take Test
Consider this code: List list = new CopyOnWriteArrayList(); list.add("A"); list.add("B"); Iterator itr = list.iterator(); list.add("C"); System.out.println(itr.next());
A Throws ConcurrentModificationException
B Prints 'A'
C Prints 'C'
D Throws NoSuchElementException
Correct Answer:  B. Prints 'A'
EXPLANATION

CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.

Take Test
What will happen when you execute: Map map = new HashMap(); map.put("key", null); System.out.println(map.get("key"));
A Throws NullPointerException
B Prints 'null'
C Prints 'key'
D Throws IllegalArgumentException
Correct Answer:  B. Prints 'null'
EXPLANATION

HashMap allows null values. The code stores null as the value for key 'key' and retrieves it successfully, printing null.

Take Test
Which collection would you use if you need to maintain a sorted order of elements and also need O(log n) insertion/deletion time complexity?
A ArrayList
B LinkedList
C TreeSet
D Vector
Correct Answer:  C. TreeSet
EXPLANATION

TreeSet maintains elements in sorted order using a Red-Black tree internally, providing O(log n) for add, remove, and contains operations.

Take Test
What will be the output of the following code? Set set = new TreeSet(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());
A 3
B 4
C 2
D 5
Correct Answer:  A. 3
EXPLANATION

TreeSet does not allow duplicates. Adding 2 twice means it is stored only once. Final set contains {2, 5, 8}, so size is 3.

Take Test
Which of the following collections maintains insertion order while allowing fast random access?
A ArrayList
B HashSet
C TreeSet
D LinkedHashMap
Correct Answer:  A. ArrayList
EXPLANATION

ArrayList maintains insertion order and provides O(1) random access via index. LinkedHashMap maintains insertion order but is a Map, not a List.

Take Test
Which collection interface guarantees that elements are stored in a sorted order with no duplicates?
A List
B Queue
C SortedSet
D Collection
Correct Answer:  C. SortedSet
EXPLANATION

SortedSet interface guarantees sorted order and no duplicates. TreeSet is the primary implementation of SortedSet.

Take Test
What is the main advantage of EnumSet over HashSet when working with Enum constants?
A EnumSet is thread-safe
B EnumSet uses bit vectors internally for better performance
C EnumSet allows null elements
D EnumSet maintains insertion order
Correct Answer:  B. EnumSet uses bit vectors internally for better performance
EXPLANATION

EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.

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