Skip to content
Sharp

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 equals and hashCode together: equal objects must have equal hash codes.
  • Interfaces can have default and static methods (Java 8+) but no instance state. Abstract classes can hold state and constructors.
  • String is immutable; compile-time constant strings are interned.

Questions (page 10 of 10)

  1. Why does a method that validates input before processing matter in practice?Multiple choiceMedium
  2. Given a CartItem class and a Product class, how should CartItem access product details?Multiple choiceMedium
  3. Why does a method in a Java class matter in practice?Multiple choiceEasy
  4. Why does polymorphism reduce caller coupling in Java?Multiple choiceMedium
  5. Which approach ensures that a method in Java returns a deep copy of an object?Multiple choiceHard
  6. Why can using polymorphism extensively in a large codebase become a problem in production code?Multiple choiceHard
  7. Why might you choose an abstract class over an interface in Java?Multiple choiceEasy
  8. Which sign most clearly shows that object collaboration is improving compared with one giant main method?Multiple choiceHard
  9. Why do multi-object rules matter when Cart.addItem(...) both reserves product stock and adds a CartItem?Multiple choiceMedium
  10. Which Java keyword would you use to create a new instance of a class in Java?Multiple choiceEasy
  11. Why does an abstract class in achieving polymorphism matter in this design?Multiple choiceMedium
  12. Which of the following statements is true about method overriding and polymorphism?Multiple choiceMedium
  13. How can a ShoppingCart notify a user when a product is out of stock?Multiple choiceMedium
  14. Why should a CartItem class have a reference to a Product rather than duplicating product details?Multiple choiceMedium
  15. If a Product class has a private field price, which method signature allows a CartItem to read it?Multiple choiceMedium
  16. How can 'this' be used to resolve a naming conflict between a method parameter and an instance field?Multiple choiceMedium
  17. In an e-commerce application, which statement correctly initializes a field using 'this'? public class Order { private int quantity; public Order(int…Multiple choiceMedium
  18. Why does an interface in Java matter in practice?Multiple choiceEasy
  19. Why do teams use getters and setters in a class in production code?Multiple choiceEasy
  20. How would you explain an invariant in the context of Object-Oriented Programming in an interview?Multiple choiceMedium
  21. Which is NOT a benefit of using interfaces in Java?Multiple choiceMedium
  22. Why do teams use the @Override annotation in a subclass in production code?Multiple choiceEasy
  23. Consider a class Order with a private field quantity. Which design choice ensures the quantity is always positive?Multiple choiceHard
  24. Why is it beneficial to validate input data in both constructors and setter methods?Multiple choiceHard
  25. In which scenario is using 'this' unnecessary but still valid?Multiple choiceHard
  26. Why might you choose to return a copy of a list in a getter method?Multiple choiceMedium
  27. Why does 'this' keyword in Java matter in practice?Multiple choiceEasy
  28. In Java, how can you ensure that a class cannot be subclassed?Multiple choiceMedium
  29. How does 'this' help when field names and parameter names are the same?Multiple choiceEasy
  30. In which scenario does method overriding violate the Liskov Substitution Principle?Multiple choiceHard
  31. In a well-encapsulated Java class, how should the fields be accessed?Multiple choiceEasy
  32. When a subclass overrides a superclass method, how can the original superclass method be accessed?Multiple choiceMedium
  33. Why might using 'this' in static methods result in an error?Multiple choiceHard
  34. In a Java e-commerce application, which design decision supports encapsulation when managing quantities of CartItems?Multiple choiceHard
  35. Why should a Product class in an e-commerce application have private fields?Multiple choiceEasy
  36. Which concept allows a subclass to provide a specific implementation of a method that is already defined in its superclass?Multiple choiceEasy
  37. How can a ShoppingCart efficiently calculate the total cost of all items?Multiple choiceMedium
  38. Consider an Order class that must always have a positive quantity. How should this invariant be enforced?Multiple choiceHard
  39. Why does setting an object reference to null in Java change runtime behavior?Multiple choiceMedium
  40. Why might a developer use the super keyword in overridden methods?Multiple choiceEasy
  41. If two CartItem objects hold references to the same Product object and one CartItem changes the Product's price, what happens?Multiple choiceMedium
  42. In a store modeling application, what class design can help manage Product stock levels properly?Multiple choiceMedium