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 5 of 10)

  1. Why is it beneficial to use getters and setters in Java classes?Multiple choiceMedium
  2. How does implementing interfaces contribute to loose coupling in Java applications?Multiple choiceMedium
  3. Why can providing public setter methods for all fields in a class become a problem in production code?Multiple choiceMedium
  4. In Java, how can instance methods affect the state of an object?Multiple choiceEasy
  5. In the context of encapsulation, why is it important to declare fields as private in a Java class?Multiple choiceEasy
  6. In an e-commerce application, a Product class has a discount field. Why is it important to encapsulate this field?Multiple choiceMedium
  7. Why is it generally a good practice to make fields private in a Java class?Multiple choiceEasy
  8. Given the code snippet below, how can you make the 'orders' list immutable to protect it from external modifications?Multiple choiceHard
  9. How does making a class immutable help reduce mutation bugs in an e-commerce application?Multiple choiceMedium
  10. Consider the following code. Which interface method must be implemented by the Product class?Multiple choiceMedium
  11. A Customer class has fields for name, email, and password. Which field should be handled with extra security considerations?Multiple choiceHard
  12. In a Java class, what best defines the concept of 'object state'?Multiple choiceEasy
  13. Given the following code snippet, what is the issue with the 'changeQuantity' method?Multiple choiceHard
  14. Why do teams use interfaces in Java in production code?Multiple choiceEasy
  15. Why does encapsulation in object-oriented programming matter in practice?Multiple choiceEasy
  16. Why does having a well-defined API surface for a class help in practice?Multiple choiceEasy
  17. What potential issue arises when a mutable object is returned from a method of an immutable class?Multiple choiceHard
  18. Which feature of Java interfaces allows implementing classes to have method implementations?Multiple choiceEasy
  19. Which of the following is a reason to use instance methods over static methods?Multiple choiceMedium
  20. Why might a developer choose to use a method like 'adjustStock' instead of a public setter for inventory?Multiple choiceMedium
  21. In the context of object-oriented programming, why should behavior be moved closer to the data it operates on?Multiple choiceMedium
  22. How can a class ensure that its objects remain immutable?Multiple choiceMedium
  23. Which Java keyword would you use to implement an interface in a Java class?Multiple choiceEasy
  24. How does encapsulation help in maintaining a trustworthy store model in an e-commerce application?Multiple choiceMedium
  25. Which of the following best describes an instance method in Java?Multiple choiceEasy
  26. Why might a 'Product' class in an e-commerce system include a 'category' field?Multiple choiceMedium
  27. Why can exposing a public getter for a 'password' field in a User class become a problem in production code?Multiple choiceMedium
  28. In Java, how would you ensure that a Product class's price cannot be changed once it's set at construction?Multiple choiceMedium
  29. Which of the following is true about implementing multiple interfaces in Java?Multiple choiceMedium
  30. Which statement best describes the relationship between fields and instance methods in a well-designed Java class?Multiple choiceHard
  31. Why is it beneficial for a CartItem class to have a method that calculates its own line total?Multiple choiceMedium
  32. Why is encapsulation important in maintaining class invariants in Java?Multiple choiceEasy
  33. Given a CartItem class, why might it be beneficial to include a Product field within it?Multiple choiceMedium
  34. Which keyword in Java is used to restrict access to members of a class?Multiple choiceEasy
  35. How would you explain a limitation of using inheritance for code reuse in an interview?Multiple choiceMedium
  36. Why is returning a raw internal List<CartItem> from Cart risky?Multiple choiceHard
  37. What role does encapsulation play in maintaining invariants in a class?Multiple choiceMedium
  38. What does declaring a class as final in Java evaluate to?Multiple choiceEasy
  39. Which of the following is NOT an advantage of encapsulation?Multiple choiceEasy
  40. Which approach best protects a mutable internal list from external modification in a multi-threaded environment?Multiple choiceHard
  41. In designing a 'Customer' class, which field should ideally be immutable?Multiple choiceHard
  42. In the context of an e-commerce application, which method would most appropriately belong within a 'Product' class?Multiple choiceMedium
  43. In Java, which access modifier would you use to ensure a field is only accessible within its own class?Multiple choiceEasy
  44. If a class method conflicts with an interface default method of the same signature, which implementation wins?Multiple choiceMedium
  45. Why is lineTotal() usually a better method on CartItem than one giant procedural utility that takes many separate parameters?Multiple choiceHard
  46. Given the following code, what will be the output of calling product.displayDetails()?Multiple choiceMedium
  47. Why should setter methods be avoided in an immutable class?Multiple choiceMedium
  48. How does the state of an object affect method execution in Java?Multiple choiceMedium
  49. Which of the following is a benefit of using immutable objects?Multiple choiceMedium
  50. When is an interface usually a better fit than an abstract class?Multiple choiceMedium