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

  1. Why does a class in Java matter in practice?Multiple choiceEasy
  2. In an e-commerce application, you have a class 'Customer' with a field 'name'. What does the 'this' keyword refer to in the method below? public class…Multiple choiceEasy
  3. How would you explain an invariant in object design in an interview?Multiple choiceHard
  4. Given the following code, how do CartItem and Product communicate?Multiple choiceMedium
  5. How would you explain a potential issue when a class exposes its fields as public in an interview?Multiple choiceEasy
  6. Which situation could lead to a problem when a subclass overrides a method?Multiple choiceHard
  7. Which pattern should be used to ensure that a Product's price does not fall below a minimum allowable value?Multiple choiceHard
  8. In the context of encapsulation, what is the primary benefit of validating data in a constructor?Multiple choiceMedium
  9. In a class representing an e-commerce order, you want to ensure that the quantity of items is never negative. Which approach should you take to enforce this…Multiple choiceHard
  10. What role does a constructor play in the creation of objects in Java?Multiple choiceMedium
  11. Which design principle is violated if a Cart class directly changes a Product's availability status?Multiple choiceHard
  12. If a 'Product' class has a method that modifies its price, how can you ensure this modification is visible to all 'CartItem' instances holding a reference to…Multiple choiceMedium
  13. Which of the following is not true about method overriding?Multiple choiceEasy
  14. How does Java achieve polymorphism with interfaces?Multiple choiceMedium
  15. Consider the following code. What will be the output when the 'processOrder' method is called with a 'DigitalProduct'?Multiple choiceMedium
  16. In an e-commerce application, which statement correctly assigns an instance field using 'this' in a method? public class ShoppingCart { private int itemCount;…Multiple choiceMedium
  17. With a superclass reference pointing to a subclass object, what prints here?Multiple choiceHard
  18. What issue arises if CartItem duplicates Product fields such as name and price?Multiple choiceHard
  19. Why does 'this' keyword in a constructor matter in practice?Multiple choiceEasy
  20. In Java, what happens when you assign one object reference to another?Multiple choiceEasy
  21. How can a subclass ensure that it maintains the contract of a superclass method while still providing additional functionality?Multiple choiceHard
  22. How can immutability help in maintaining invariants within an object?Multiple choiceHard
  23. Which of the following scenarios can lead to a compile-time error when overriding a method?Multiple choiceHard
  24. Which of the following statements about method overriding is true?Multiple choiceMedium
  25. In the context of method overriding, what role does the super keyword play?Multiple choiceMedium
  26. What is substitutability in the context of Java polymorphism?Multiple choiceEasy
  27. Why might a Product class use a setPrice(double price) method?Multiple choiceMedium
  28. Consider the following code snippet. What will be the output?Multiple choiceMedium
  29. Why does method overriding in Java help in practice?Multiple choiceEasy
  30. In a class design, why might you choose to use an immutable object for sensitive data?Multiple choiceHard
  31. Why do teams use interfaces in Java in production code?Multiple choiceEasy
  32. Why does making a class immutable in Java help in practice?Multiple choiceMedium
  33. How does returning a copy of a list in a getter method help maintain encapsulation?Multiple choiceMedium
  34. Consider a Product class in an e-commerce application. Which design choice best ensures that the product's price is always valid and never negative?Multiple choiceMedium
  35. In an e-commerce application, how would you safely share a 'Product' object across multiple threads?Multiple choiceHard
  36. What can cause a ClassCastException in a polymorphic Java application?Multiple choiceHard
  37. Which keyword would you use to call another constructor within the same class?Multiple choiceMedium
  38. Why might excessive use of polymorphism affect performance in a Java application?Multiple choiceHard
  39. Why can a ShoppingCart that directly modifies Product stock levels become a bug in practice?Multiple choiceHard
  40. Which of the following is the best practice for managing access to an object's fields in Java?Multiple choiceEasy
  41. How does polymorphism benefit code maintainability in Java?Multiple choiceEasy
  42. In a well-designed e-commerce application, which class should handle the logic for applying a shipping fee based on the total weight of items?Multiple choiceHard
  43. How does polymorphism reduce the dependency between application modules?Multiple choiceHard
  44. What prints here? Product item = new DigitalProduct(); System.out.println(item.label());?Multiple choiceMedium
  45. In a customer management system, which class should own the logic for verifying customer age?Multiple choiceMedium
  46. Why does a constructor often use this.name = name;?Multiple choiceMedium
  47. Given the class 'Product', how can you ensure that a 'CartItem' has a reference to a 'Product'?Multiple choiceMedium
  48. How would you explain an instance variable in a Java class in an interview?Multiple choiceEasy
  49. Which Java feature allows different classes to share a method signature but provide different implementations?Multiple choiceMedium
  50. In the following code snippet, why does using 'this' cause a compile error? public class Store { private String name; public static void…Multiple choiceHard