Object-oriented programming interview questions
Classes, interfaces, equals/hashCode, immutability.
492 questions with answers and explanations · 490 flashcards
Key ideas
==compares references for objects;equals()compares logical equality.- Override
equalsandhashCodetogether: equal objects must have equal hash codes. - Interfaces can have
defaultandstaticmethods (Java 8+) but no instance state. Abstract classes can hold state and constructors. Stringis immutable; compile-time constant strings are interned.
Questions (page 10 of 10)
- Why does a method that validates input before processing matter in practice?Multiple choiceMedium
- Given a CartItem class and a Product class, how should CartItem access product details?Multiple choiceMedium
- Why does a method in a Java class matter in practice?Multiple choiceEasy
- Why does polymorphism reduce caller coupling in Java?Multiple choiceMedium
- Which approach ensures that a method in Java returns a deep copy of an object?Multiple choiceHard
- Why can using polymorphism extensively in a large codebase become a problem in production code?Multiple choiceHard
- Why might you choose an abstract class over an interface in Java?Multiple choiceEasy
- Which sign most clearly shows that object collaboration is improving compared with one giant main method?Multiple choiceHard
- Why do multi-object rules matter when Cart.addItem(...) both reserves product stock and adds a CartItem?Multiple choiceMedium
- Which Java keyword would you use to create a new instance of a class in Java?Multiple choiceEasy
- Why does an abstract class in achieving polymorphism matter in this design?Multiple choiceMedium
- Which of the following statements is true about method overriding and polymorphism?Multiple choiceMedium
- How can a ShoppingCart notify a user when a product is out of stock?Multiple choiceMedium
- Why should a CartItem class have a reference to a Product rather than duplicating product details?Multiple choiceMedium
- If a Product class has a private field price, which method signature allows a CartItem to read it?Multiple choiceMedium
- How can 'this' be used to resolve a naming conflict between a method parameter and an instance field?Multiple choiceMedium
- In an e-commerce application, which statement correctly initializes a field using 'this'? public class Order { private int quantity; public Order(int…Multiple choiceMedium
- Why does an interface in Java matter in practice?Multiple choiceEasy
- Why do teams use getters and setters in a class in production code?Multiple choiceEasy
- How would you explain an invariant in the context of Object-Oriented Programming in an interview?Multiple choiceMedium
- Which is NOT a benefit of using interfaces in Java?Multiple choiceMedium
- Why do teams use the @Override annotation in a subclass in production code?Multiple choiceEasy
- Consider a class Order with a private field quantity. Which design choice ensures the quantity is always positive?Multiple choiceHard
- Why is it beneficial to validate input data in both constructors and setter methods?Multiple choiceHard
- In which scenario is using 'this' unnecessary but still valid?Multiple choiceHard
- Why might you choose to return a copy of a list in a getter method?Multiple choiceMedium
- Why does 'this' keyword in Java matter in practice?Multiple choiceEasy
- In Java, how can you ensure that a class cannot be subclassed?Multiple choiceMedium
- How does 'this' help when field names and parameter names are the same?Multiple choiceEasy
- In which scenario does method overriding violate the Liskov Substitution Principle?Multiple choiceHard
- In a well-encapsulated Java class, how should the fields be accessed?Multiple choiceEasy
- When a subclass overrides a superclass method, how can the original superclass method be accessed?Multiple choiceMedium
- Why might using 'this' in static methods result in an error?Multiple choiceHard
- In a Java e-commerce application, which design decision supports encapsulation when managing quantities of CartItems?Multiple choiceHard
- Why should a Product class in an e-commerce application have private fields?Multiple choiceEasy
- Which concept allows a subclass to provide a specific implementation of a method that is already defined in its superclass?Multiple choiceEasy
- How can a ShoppingCart efficiently calculate the total cost of all items?Multiple choiceMedium
- Consider an Order class that must always have a positive quantity. How should this invariant be enforced?Multiple choiceHard
- Why does setting an object reference to null in Java change runtime behavior?Multiple choiceMedium
- Why might a developer use the super keyword in overridden methods?Multiple choiceEasy
- If two CartItem objects hold references to the same Product object and one CartItem changes the Product's price, what happens?Multiple choiceMedium
- In a store modeling application, what class design can help manage Product stock levels properly?Multiple choiceMedium