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

  1. Why does Chapter 2 move from loose variables to classes like Product and CartItem?Multiple choiceEasy
  2. In the context of Java, what is an object?Multiple choiceEasy
  3. Why does missing to initialize a field in a constructor change runtime behavior?Multiple choiceMedium
  4. When is an abstract class usually a better fit than an interface?Multiple choiceEasy
  5. Why can exposing a mutable object through a getter method in a class become a real problem?Multiple choiceMedium
  6. Why does providing multiple constructors in a Java class help in practice?Multiple choiceMedium
  7. Which design principle is violated if a class hierarchy is built based on shared implementation details rather than shared behavior?Multiple choiceMedium
  8. Which Java keyword is used to create an object of a class?Multiple choiceMedium
  9. Why is it generally a bad practice to provide setters for all fields in a class?Multiple choiceHard
  10. In an e-commerce application, which class should likely contain the method to add an item to the shopping cart?Multiple choiceEasy
  11. Why is composition often better when tax, discount, or payment behavior changes independently of the main type?Multiple choiceEasy
  12. In the context of constructors, why is it important to use 'this' keyword?Multiple choiceHard
  13. Which access modifier allows a field to be accessed from any class in Java?Multiple choiceEasy
  14. Why would you use private access modifiers for fields in a class?Multiple choiceMedium
  15. What happens if a constructor is private in a Java class?Multiple choiceMedium
  16. What keyword is used to inherit from a class in Java?Multiple choiceEasy
  17. Why would you choose to use methods within a Java class?Multiple choiceMedium
  18. Given the constructor public Order(String id, List<Item> items), what is a potential design improvement?Multiple choiceMedium
  19. Checkout needs one discount strategy today and another tomorrow. Which design keeps that change most local?Multiple choiceHard
  20. Why might it be problematic to perform complex logic in a constructor?Multiple choiceHard
  21. How would you explain a key characteristic of an abstract class in a Java interview?Multiple choiceEasy
  22. In Java, what is the effect of marking a class field as 'private'?Multiple choiceEasy
  23. Why might a deep inheritance hierarchy be problematic in a Java application handling various types of payments?Multiple choiceHard
  24. In designing a payment processing module, you need to handle credit card and PayPal transactions. Why might you choose composition over inheritance to model…Multiple choiceMedium
  25. In Java, what allows a class to inherit methods from multiple sources?Multiple choiceEasy
  26. Why do teams use constructors in a Java class in production code?Multiple choiceMedium
  27. When designing a Java application, what is a potential downside of using inheritance to create a 'PremiumUser' class that extends a 'User' class?Multiple choiceMedium
  28. How would you explain the key difference between an abstract class and an interface in a Java interview?Multiple choiceEasy
  29. Given the constructor public Order(String id, List<Item> items), what is a potential flaw in this design?Multiple choiceHard
  30. Why is it recommended to group related fields and methods in a class?Multiple choiceMedium
  31. Which of these statements about abstract classes in Java is true?Multiple choiceMedium
  32. Which design principle is violated if a 'Product' class directly modifies a 'Customer' class's balance?Multiple choiceHard
  33. Why does encapsulating fields within a class using private access modifiers help in practice?Multiple choiceHard
  34. When two classes, 'Product' and 'Category', are modeled in an e-commerce system, what's the most appropriate relationship?Multiple choiceHard
  35. What happens if a method in a Java class has a return type but does not have a return statement?Multiple choiceMedium
  36. Which of the following is true about abstract methods in Java?Multiple choiceMedium
  37. If a 'Customer' class needs to track multiple shipping addresses, which design is most appropriate?Multiple choiceHard
  38. Why might an e-commerce system's 'Order' class use private setters for its fields?Multiple choiceMedium
  39. Why does declaring a method as 'final' in a superclass change runtime behavior?Multiple choiceMedium
  40. Why might a developer choose to use a 'restock' method rather than a public setter for inventory?Multiple choiceHard
  41. How do unmodifiable views differ from defensive copies in protecting internal collections?Multiple choiceMedium
  42. In an e-commerce application, what is the advantage of using a 'processOrder' method in a Product class?Multiple choiceMedium
  43. Which statement is true about interfaces in Java?Multiple choiceMedium
  44. Why are public mutable fields risky in a store model?Multiple choiceEasy
  45. Why can generating getters and setters for every field in a class become a problem in production code?Multiple choiceMedium
  46. Why does marker interfaces in Java matter in this design?Multiple choiceMedium
  47. What keyword in Java is used to inherit from a superclass?Multiple choiceEasy
  48. Which access modifier is the best fit for fields to ensure encapsulation?Multiple choiceEasy
  49. In Java, why should you avoid returning a reference to a mutable internal collection?Multiple choiceEasy
  50. Which of the following best describes a field in a Java class?Multiple choiceEasy