Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 751–760 of 958
Topics in Java Programming
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
Which collection does NOT allow duplicate elements?
A ArrayList
B HashSet
C LinkedList
D Vector
Correct Answer:  B. HashSet
EXPLANATION

Set interface implementations like HashSet do not allow duplicates. Lists (ArrayList, LinkedList, Vector) allow duplicates.

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
What is the difference between ArrayList and LinkedList?
A ArrayList uses array, LinkedList uses linked list; ArrayList is faster for random access
B LinkedList uses array, ArrayList uses linked list
C Both use arrays internally
D ArrayList maintains insertion order, LinkedList doesn't
Correct Answer:  A. ArrayList uses array, LinkedList uses linked list; ArrayList is faster for random access
EXPLANATION

ArrayList is backed by a resizable array providing O(1) random access. LinkedList uses doubly-linked list providing O(1) insertion/deletion at ends but O(n) for random access.

Take Test
Which of the following collections maintains insertion order?
A HashSet
B TreeSet
C LinkedHashSet
D HashMap
Correct Answer:  C. LinkedHashSet
EXPLANATION

LinkedHashSet maintains insertion order using doubly-linked list. HashSet doesn't maintain order, TreeSet maintains sorted order, HashMap doesn't maintain insertion order.

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

HashMap provides O(1) average time complexity for get() operation. In worst case with hash collisions, it can be O(n).

Take Test
Which interface does HashMap implement in Java Collections Framework?
A Map interface
B Collection interface
C List interface
D Set interface
Correct Answer:  A. Map interface
EXPLANATION

HashMap implements the Map interface which stores key-value pairs. It does not implement Collection, List, or Set interfaces directly.

Take Test
Q.759 Medium OOP in Java
In Java, when you override a method from a parent class, which of the following is NOT a valid reason to use the @Override annotation?
A To enable compile-time checking for correct method signature
B To improve code readability and maintainability
C To automatically invoke the parent class method implementation
D To catch errors if the parent method is removed or signature changes
Correct Answer:  C. To automatically invoke the parent class method implementation
EXPLANATION

The @Override annotation helps verify that you are correctly overriding a parent method and improves code clarity, but it does NOT automatically invoke the parent class method. You must use super.methodName() for that. @Override is purely a compile-time marker for verification purposes.

Take Test
Q.760 Hard OOP in Java
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List accounts = new ArrayList(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
A Encapsulation
B Abstraction only
C Polymorphism and Runtime Binding
D Inheritance only
Correct Answer:  C. Polymorphism and Runtime Binding
EXPLANATION

The same withdraw() call behaves differently for SavingsAccount and CurrentAccount based on the actual object type at runtime. This is polymorphism with runtime (dynamic) binding.

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