Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
Which of the following statements about Queue interface is correct?
Answer: C
offer() returns false if element cannot be added, while add() throws exception. poll() returns null if empty.
Q.22Medium
What will be the result of executing this code? PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(5); pq.add(3); pq.add(7); System.out.println(pq.peek());
Answer: B
PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.
Q.23Medium
Consider the following code. What is the behavior? Collection<String> col = new ArrayList<>(); col.add("Java"); Iterator<String> it = col.iterator(); while(it.hasNext()) { String s = it.next(); col.remove(s); }
Answer: B
Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.
Q.24Easy
Which collection class implements NavigableMap interface?
Answer: B
TreeMap implements NavigableMap which provides methods like higherKey(), lowerKey(), floorEntry(), etc.
Q.25Medium
What is the output of the following code? List<String> list = Arrays.asList("A", "B", "C"); list.add("D"); System.out.println(list.size());
Answer: C
Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.
Q.26Easy
In the context of streams and collections, which method returns a Sequential Stream in Java 8+?
Answer: B
collection.stream() returns a sequential stream, while parallelStream() returns a parallel stream.
Q.27Medium
What is the time complexity of contains() operation in HashSet?
Answer: A
HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.
Q.28Medium
Which of the following is a fail-fast iterator behavior?
Answer: B
Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.
Q.29Medium
Consider a scenario where you need fast random access and frequent insertions in the middle. Which collection is most suitable?
Answer: B
LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.
Q.30Medium
What does the removeIf() method in Collection interface do?
Answer: B
removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).
Q.31Hard
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
Answer: B
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
Q.32Hard
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct? List<Integer> list = Arrays.asList(5, 2, 8, 1);
Answer: D
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
Q.33Easy
Which Collection method was introduced in Java 9 to create immutable collections?
Answer: B
Java 9 introduced factory methods like List.of(), Set.of(), Map.of() for creating immutable collections.
Q.34Hard
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
Answer: B
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
Q.35Easy
Which of the following Collection interfaces does NOT support duplicate elements?
Answer: A
Set interface does not allow duplicate elements, while List, Queue, and Collection support duplicates.
Q.36Easy
What is the default initial capacity of a HashMap in Java?
Answer: B
HashMap has a default initial capacity of 16 and a load factor of 0.75.
Q.37Easy
Which collection class is synchronized by default?
Answer: C
Hashtable is a legacy synchronized collection class. HashMap, ArrayList, and TreeSet are not synchronized by default.
Q.38Easy
What does the poll() method return when called on an empty Queue?
Answer: B
poll() returns null if the queue is empty, while remove() throws NoSuchElementException.
Q.39Easy
Which of the following correctly represents the hierarchy of Collection framework?
Answer: B
The correct hierarchy is Iterable (interface) -> Collection -> List/Set/Queue/Deque interfaces.
Q.40Medium
What is the time complexity of add() operation in ArrayList?
Answer: A
ArrayList.add() has O(1) amortized time complexity. When capacity is exceeded, resizing takes O(n).