Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 371–380 of 476
Topics in Java Programming
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
Which collection class is synchronized and legacy?
A ArrayList
B HashSet
C Vector
D HashMap
Correct Answer:  C. Vector
EXPLANATION

Vector is a legacy synchronized collection similar to ArrayList. It is thread-safe but slower. ArrayList is preferred with Collections.synchronizedList() for synchronization.

Take Test
Which interface provides sorted ordering in Collections Framework?
A Collection
B SortedSet and SortedMap
C List
D Iterator
Correct Answer:  B. SortedSet and SortedMap
EXPLANATION

SortedSet and SortedMap interfaces provide sorted ordering. TreeSet implements SortedSet, TreeMap implements SortedMap.

Take Test
Given:
Map map = new HashMap();
map.put("A", 10);
map.put("B", 20);
map.put("A", 30);
What will be the size of map?
A 1
B 2
C 3
D Exception will be thrown
Correct Answer:  B. 2
EXPLANATION

HashMap stores unique keys. Putting same key 'A' again replaces the old value. Size remains 2 with keys A and B.

Take Test
Which collection is best for frequent insertions and deletions in the middle?
A ArrayList
B LinkedList
C Vector
D CopyOnWriteArrayList
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList provides O(1) insertion/deletion at ends and O(n) at middle. ArrayList requires O(n) for middle insertions/deletions due to shifting.

Take Test
What does the Comparable interface do in Collections Framework?
A It defines comparison logic for sorting
B It creates copies of objects
C It manages memory allocation
D It handles serialization
Correct Answer:  A. It defines comparison logic for sorting
EXPLANATION

Comparable interface provides compareTo() method to define natural ordering for objects. Used by TreeSet and TreeMap for sorting.

Take Test
Which of the following is thread-safe?
A HashMap
B TreeMap
C ConcurrentHashMap
D LinkedHashMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.

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

TreeSet is backed by a TreeMap which uses Red-Black tree. Add operation requires O(log n) time for tree balancing.

Take Test
What is the load factor in HashMap? What is its default value?
A Ratio of entries to capacity; default is 0.5
B Ratio of entries to capacity; default is 0.75
C Number of collisions; default is 0.75
D Hash code value; default is 0.5
Correct Answer:  B. Ratio of entries to capacity; default is 0.75
EXPLANATION

Load factor is the ratio of size to capacity. Default load factor in HashMap is 0.75, which provides good balance between time and space complexity.

Take Test
Which method is used to remove an element from ArrayList while iterating?
A Using remove() directly in loop
B Using Iterator.remove()
C Using Collections.remove()
D Using System.arraycopy()
Correct Answer:  B. Using Iterator.remove()
EXPLANATION

Using remove() directly in loop causes ConcurrentModificationException. Iterator.remove() is the safe way to remove elements while iterating.

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