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

  1. Which Java keyword would you use to declare an interface in Java?Multiple choiceEasy
  2. Why might a developer choose to use an abstract class instead of an interface in Java?Multiple choiceMedium
  3. How would you explain a key advantage of defining a method to check stock availability within the Product class in an interview?Multiple choiceMedium
  4. Which of the following is not a characteristic of interfaces in Java?Multiple choiceEasy
  5. Given the following code, which statement is true about the class OnlineOrder?Multiple choiceMedium
  6. Why might it be problematic to allow direct access to an object's fields?Multiple choiceHard
  7. In Java, which of the following best describes the concept of encapsulation?Multiple choiceEasy
  8. How does the concept of immutability relate to encapsulation in Java?Multiple choiceMedium
  9. Consider an e-commerce application where you need to process different types of payments. How can interfaces help in designing the payment system?Multiple choiceMedium
  10. In an e-commerce system, why should a method to apply discounts be placed in the CartItem class?Multiple choiceMedium
  11. What is one common practice to achieve immutability in Java classes?Multiple choiceEasy
  12. Which access modifier in Java ensures that a field is only accessible within its own class?Multiple choiceEasy
  13. Which of the following best describes the benefit of moving behavior closer to data in object-oriented programming?Multiple choiceHard
  14. What happens if a subclass in Java does not provide an implementation for an abstract method from its abstract superclass?Multiple choiceMedium
  15. In the context of shared mutable state, which technique helps prevent data races in a multithreaded environment?Multiple choiceHard
  16. What does trying to access a non-static field from a static method in Java evaluate to?Multiple choiceEasy
  17. What access level should be provided to mutable fields in a class that follows encapsulation principles?Multiple choiceMedium
  18. What can be a consequence of poor field design in a Java class?Multiple choiceHard
  19. Why would a 'Product' class in an e-commerce application include a field for 'price'?Multiple choiceMedium
  20. What does the 'is-a' relationship signify in object-oriented programming?Multiple choiceEasy
  21. Why can not using encapsulation become a problem in practice?Multiple choiceEasy
  22. Why doesn't every field deserve a setter?Multiple choiceEasy
  23. In Java, what access level allows a field to be accessed only within its own class?Multiple choiceEasy
  24. Why does an instance method in a Java class matter in practice?Multiple choiceEasy
  25. If a class implements two interfaces with methods that have the same signature, what must the class do?Multiple choiceHard
  26. In designing a 'Customer' class, what could be a downside of using auto-generated getters and setters for all fields?Multiple choiceHard
  27. When designing a class for an e-commerce application, under which condition would you choose to make it immutable?Multiple choiceHard
  28. Consider a 'CartItem' class with a field 'quantity'. How might changing the 'quantity' field affect object behavior?Multiple choiceHard
  29. In terms of encapsulation, why is it important for a class to control the visibility of its fields?Multiple choiceEasy
  30. How does the state of an object affect its behavior in Java?Multiple choiceMedium
  31. Why does a getter method in a well-encapsulated class matter in this design?Multiple choiceEasy
  32. In Java, which of the following statements correctly describes a subclass?Multiple choiceEasy
  33. Why is immutability a desirable property for objects representing value-like data, such as a customer's profile?Multiple choiceMedium
  34. Why might a developer choose to use an interface over an abstract class in Java?Multiple choiceHard
  35. Why should setters in a class be avoided or minimized?Multiple choiceMedium
  36. Why might the Liskov Substitution Principle be violated in an inheritance hierarchy?Multiple choiceHard
  37. Why do teams use private fields in a Java class in production code?Multiple choiceMedium
  38. What keyword is used to specify that a class implements an interface in Java?Multiple choiceEasy
  39. In a typical e-commerce application, how should a CartItem object access the price of a Product?Multiple choiceEasy
  40. What design principle is violated if a Cart class directly manipulates a Product's price?Multiple choiceMedium
  41. Why might CartItem hold a Product reference instead of copying every product field into loose variables?Multiple choiceMedium
  42. In the context of e-commerce, which class is most likely to contain a method called calculateTotalPrice()?Multiple choiceEasy
  43. What access modifier should be used for fields to adhere to encapsulation principles?Multiple choiceEasy
  44. How would you explain a key reason to use encapsulation when designing an object with multiple responsibilities in an interview?Multiple choiceMedium
  45. How does encapsulation contribute to a class's responsibility?Multiple choiceMedium
  46. Given the code snippet, what is the result of calling 'getOrderDetails' on a 'SpecialOrder' object?Multiple choiceHard
  47. Consider a method that accepts an 'Order' interface. What is a benefit of this design?Multiple choiceMedium
  48. Given a class with a price field, how can you ensure the price is never negative?Multiple choiceHard
  49. Consider a Product class with a price field. Which approach ensures the price is always positive?Multiple choiceMedium
  50. Which Java keyword would you use to define that a subclass method is providing its own implementation of a superclass method?Multiple choiceEasy