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
equalsandhashCodetogether: equal objects must have equal hash codes. - Interfaces can have
defaultandstaticmethods (Java 8+) but no instance state. Abstract classes can hold state and constructors. Stringis immutable; compile-time constant strings are interned.
Questions (page 3 of 10)
- Why does Chapter 2 move from loose variables to classes like Product and CartItem?Multiple choiceEasy
- In the context of Java, what is an object?Multiple choiceEasy
- Why does missing to initialize a field in a constructor change runtime behavior?Multiple choiceMedium
- When is an abstract class usually a better fit than an interface?Multiple choiceEasy
- Why can exposing a mutable object through a getter method in a class become a real problem?Multiple choiceMedium
- Why does providing multiple constructors in a Java class help in practice?Multiple choiceMedium
- Which design principle is violated if a class hierarchy is built based on shared implementation details rather than shared behavior?Multiple choiceMedium
- Which Java keyword is used to create an object of a class?Multiple choiceMedium
- Why is it generally a bad practice to provide setters for all fields in a class?Multiple choiceHard
- In an e-commerce application, which class should likely contain the method to add an item to the shopping cart?Multiple choiceEasy
- Why is composition often better when tax, discount, or payment behavior changes independently of the main type?Multiple choiceEasy
- In the context of constructors, why is it important to use 'this' keyword?Multiple choiceHard
- Which access modifier allows a field to be accessed from any class in Java?Multiple choiceEasy
- Why would you use private access modifiers for fields in a class?Multiple choiceMedium
- What happens if a constructor is private in a Java class?Multiple choiceMedium
- What keyword is used to inherit from a class in Java?Multiple choiceEasy
- Why would you choose to use methods within a Java class?Multiple choiceMedium
- Given the constructor public Order(String id, List<Item> items), what is a potential design improvement?Multiple choiceMedium
- Checkout needs one discount strategy today and another tomorrow. Which design keeps that change most local?Multiple choiceHard
- Why might it be problematic to perform complex logic in a constructor?Multiple choiceHard
- How would you explain a key characteristic of an abstract class in a Java interview?Multiple choiceEasy
- In Java, what is the effect of marking a class field as 'private'?Multiple choiceEasy
- Why might a deep inheritance hierarchy be problematic in a Java application handling various types of payments?Multiple choiceHard
- In designing a payment processing module, you need to handle credit card and PayPal transactions. Why might you choose composition over inheritance to model…Multiple choiceMedium
- In Java, what allows a class to inherit methods from multiple sources?Multiple choiceEasy
- Why do teams use constructors in a Java class in production code?Multiple choiceMedium
- When designing a Java application, what is a potential downside of using inheritance to create a 'PremiumUser' class that extends a 'User' class?Multiple choiceMedium
- How would you explain the key difference between an abstract class and an interface in a Java interview?Multiple choiceEasy
- Given the constructor public Order(String id, List<Item> items), what is a potential flaw in this design?Multiple choiceHard
- Why is it recommended to group related fields and methods in a class?Multiple choiceMedium
- Which of these statements about abstract classes in Java is true?Multiple choiceMedium
- Which design principle is violated if a 'Product' class directly modifies a 'Customer' class's balance?Multiple choiceHard
- Why does encapsulating fields within a class using private access modifiers help in practice?Multiple choiceHard
- When two classes, 'Product' and 'Category', are modeled in an e-commerce system, what's the most appropriate relationship?Multiple choiceHard
- What happens if a method in a Java class has a return type but does not have a return statement?Multiple choiceMedium
- Which of the following is true about abstract methods in Java?Multiple choiceMedium
- If a 'Customer' class needs to track multiple shipping addresses, which design is most appropriate?Multiple choiceHard
- Why might an e-commerce system's 'Order' class use private setters for its fields?Multiple choiceMedium
- Why does declaring a method as 'final' in a superclass change runtime behavior?Multiple choiceMedium
- Why might a developer choose to use a 'restock' method rather than a public setter for inventory?Multiple choiceHard
- How do unmodifiable views differ from defensive copies in protecting internal collections?Multiple choiceMedium
- In an e-commerce application, what is the advantage of using a 'processOrder' method in a Product class?Multiple choiceMedium
- Which statement is true about interfaces in Java?Multiple choiceMedium
- Why are public mutable fields risky in a store model?Multiple choiceEasy
- Why can generating getters and setters for every field in a class become a problem in production code?Multiple choiceMedium
- Why does marker interfaces in Java matter in this design?Multiple choiceMedium
- What keyword in Java is used to inherit from a superclass?Multiple choiceEasy
- Which access modifier is the best fit for fields to ensure encapsulation?Multiple choiceEasy
- In Java, why should you avoid returning a reference to a mutable internal collection?Multiple choiceEasy
- Which of the following best describes a field in a Java class?Multiple choiceEasy