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

  1. When comparing two String objects, what's the difference between == and equals()?Multiple choiceEasy
  2. What does this program print?What does this code output?Medium
  3. Which statements about the equals/hashCode contract are true? Select all that apply.Select all that applyMedium
  4. In modern Java (8+), when would you choose an abstract class over an interface? Compare the two.Interview answerMedium
  5. What issue might arise if a constructor directly assigns a mutable object without copying it?Multiple choiceHard
  6. What happens if a subclass does not implement all abstract methods from its abstract superclass?Multiple choiceEasy
  7. What does the 'this' keyword refer to within an instance method of a class?Multiple choiceMedium
  8. Which Java keyword would you use to declare an abstract class in Java?Multiple choiceEasy
  9. Which statement correctly creates an instance of the 'Product' class?Multiple choiceMedium
  10. How does using interfaces in Java promote flexibility in application design?Multiple choiceEasy
  11. In Java, why might composition be preferred over inheritance when designing a class to calculate discounts for different product categories?Multiple choiceEasy
  12. What issue might arise from using deep inheritance hierarchies?Multiple choiceHard
  13. In an e-commerce application, why might you choose composition over inheritance for a 'ShoppingCart' class?Multiple choiceHard
  14. How can you prevent a class from being subclassed in Java?Multiple choiceHard
  15. How would you explain a common problem with using deep inheritance hierarchies in Java application design in an interview?Multiple choiceHard
  16. Which Java feature allows a class to inherit behavior from multiple interfaces?Multiple choiceEasy
  17. Which of these statements is true about abstract classes and interfaces in Java?Multiple choiceMedium
  18. Why does a constructor in a Java class matter in practice?Multiple choiceEasy
  19. Which of the following scenarios demonstrates a poor use of inheritance?Multiple choiceMedium
  20. Why is it useful to declaring a class as abstract in Java?Multiple choiceEasy
  21. Consider the following class. Which statement accurately describes the constructor's role in object creation?Multiple choiceHard
  22. In which scenario would an abstract class be more appropriate than an interface?Multiple choiceMedium
  23. How would you explain a method in a Java class in an interview?Multiple choiceMedium
  24. Which of the following is true about the constructor name in Java?Multiple choiceEasy
  25. How would you explain a drawback of using inheritance over composition when implementing a 'NotificationService' that supports multiple channels like email,…Multiple choiceHard
  26. Which access modifier should be used for a method that is only intended to be used within its own class?Multiple choiceEasy
  27. What keyword is used to create a new object in Java?Multiple choiceEasy
  28. How does an abstract class differ from a concrete class?Multiple choiceMedium
  29. Why does the difference between a class and an object matter in practice?Multiple choiceEasy
  30. Which practice is beneficial for maintaining encapsulation when designing a class's API?Multiple choiceMedium
  31. Why is composition often favored over inheritance in object-oriented design?Multiple choiceEasy
  32. Which Java keyword would you use to allow a subclass in a different package to access a superclass's member?Multiple choiceMedium
  33. How can an abstract class enforce a contract for its subclasses?Multiple choiceMedium
  34. In Java, what is an object?Multiple choiceEasy
  35. In what scenario would using an abstract class be a limitation compared to an interface?Multiple choiceHard
  36. What issue might arise if the constructor of a class sets a mutable object field directly instead of a defensive copy?Multiple choiceHard
  37. Given a 'Customer' class with a 'name' field, which method would be most appropriate to include?Multiple choiceMedium
  38. How would you explain an effective way to ensure immutability in a 'Customer' class in a Java application in an interview?Multiple choiceHard
  39. Which design pattern is most closely associated with favoring composition over inheritance?Multiple choiceMedium
  40. Which Java keyword would you use to call one constructor from another in the same class?Multiple choiceEasy
  41. In an e-commerce application, why might a 'CartItem' class have a reference to a 'Product' class?Multiple choiceHard
  42. What will happen if you try to declare two fields with the same name in a Java class?Multiple choiceMedium
  43. In the context of the 'Open/Closed Principle', how does composition support this principle?Multiple choiceMedium
  44. Why should you prefer composition over inheritance when designing classes?Multiple choiceHard
  45. Which access modifier should you use for a method that should only be accessible by classes in the same package?Multiple choiceEasy
  46. What happens if a Java class has a constructor with parameters but no default constructor?Multiple choiceMedium
  47. How would you explain a key characteristic of a constructor in a Java interview?Multiple choiceEasy
  48. How does encapsulation contribute to safe object design?Multiple choiceMedium
  49. If a class has no constructor defined, what does Java provide by default?Multiple choiceMedium
  50. Consider the following code snippet. What is the issue with the constructor?Multiple choiceMedium