Java Collections MCQs for MNC Interviews

📘 Follow me for daily Java, Spring Boot, SQL & System Design MCQs to crack MNC interviews 🚀 🧠 Java Interview MCQ — Day 6 Topic: Java Collections Q1. Output of List with duplicates List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("A"); System.out.println(list.size()); Answer: C) 3 Why: ArrayList allows duplicate elements and preserves insertion order. You added 3 items, so size is 3. Q2. Which collection does NOT allow duplicate elements? Answer: C) HashSet Why: HashSet follows Set rules. Set never allows duplicate values. Q3. Method used to sort a List in Java Answer: B) Collections.sort() Why: Collections.sort() sorts a List in natural order or using a Comparator. Q4. Default initial capacity of ArrayList Answer: C) 10 Why: When ArrayList resizes for the first time, it creates capacity of 10. Q5. Which interface is implemented by ArrayList? Answer: C) List Why: ArrayList implements the List interface. Q6. What happens if you add null into HashSet? Answer: B) Only one null allowed Why: HashSet allows a single null value because duplicates are not allowed. Q7. Which class maintains insertion order? Answer: C) LinkedHashSet Why: LinkedHashSet maintains insertion order using a linked list and hash table. Q8. Which of the following is NOT synchronized? Answer: C) ArrayList Why: ArrayList is not thread-safe. Vector, Hashtable, and Stack are synchronized. Q9. Which method removes an element by index in ArrayList? Answer: B) remove() Why: remove(int index) method removes element by index. Q10. Which collection is best for key–value pairs? Answer: C) Map Why: Map is designed for key and value mapping like HashMap, TreeMap, LinkedHashMap. ✅ Concept Summary List → allows duplicates, ordered Set → no duplicates Map → key/value pairs ArrayList → not synchronized, allows duplicates HashSet → one null, no duplicates LinkedHashSet → maintains insertion order #Java #SpringBoot #FullStackDeveloper

  • No alternative text description for this image

📌 The key clue to the correct answer is already mentioned above the question.

Like
Reply

To view or add a comment, sign in

Explore content categories