Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 731–740 of 958
Topics in Java Programming
Which of the following is a fail-fast iterator behavior?
A Iterator stops when structural modification occurs
B Iterator throws exception when structural modification occurs
C Iterator skips modified elements silently
D Iterator creates a snapshot of collection
Correct Answer:  B. Iterator throws exception when structural modification occurs
EXPLANATION

Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.

Take Test
What is the time complexity of contains() operation in HashSet?
A O(1) average case, O(n) worst case
B O(n) always
C O(log n)
D O(n log n)
Correct Answer:  A. O(1) average case, O(n) worst case
EXPLANATION

HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.

Take Test
In the context of streams and collections, which method returns a Sequential Stream in Java 8+?
A collection.parallelStream()
B collection.stream()
C Stream.of(collection)
D collection.iterator().stream()
Correct Answer:  B. collection.stream()
EXPLANATION

collection.stream() returns a sequential stream, while parallelStream() returns a parallel stream.

Take Test
What is the output of the following code?
List list = Arrays.asList("A", "B", "C");
list.add("D");
System.out.println(list.size());
A 4
B 3
C Throws UnsupportedOperationException
D Throws ArrayIndexOutOfBoundsException
Correct Answer:  C. Throws UnsupportedOperationException
EXPLANATION

Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.

Take Test
Which collection class implements NavigableMap interface?
A HashMap
B TreeMap
C LinkedHashMap
D EnumMap
Correct Answer:  B. TreeMap
EXPLANATION

TreeMap implements NavigableMap which provides methods like higherKey(), lowerKey(), floorEntry(), etc.

Take Test
Consider the following code. What is the behavior?
Collection col = new ArrayList();
col.add("Java");
Iterator it = col.iterator();
while(it.hasNext()) {
String s = it.next();
col.remove(s);
}
A Executes successfully
B Throws ConcurrentModificationException
C Removes all elements
D Throws NullPointerException
Correct Answer:  B. Throws ConcurrentModificationException
EXPLANATION

Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.

Take Test
What will be the result of executing this code?
PriorityQueue pq = new PriorityQueue();
pq.add(5);
pq.add(3);
pq.add(7);
System.out.println(pq.peek());
A 5
B 3
C 7
D Null
Correct Answer:  B. 3
EXPLANATION

PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.

Take Test
Which of the following statements about Queue interface is correct?
A Queue follows LIFO principle
B Queue.poll() throws exception if queue is empty
C Queue.offer() returns boolean instead of throwing exception
D Queue elements are accessed randomly
Correct Answer:  C. Queue.offer() returns boolean instead of throwing exception
EXPLANATION

offer() returns false if element cannot be added, while add() throws exception. poll() returns null if empty.

Take Test
What is the output of LinkedHashMap iteration in the following code?
LinkedHashMap map = new LinkedHashMap();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for(String key : map.keySet()) System.out.print(key);
A ABC
B CBA
C Random order
D BCA
Correct Answer:  A. ABC
EXPLANATION

LinkedHashMap maintains insertion order. Elements are iterated in the order they were inserted.

Take Test
Which method in Set collection prevents duplicate insertion by returning false?
A add()
B insert()
C put()
D append()
Correct Answer:  A. add()
EXPLANATION

The add() method in Set returns boolean - true if element is added, false if duplicate exists.

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