Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 721–730 of 958
Topics in Java Programming
What does the poll() method return when called on an empty Queue?
A throws NoSuchElementException
B returns null
C returns empty object
D blocks indefinitely
Correct Answer:  B. returns null
EXPLANATION

poll() returns null if the queue is empty, while remove() throws NoSuchElementException.

Take Test
Which collection class is synchronized by default?
A HashMap
B ArrayList
C Hashtable
D TreeSet
Correct Answer:  C. Hashtable
EXPLANATION

Hashtable is a legacy synchronized collection class. HashMap, ArrayList, and TreeSet are not synchronized by default.

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

HashMap has a default initial capacity of 16 and a load factor of 0.75.

Take Test
Which of the following Collection interfaces does NOT support duplicate elements?
A Set
B List
C Queue
D Collection
Correct Answer:  A. Set
EXPLANATION

Set interface does not allow duplicate elements, while List, Queue, and Collection support duplicates.

Take Test
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
A Collections.synchronizedList(new ArrayList())
B new CopyOnWriteArrayList()
C new Vector()
D new LinkedList()
Correct Answer:  B. new CopyOnWriteArrayList()
EXPLANATION

CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.

Take Test
Which Collection method was introduced in Java 9 to create immutable collections?
A Collections.unmodifiableList()
B List.of()
C List.copyOf()
D Stream.collect()
Correct Answer:  B. List.of()
EXPLANATION

Java 9 introduced factory methods like List.of(), Set.of(), Map.of() for creating immutable collections.

Take Test
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List list = Arrays.asList(5, 2, 8, 1);
A Collections.sort(list, Collections.reverseOrder());
B Collections.sort(list, (a,b) -> b.compareTo(a));
C Collections.sort(list, (a,b) -> a.compareTo(b) * -1);
D All of above
Correct Answer:  D. All of above
EXPLANATION

All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.

Take Test
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
A Returns element without affecting order
B Moves accessed element to end (updates access order)
C Throws exception
D Returns null for accessed elements
Correct Answer:  B. Moves accessed element to end (updates access order)
EXPLANATION

LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).

Take Test
What does the removeIf() method in Collection interface do?
A Removes the first element matching predicate
B Removes all elements matching predicate
C Removes elements at specific index if predicate is true
D Removes duplicates based on predicate
Correct Answer:  B. Removes all elements matching predicate
EXPLANATION

removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).

Take Test
Consider a scenario where you need fast random access and frequent insertions in the middle. Which collection is most suitable?
A ArrayList
B LinkedList
C Vector
D CopyOnWriteArrayList
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.

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