Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 681–690 of 958
Topics in Java Programming
Consider: List list = new ArrayList(); list.add("Java"); list.add("Python"); What will list.get(1) return?
A "Java"
B "Python"
C null
D IndexOutOfBoundsException
Correct Answer:  B. "Python"
EXPLANATION

ArrayList uses 0-based indexing. Index 1 refers to the second element added, which is "Python".

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 does the PriorityQueue in Java Collections Framework guarantee?
A FIFO ordering
B LIFO ordering
C Elements are ordered by priority
D Random element ordering
Correct Answer:  C. Elements are ordered by priority
EXPLANATION

PriorityQueue is a min-heap by default where elements are retrieved based on priority, not insertion order.

Take Test
Which collection class is synchronized and thread-safe by default?
A HashMap
B Hashtable
C TreeMap
D LinkedHashMap
Correct Answer:  B. Hashtable
EXPLANATION

Hashtable is a legacy synchronized collection class. For modern applications, ConcurrentHashMap is preferred over Hashtable.

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

HashMap provides O(1) average case time complexity for get, put, and remove operations due to hash-based indexing.

Take Test
Which interface in Java Collections Framework is used to maintain insertion order while allowing duplicates?
A List
B Set
C Queue
D Map
Correct Answer:  A. List
EXPLANATION

List interface maintains insertion order and allows duplicate elements. ArrayList, LinkedList, and Vector are common implementations.

Take Test
In Java 2024-25, which enhancement improved record support in collections?
A Records automatically implement Comparable
B Records can be used as sealed collection types with pattern matching
C Records eliminate the need for custom Comparators
D Records are automatically thread-safe
Correct Answer:  B. Records can be used as sealed collection types with pattern matching
EXPLANATION

Java 21+ enhanced sealed classes and pattern matching, allowing records to be used with pattern matching in collection operations and switch statements for type-safe data handling.

Take Test
What is the primary purpose of the Comparator interface in Collections Framework?
A To compare two objects and return equality
B To define custom ordering for sorting collections
C To prevent duplicate elements
D To enhance performance of HashMap
Correct Answer:  B. To define custom ordering for sorting collections
EXPLANATION

Comparator interface allows defining custom comparison logic for sorting. It provides compare(T o1, T o2) method to establish custom ordering independent of the object's natural order.

Take Test
Which of these operations is guaranteed to be atomic in ConcurrentHashMap?
A if(!map.containsKey(key)) map.put(key, value);
B map.putIfAbsent(key, value);
C map.get(key) followed by map.put(key, newValue);
D map.remove(key) followed by map.put(key, value);
Correct Answer:  B. map.putIfAbsent(key, value);
EXPLANATION

putIfAbsent() is an atomic operation. Options A and C-D are compound operations that are not atomic and can have race conditions between checks and modifications.

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