Java OOP MCQ Test
20 random Java OOP MCQs with answers: classes and objects, constructors, encapsulation, inheritance, polymorphism, abstraction, interfaces, equals/hashCode, records and SOLID.
- 20 random questions
- 20 minutes
- 36 in the question bank
No sign-up needed. Answers and explanations are shown at the end.
About this test
Object-oriented programming is the most asked Java interview topic. This test checks whether you can tell overloading from overriding, an abstract class from an interface, and why equals and hashCode go together.
The explanations point to the exact rule, so you can revise the lesson behind each mistake.
Each attempt picks new questions at random from the question bank. You can go back and change answers until you finish or the timer runs out. At the end you see your score, the right answer and a short explanation for every question, and the lessons to revise for the ones you missed.
Sample questions with answers
1. What is the relationship between a class and an object?
- They are the same thing
- A class is a blueprint; objects are instances built from it
- An object creates classes
Show answer
B. A class is a blueprint; objects are instances built from it — One Dog class can create many Dog objects with their own data.
2. What does this.name = name; do in a constructor?
- Calls another constructor
- Sets the object's field to the parameter's value
- Creates a new variable
Show answer
B. Sets the object's field to the parameter's value — this refers to the current object; it disambiguates field and parameter.
3. How many copies of a static field exist?
- One, shared by the whole class
- None until main runs
- One per object
Show answer
A. One, shared by the whole class — Static members belong to the class, not to instances.
4. What is the main idea of encapsulation?
- Make every field public for convenience
- Put all classes in one file
- Keep fields private and change them through methods with rules
Show answer
C. Keep fields private and change them through methods with rules — Methods like deposit/withdraw validate every change.
5. What does @Override give you?
- A compile error if the method doesn't actually override a parent method
- Multiple inheritance
- Faster code
Show answer
A. A compile error if the method doesn't actually override a parent method — It catches typos like speek() instead of speak().
6. With Animal a = new Dog(); a.speak(); which speak runs?
- Dog's
- Neither
- Animal's
Show answer
A. Dog's — The object's real class decides at runtime (dynamic dispatch).
7. Can you create an object of an abstract class with new?
- Yes
- Only if it has a constructor
- No
Show answer
C. No — Abstract classes are partial blueprints; instantiate a concrete subclass.
8. How many interfaces can a class implement?
- One
- Two
- Many
Show answer
C. Many — Java allows multiple interface implementation (but one superclass).
9. What's wrong with overriding equals but not hashCode?
- toString stops working
- Nothing
- Equal objects may land in different HashMap/HashSet buckets
Show answer
C. Equal objects may land in different HashMap/HashSet buckets — Equal objects must have equal hash codes.
10. Which statement about records is true?
- Their fields are final and they can't extend other classes
- They have setters
- They must be abstract
Show answer
A. Their fields are final and they can't extend other classes — Records are immutable data carriers that can implement interfaces.