iGET

Java Programming - MCQ Practice Questions

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.

951 questions | 100% Free

Q.281Medium

What is the initial capacity of a HashMap in Java?

Q.282Easy

Which method returns true if the map contains the specified value?

Q.283Medium

What happens when you add a null key to a HashMap?

Q.284Medium

Which collection implementation is best for frequent insertions and deletions at both ends?

Q.285Medium

What is the purpose of the fail-fast iterator in Collections?

Q.286Medium

Consider a TreeMap with Integer keys. What will headMap(10) return?

Q.287Hard

Which method in Collections can create a thread-safe Map from a non-thread-safe Map?

Q.288Hard

What is the main advantage of EnumSet over HashSet when working with Enum constants?

Q.289Medium

Which collection interface guarantees that elements are stored in a sorted order with no duplicates?

Q.290Easy

Which of the following collections maintains insertion order while allowing fast random access?

Q.291Easy

What will be the output of the following code? Set<Integer> set = new TreeSet<>(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());

Q.292Medium

Which collection would you use if you need to maintain a sorted order of elements and also need O(log n) insertion/deletion time complexity?

Q.293Easy

What will happen when you execute: Map<String, String> map = new HashMap<>(); map.put("key", null); System.out.println(map.get("key"));

Q.294Hard

Consider this code: List<String> list = new CopyOnWriteArrayList<>(); list.add("A"); list.add("B"); Iterator<String> itr = list.iterator(); list.add("C"); System.out.println(itr.next());

Q.295Medium

Which of the following statements is true about PriorityQueue?

Q.296Medium

What is the time complexity of get() operation on a LinkedHashMap with n elements?

Q.297Medium

Which collection interface should be used if you need both key-value pairing and need to iterate in the order of keys' natural ordering?

Q.298Medium

What will be printed? Queue<Integer> queue = new LinkedList<>(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.poll()); System.out.println(queue.peek());

Q.299Hard

In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?

Q.300Easy

Which interface must be implemented to create a thread in Java?