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

  1. In modern Java, how can interfaces provide additional functionality without breaking existing implementations?Multiple choiceMedium
  2. Why might a Java class have private fields and public methods?Multiple choiceMedium
  3. Consider the following code snippet. What is a potential issue with the 'updatePrice' method?Multiple choiceHard
  4. What does calling List.copyOf on a mutable list evaluate to?Multiple choiceEasy
  5. Consider the following code snippet. What is one way to ensure that the 'Customer' class is immutable?Multiple choiceHard
  6. How can defensive copying help in protecting internal state of a class?Multiple choiceMedium
  7. In an e-commerce application, how can you safely expose a list of available products to a caller?Multiple choiceMedium
  8. What should a developer consider when adding a method to update object state?Multiple choiceMedium
  9. Which Java keyword would you use to refer to the current object within an instance method?Multiple choiceEasy
  10. In Java, what is the effect of making a class's fields protected?Multiple choiceMedium
  11. How would you explain the real benefit of hiding fields behind meaningful methods in an interview?Multiple choiceMedium
  12. Why do teams use encapsulation in Java in production code?Multiple choiceMedium
  13. How would you explain a consequence of not using encapsulation in Java classes in an interview?Multiple choiceMedium
  14. Why does a constructor in a Java class matter in practice?Multiple choiceMedium
  15. Given two interfaces with default methods of the same signature, how can a class implementing both resolve the method conflict?Multiple choiceHard
  16. When is it more appropriate to use composition over inheritance in Java?Multiple choiceMedium
  17. Given the following code, what is the output of calling product.checkStock()? Assume stock is 10?Multiple choiceHard
  18. In Java, what role does a constructor play in a class?Multiple choiceEasy
  19. Why is it often unnecessary to make a method static in a class like CartItem?Multiple choiceMedium
  20. Why might you choose to implement a domain-specific method instead of a generic setter for updating a field?Multiple choiceMedium
  21. What would be the result of calling product.updatePrice(50) with the code provided?Multiple choiceMedium
  22. How would you explain an invariant in the context of Java classes in an interview?Multiple choiceEasy
  23. Why do teams use encapsulation in Java in production code?Multiple choiceEasy
  24. Which scenario might justify exposing a public setter for a 'description' field in a Product class?Multiple choiceHard
  25. When designing a 'User' class for an e-commerce application, what is a key consideration for maintaining encapsulation?Multiple choiceHard
  26. Consider a class representing a customer's order in an e-commerce application. Which of the following practices helps ensure the class is immutable?Multiple choiceMedium
  27. In Java, what role do constructor parameters play in ensuring immutability for a class?Multiple choiceMedium
  28. Which of the following statements about encapsulation is true?Multiple choiceMedium
  29. In a 'CartItem' class, why is it important to have a method to update 'quantity'?Multiple choiceEasy
  30. Consider a class with a private list of items in a shopping cart. What is a safe way to allow external classes to view these items?Multiple choiceMedium
  31. Why does the difference between a field and a local variable in Java matter in practice?Multiple choiceMedium
  32. How does using interfaces in Java improve testability of code?Multiple choiceMedium
  33. How would you explain a common technique to avoid unrestricted setters in a Java interview?Multiple choiceEasy
  34. Which principle is violated if class fields are accessed directly?Multiple choiceEasy
  35. In Java, what is the effect of declaring a field as 'final'?Multiple choiceEasy
  36. Consider an e-commerce application with a Product class. Which fields are most appropriate for this class?Multiple choiceMedium
  37. What keyword is used to refer to the current object within an instance method?Multiple choiceEasy
  38. Which of the following is a feature of immutable objects?Multiple choiceEasy
  39. In an e-commerce application, which instance method would be most appropriate for a CartItem class?Multiple choiceEasy
  40. In a well-encapsulated class, why might a method be preferred over a setter for updating a field?Multiple choiceHard
  41. Which of the following is true about abstract classes in Java?Multiple choiceMedium
  42. In an e-commerce application, why is it important to encapsulate the logic for adding an item to a cart?Multiple choiceMedium
  43. Why is it important to validate inputs when setting an internal collection in a class?Multiple choiceHard
  44. How would you explain a key reason to use private fields in a Java class in an interview?Multiple choiceMedium
  45. Given the following code snippet, what will be the output when cartItem.calculateLineTotal() is called?Multiple choiceMedium
  46. Consider the following code: What is a flaw in using public getters and setters for the 'balance' field in a BankAccount class?Multiple choiceHard
  47. Why does declaring a class as final in Java change runtime behavior?Multiple choiceEasy
  48. When designing an immutable class in Java, which principle should you follow to ensure encapsulation?Multiple choiceHard
  49. In Java, how can you enforce an invariant in a class?Multiple choiceMedium
  50. In Java, what is the purpose of a constructor?Multiple choiceEasy