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

  1. Which of the following statements is true regarding immutability and thread safety in Java?Multiple choiceHard
  2. Which of the following is a characteristic of well-designed fields in a Java class?Multiple choiceEasy
  3. How can you ensure that a collection inside an immutable class cannot be modified?Multiple choiceHard
  4. How can encapsulation be achieved in Java?Multiple choiceMedium
  5. Why would you prefer a method like 'calculateTotalPrice()' in an Order class over a public getter for a 'totalPrice' field?Multiple choiceMedium
  6. Why do teams use private setters in a class in production code?Multiple choiceMedium
  7. How would you explain a potential issue when forcing inheritance for reuse in an interview?Multiple choiceHard
  8. If a method needs to modify the state of an object, what type of method should it be?Multiple choiceMedium
  9. Why might you use Collections.unmodifiableList in your Java code?Multiple choiceMedium
  10. In an e-commerce application, what is the primary reason for encapsulating a product's price field?Multiple choiceEasy
  11. When is immutability often a strong choice?Multiple choiceMedium
  12. Why does making a class's fields private in Java change runtime behavior?Multiple choiceEasy
  13. In the context of an e-commerce platform, why might you use a method like 'updateStock(int quantity)' instead of a generic setter for a 'stock' field?Multiple choiceMedium
  14. Why might a developer use an interface with a single method, known as a functional interface, in Java?Multiple choiceHard
  15. How does using private fields in a class contribute to encapsulation?Multiple choiceMedium
  16. What will be the output of calling orderItem.calculateTotal() given the following code?Multiple choiceHard
  17. Which Java keyword is used to ensure a class's field cannot be modified after initialization?Multiple choiceEasy
  18. A product can appear in many carts with different quantities. Where does the quantity for one cart line belong most naturally?Multiple choiceMedium
  19. Why might using inheritance be considered a poor design choice in this scenario?Multiple choiceHard
  20. In the context of a Customer class, why might exposing an email field with a public setter be problematic?Multiple choiceHard
  21. Why do teams use interfaces in Java over abstract classes in production code?Multiple choiceEasy
  22. Why do teams use interfaces in Java in production code?Multiple choiceEasy
  23. In Java, why should field variables typically be private?Multiple choiceHard
  24. In the context of Java, what does it mean for a class to be immutable?Multiple choiceMedium
  25. Which of the following statements correctly defines a final field in a Java class?Multiple choiceEasy
  26. Why is reserve(int amount) usually safer than a generic setStock(int newStock) method?Multiple choiceMedium
  27. In the context of encapsulation, what is a potential downside of using too many getter methods?Multiple choiceHard
  28. In an e-commerce application, which class is a good candidate for immutability?Multiple choiceEasy
  29. Consider the following code snippet. Why does this inheritance hierarchy violate good design practices?Multiple choiceHard
  30. If a Product class in an e-commerce system includes a 'discountRate' field, why might you prefer a method like 'applyDiscount()' over a setter?Multiple choiceHard
  31. Which technique can help maintain class invariants when encapsulating fields in a Java class?Multiple choiceHard
  32. How would you explain a potential risk of exposing internal mutable collections directly in an interview?Multiple choiceMedium
  33. How would you explain a valid reason to use default methods in interfaces in a Java interview?Multiple choiceHard
  34. In an e-commerce application, how would you ensure that a customer's address list cannot be modified after initialization?Multiple choiceHard
  35. Which of these features was introduced to interfaces in Java 9?Multiple choiceEasy
  36. How can business operations help in maintaining an object's encapsulation?Multiple choiceMedium
  37. Why can setting all fields in a Java class to public become a problem in practice?Multiple choiceHard
  38. Why might you choose to use a getter method instead of making a field publicly accessible?Multiple choiceMedium
  39. Why do teams use List.copyOf when returning a collection from a method in production code?Multiple choiceMedium
  40. Which of the following statements about Java interfaces is correct?Multiple choiceMedium
  41. In an e-commerce application, which of the following fields in a Product class is least likely to need a setter?Multiple choiceEasy
  42. Why is it generally a bad idea to expose a class's internal fields as public?Multiple choiceMedium
  43. In a Java class representing a product, why might you choose to make the 'price' field private with no public setter?Multiple choiceEasy
  44. When is inheritance an honest design choice in Java?Multiple choiceEasy
  45. Which strategy would best ensure the immutability of a class?Multiple choiceHard
  46. In an e-commerce application, why is it important to use meaningful update methods like 'restock' instead of direct field manipulation?Multiple choiceHard
  47. Which principle of OOP is demonstrated by a 'Product' class having a method that checks its own stock level?Multiple choiceHard
  48. You have a class with a list of orders in an e-commerce system. How can you safely allow external classes to access these orders?Multiple choiceHard
  49. Which Java keyword would you use to restrict access to class members, ensuring encapsulation?Multiple choiceEasy
  50. How can private methods in interfaces be useful in Java?Multiple choiceHard