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

  1. How can immutability help maintain invariants in a class with complex validation logic?Multiple choiceHard
  2. How does Java decide which overridden method to call at runtime?Multiple choiceMedium
  3. Why do teams use getter methods in a class in production code?Multiple choiceEasy
  4. Which approach should be used to prevent modification of a list returned by a method?Multiple choiceHard
  5. In an e-commerce application, which class should be responsible for validating a discount code?Multiple choiceMedium
  6. Which greeting is printed when dynamic dispatch calls Derived.sayHello()?Multiple choiceMedium
  7. What happens when you cast a superclass reference to a subclass type in Java?Multiple choiceMedium
  8. Which approach ensures that a method returns an unmodifiable list in Java?Multiple choiceMedium
  9. Why does fields in a Java class matter in this design?Multiple choiceEasy
  10. Why is it important to use the @Override annotation when overriding a method in Java?Multiple choiceEasy
  11. Which of the following best describes a class invariant?Multiple choiceEasy
  12. What problem might arise if a ShoppingCart directly manipulates Product details without using its interface?Multiple choiceHard
  13. Why does encapsulation in object-oriented programming matter in practice?Multiple choiceEasy
  14. What is the purpose of using 'this' in the following constructor? public class Product { private double price; public Product(double price) { this.price =…Multiple choiceMedium
  15. Which class should be responsible for calculating the total price of items in an e-commerce cart?Multiple choiceMedium
  16. Why is it important to reject invalid stock quantities in an e-commerce application?Multiple choiceMedium
  17. What happens if you assign a 'Product' object reference in a 'Customer' object, and then set the 'Product' to null?Multiple choiceMedium
  18. How can a 'Customer' object maintain a list of 'Cart' objects and ensure only one cart is active at a time?Multiple choiceMedium
  19. In the context of classes, what does 'this' represent?Multiple choiceEasy
  20. Which Java keyword would you use to declare a class that implements an interface in Java?Multiple choiceEasy
  21. Which of the following is a correct use of 'this' in a method?Multiple choiceEasy
  22. Why should you prefer composition over inheritance when designing a Product and Cart relationship?Multiple choiceMedium
  23. Given a superclass method protected void calculate(), which of the following correctly overrides it in a subclass?Multiple choiceHard
  24. Why might you create a method in the 'Product' class that returns a deep copy of a 'Product' object?Multiple choiceMedium
  25. What happens when you call a method on an object stored in a variable of its superclass type?Multiple choiceMedium
  26. In a system with shared 'Product' objects in multiple 'CartItem' instances, how do you ensure modifications in one do not affect the others?Multiple choiceHard
  27. What design flaw exists if a User class directly accesses ShoppingCart's internal list of items?Multiple choiceHard
  28. How can you design a 'Cart' class to maintain a list of 'CartItem' objects and ensure the total price is always up to date?Multiple choiceHard
  29. What does using the 'new' keyword in Java evaluate to?Multiple choiceEasy
  30. How would you explain the keyword used to refer to the current object instance in a Java interview?Multiple choiceEasy
  31. How do you prevent a 'Product' instance from being modified after it has been added to a 'Cart'?Multiple choiceHard
  32. What will happen if you copy an object reference to another variable and then modify the object via the new reference?Multiple choiceMedium
  33. In Java, why is it important for fields in a class to be marked as private?Multiple choiceEasy
  34. Consider a class with a method public void display() {}. Which of the following is a valid override in a subclass?Multiple choiceMedium
  35. Why might you prefer to use an interface instead of an abstract class in Java?Multiple choiceMedium
  36. Why is 'this()' required to be the first line in a constructor when used?Multiple choiceMedium
  37. What does upcasting a PremiumCustomerDiscountPolicy to DiscountPolicy actually change?Multiple choiceHard
  38. In a shopping cart application, how can you prevent a 'Cart' from directly modifying a 'Product'?Multiple choiceHard
  39. How would you explain a key reason for making a class immutable in a Java interview?Multiple choiceMedium
  40. What will happen if 'this' is misused in the following code? public class Store { private String name; public static void main(String[] args) { this.name =…Multiple choiceHard
  41. Which of the following describes a 'constructor' in Java?Multiple choiceEasy
  42. Which Java keyword would you use to explicitly invoke a superclass's method when it has been overridden in a subclass?Multiple choiceMedium
  43. What does it mean if a method in Product is static?Multiple choiceMedium
  44. In the following code, what does 'this.price' refer to? public class Product { private double price; public Product(double price) { this.price = price; } }?Multiple choiceMedium
  45. In the context of method overriding, what does 'same signature' mean?Multiple choiceEasy
  46. What role does encapsulation play in method validation?Multiple choiceMedium
  47. In what scenario might polymorphism cause unexpected behavior in Java applications?Multiple choiceHard
  48. When should you use a constructor in a Java class?Multiple choiceMedium
  49. What should a method in an encapsulated class do to allow data modification?Multiple choiceEasy
  50. What design issue might arise if CartItem modifies the Product quantity directly?Multiple choiceHard