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

  1. How does Java distinguish between different methods with the same name in a class?Multiple choiceMedium
  2. Why does declaring a class as 'public' in Java change runtime behavior?Multiple choiceMedium
  3. How can you ensure that a class cannot be instantiated in Java?Multiple choiceHard
  4. Why does the 'static' keyword in a field declaration matter in this design?Multiple choiceHard
  5. Why does encapsulation in Java matter in practice?Multiple choiceEasy
  6. Which statement is true about constructor overloading?Multiple choiceEasy
  7. In an e-commerce application, why might you prefer to use composition over inheritance for implementing a 'DiscountStrategy'?Multiple choiceMedium
  8. Why should a class's internal state not be exposed via public mutable fields?Multiple choiceHard
  9. Given an abstract class with a concrete method, which statement is true?Multiple choiceHard
  10. How can constructor chaining be achieved within the same class?Multiple choiceMedium
  11. In Java, which of the following best describes the relationship between a class and an object?Multiple choiceEasy
  12. Why does a constructor in a Java class matter in this design?Multiple choiceEasy
  13. In which scenario is it beneficial to use multiple constructors in a class?Multiple choiceMedium
  14. Why does using super() in a constructor change runtime behavior?Multiple choiceMedium
  15. Why does having a private constructor in a Java class change runtime behavior?Multiple choiceEasy
  16. In a Java class 'Order', which constructor signature is most appropriate for initializing an order with a list of items and a customer?Multiple choiceHard
  17. Why do teams use interfaces over abstract classes in a Java application in production code?Multiple choiceMedium
  18. Why are constructors important in beginner object modeling?Multiple choiceMedium
  19. What happens when a subclass does not implement an abstract method from its superclass?Multiple choiceEasy
  20. In terms of encapsulation, what is a potential problem with a public field in a class?Multiple choiceMedium
  21. Why can using public fields in a class become a problem in practice?Multiple choiceMedium
  22. Which design decision is the best fit for representing a 'Discount' that may apply to multiple products in a store model?Multiple choiceMedium
  23. In an e-commerce application, you have a class 'Order' that contains a list of 'Product' items. Which of the following responsibilities is best suited for the…Multiple choiceMedium
  24. Consider this code snippet: What is the visibility of the 'price' field in the Product class?Multiple choiceMedium
  25. What will be the result if a method tries to modify a field of a class without using the 'this' keyword?Multiple choiceMedium
  26. Consider a class 'Product'. How can you ensure every 'Product' object has a unique identifier upon creation?Multiple choiceHard
  27. Given the following class, how can you improve encapsulation?Multiple choiceHard
  28. When designing a 'Product' class, which attribute is least likely to belong directly to the class?Multiple choiceMedium
  29. Consider a 'Product' class in an e-commerce application. What is a potential risk if its price field is public?Multiple choiceMedium
  30. Given the following code snippet, which line will cause a compilation error?Multiple choiceHard
  31. Why do teams use the 'private' access modifier in a class in production code?Multiple choiceEasy
  32. What will be the output of the following code snippet if the Product class is used to create multiple objects?Multiple choiceMedium
  33. Which of the following scenarios best fits the use of an abstract class?Multiple choiceMedium
  34. What problem can occur if a constructor performs complex logic or throws exceptions?Multiple choiceHard
  35. In a store model, which class should be responsible for calculating the total price of items in a shopping cart?Multiple choiceMedium
  36. Why does a method within a Java class matter in practice?Multiple choiceEasy
  37. Why might an abstract class contain both abstract and concrete methods?Multiple choiceHard
  38. Which of the following best describes an advantage of using methods in a Java class?Multiple choiceMedium
  39. Why is it a good design practice to minimize the public surface of a class?Multiple choiceMedium
  40. How does the use of composition support the open/closed principle in Java?Multiple choiceEasy
  41. In the context of immutability, why is it important to make defensive copies of internal mutable objects?Multiple choiceHard
  42. Can an abstract class be instantiated directly in Java?Multiple choiceEasy
  43. Which of the following best describes a class in Java?Multiple choiceEasy
  44. Which access modifier allows a class member to be accessed within the same package but not from outside the package?Multiple choiceEasy
  45. In an e-commerce system, how can encapsulation help manage complex interactions between 'CartItem' objects?Multiple choiceHard
  46. Consider the following constructor: public Product(String name, double price). What is required to create a new Product object?Multiple choiceEasy
  47. Consider an e-commerce system where different types of users, such as Admin and Customer, have common behaviors but also distinct actions. Which design choice…Multiple choiceMedium
  48. How would you explain a key limitation of using abstract classes in a Java interview?Multiple choiceMedium
  49. How would you explain a constructor's role regarding class fields in an interview?Multiple choiceEasy
  50. Which of the following best describes a scenario where inheritance is appropriately used in Java?Multiple choiceMedium