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 9 of 10)
- Why does a class in Java matter in practice?Multiple choiceEasy
- 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
- How would you explain an invariant in object design in an interview?Multiple choiceHard
- Given the following code, how do CartItem and Product communicate?Multiple choiceMedium
- How would you explain a potential issue when a class exposes its fields as public in an interview?Multiple choiceEasy
- Which situation could lead to a problem when a subclass overrides a method?Multiple choiceHard
- Which pattern should be used to ensure that a Product's price does not fall below a minimum allowable value?Multiple choiceHard
- In the context of encapsulation, what is the primary benefit of validating data in a constructor?Multiple choiceMedium
- 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
- What role does a constructor play in the creation of objects in Java?Multiple choiceMedium
- Which design principle is violated if a Cart class directly changes a Product's availability status?Multiple choiceHard
- 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
- Which of the following is not true about method overriding?Multiple choiceEasy
- How does Java achieve polymorphism with interfaces?Multiple choiceMedium
- Consider the following code. What will be the output when the 'processOrder' method is called with a 'DigitalProduct'?Multiple choiceMedium
- 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
- With a superclass reference pointing to a subclass object, what prints here?Multiple choiceHard
- What issue arises if CartItem duplicates Product fields such as name and price?Multiple choiceHard
- Why does 'this' keyword in a constructor matter in practice?Multiple choiceEasy
- In Java, what happens when you assign one object reference to another?Multiple choiceEasy
- How can a subclass ensure that it maintains the contract of a superclass method while still providing additional functionality?Multiple choiceHard
- How can immutability help in maintaining invariants within an object?Multiple choiceHard
- Which of the following scenarios can lead to a compile-time error when overriding a method?Multiple choiceHard
- Which of the following statements about method overriding is true?Multiple choiceMedium
- In the context of method overriding, what role does the super keyword play?Multiple choiceMedium
- What is substitutability in the context of Java polymorphism?Multiple choiceEasy
- Why might a Product class use a setPrice(double price) method?Multiple choiceMedium
- Consider the following code snippet. What will be the output?Multiple choiceMedium
- Why does method overriding in Java help in practice?Multiple choiceEasy
- In a class design, why might you choose to use an immutable object for sensitive data?Multiple choiceHard
- Why do teams use interfaces in Java in production code?Multiple choiceEasy
- Why does making a class immutable in Java help in practice?Multiple choiceMedium
- How does returning a copy of a list in a getter method help maintain encapsulation?Multiple choiceMedium
- 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
- In an e-commerce application, how would you safely share a 'Product' object across multiple threads?Multiple choiceHard
- What can cause a ClassCastException in a polymorphic Java application?Multiple choiceHard
- Which keyword would you use to call another constructor within the same class?Multiple choiceMedium
- Why might excessive use of polymorphism affect performance in a Java application?Multiple choiceHard
- Why can a ShoppingCart that directly modifies Product stock levels become a bug in practice?Multiple choiceHard
- Which of the following is the best practice for managing access to an object's fields in Java?Multiple choiceEasy
- How does polymorphism benefit code maintainability in Java?Multiple choiceEasy
- 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
- How does polymorphism reduce the dependency between application modules?Multiple choiceHard
- What prints here? Product item = new DigitalProduct(); System.out.println(item.label());?Multiple choiceMedium
- In a customer management system, which class should own the logic for verifying customer age?Multiple choiceMedium
- Why does a constructor often use this.name = name;?Multiple choiceMedium
- Given the class 'Product', how can you ensure that a 'CartItem' has a reference to a 'Product'?Multiple choiceMedium
- How would you explain an instance variable in a Java class in an interview?Multiple choiceEasy
- Which Java feature allows different classes to share a method signature but provide different implementations?Multiple choiceMedium
- In the following code snippet, why does using 'this' cause a compile error? public class Store { private String name; public static void…Multiple choiceHard