Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 351–360 of 476
Topics in Java Programming
In what scenario would you use a ConcurrentHashMap instead of HashMap?
A When you need faster performance
B When you need thread-safe operations without locking entire map
C When you need to store null values
D When you need to maintain insertion order
Correct Answer:  B. When you need thread-safe operations without locking entire map
EXPLANATION

ConcurrentHashMap uses bucket-level locking (segment locking in Java 7, node-level in Java 8+) allowing concurrent reads/writes, making it thread-safe without synchronizing the entire map.

Take Test
Which Collections utility method creates an immutable empty list?
A Collections.emptyList()
B Collections.unmodifiableList(new ArrayList())
C new ArrayList()
D Arrays.asList()
Correct Answer:  A. Collections.emptyList()
EXPLANATION

Collections.emptyList() returns an immutable, empty list. Option B also works but is more verbose. Option C creates mutable list, and Option D creates mutable list from array.

Take Test
What exception is thrown when you try to add a null value to a TreeSet?
A NullPointerException
B IllegalArgumentException
C ClassCastException
D UnsupportedOperationException
Correct Answer:  A. NullPointerException
EXPLANATION

TreeSet uses comparator or natural ordering, which cannot handle null values, throwing NullPointerException during insertion.

Take Test
Which method of PriorityQueue returns the element without removing it?
A poll()
B remove()
C peek()
D pop()
Correct Answer:  C. peek()
EXPLANATION

peek() returns the head element without removing it. poll() removes and returns, remove() throws exception if empty, and pop() is not a PriorityQueue method.

Take Test
What is the difference between ArrayList and Vector?
A ArrayList is synchronized, Vector is not
B Vector is synchronized, ArrayList is not
C Both are synchronized
D Neither is synchronized
Correct Answer:  B. Vector is synchronized, ArrayList is not
EXPLANATION

Vector is a legacy synchronized collection, while ArrayList is unsynchronized but faster. For thread-safety with ArrayList, use Collections.synchronizedList().

Take Test
What is the time complexity of get() operation in TreeMap?
A O(1)
B O(log n)
C O(n)
D O(n log n)
Correct Answer:  B. O(log n)
EXPLANATION

TreeMap is based on Red-Black tree, so get(), put(), and remove() operations have O(log n) time complexity due to tree traversal.

Take Test
What is the output of Collections.frequency(list, null) if list contains null elements?
A Returns count of null elements
B Throws NullPointerException
C Returns 0
D Throws UnsupportedOperationException
Correct Answer:  A. Returns count of null elements
EXPLANATION

Collections.frequency() correctly handles null values and returns the count of null elements in the collection.

Take Test
Which method of NavigableSet returns the greatest element strictly less than the given element?
A floor()
B lower()
C higher()
D ceiling()
Correct Answer:  B. lower()
EXPLANATION

lower() returns the greatest element < given element. floor() returns <= element.

Take Test
What exception is thrown by Iterator.next() when there are no more elements?
A NoSuchElementException
B IndexOutOfBoundsException
C NullPointerException
D ConcurrentModificationException
Correct Answer:  A. NoSuchElementException
EXPLANATION

Iterator.next() throws NoSuchElementException when hasNext() returns false and next() is called.

Take Test
Consider a scenario: You need a data structure that maintains elements in sorted order and allows fast insertion/deletion. Which is optimal?
A TreeSet
B ArrayList
C HashSet
D LinkedList
Correct Answer:  A. TreeSet
EXPLANATION

TreeSet maintains sorted order (O(log n) insertion/deletion) using a Red-Black tree structure.

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