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 8 of 10)
- How can immutability help maintain invariants in a class with complex validation logic?Multiple choiceHard
- How does Java decide which overridden method to call at runtime?Multiple choiceMedium
- Why do teams use getter methods in a class in production code?Multiple choiceEasy
- Which approach should be used to prevent modification of a list returned by a method?Multiple choiceHard
- In an e-commerce application, which class should be responsible for validating a discount code?Multiple choiceMedium
- Which greeting is printed when dynamic dispatch calls Derived.sayHello()?Multiple choiceMedium
- What happens when you cast a superclass reference to a subclass type in Java?Multiple choiceMedium
- Which approach ensures that a method returns an unmodifiable list in Java?Multiple choiceMedium
- Why does fields in a Java class matter in this design?Multiple choiceEasy
- Why is it important to use the @Override annotation when overriding a method in Java?Multiple choiceEasy
- Which of the following best describes a class invariant?Multiple choiceEasy
- What problem might arise if a ShoppingCart directly manipulates Product details without using its interface?Multiple choiceHard
- Why does encapsulation in object-oriented programming matter in practice?Multiple choiceEasy
- 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
- Which class should be responsible for calculating the total price of items in an e-commerce cart?Multiple choiceMedium
- Why is it important to reject invalid stock quantities in an e-commerce application?Multiple choiceMedium
- What happens if you assign a 'Product' object reference in a 'Customer' object, and then set the 'Product' to null?Multiple choiceMedium
- How can a 'Customer' object maintain a list of 'Cart' objects and ensure only one cart is active at a time?Multiple choiceMedium
- In the context of classes, what does 'this' represent?Multiple choiceEasy
- Which Java keyword would you use to declare a class that implements an interface in Java?Multiple choiceEasy
- Which of the following is a correct use of 'this' in a method?Multiple choiceEasy
- Why should you prefer composition over inheritance when designing a Product and Cart relationship?Multiple choiceMedium
- Given a superclass method protected void calculate(), which of the following correctly overrides it in a subclass?Multiple choiceHard
- Why might you create a method in the 'Product' class that returns a deep copy of a 'Product' object?Multiple choiceMedium
- What happens when you call a method on an object stored in a variable of its superclass type?Multiple choiceMedium
- 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
- What design flaw exists if a User class directly accesses ShoppingCart's internal list of items?Multiple choiceHard
- 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
- What does using the 'new' keyword in Java evaluate to?Multiple choiceEasy
- How would you explain the keyword used to refer to the current object instance in a Java interview?Multiple choiceEasy
- How do you prevent a 'Product' instance from being modified after it has been added to a 'Cart'?Multiple choiceHard
- What will happen if you copy an object reference to another variable and then modify the object via the new reference?Multiple choiceMedium
- In Java, why is it important for fields in a class to be marked as private?Multiple choiceEasy
- Consider a class with a method public void display() {}. Which of the following is a valid override in a subclass?Multiple choiceMedium
- Why might you prefer to use an interface instead of an abstract class in Java?Multiple choiceMedium
- Why is 'this()' required to be the first line in a constructor when used?Multiple choiceMedium
- What does upcasting a PremiumCustomerDiscountPolicy to DiscountPolicy actually change?Multiple choiceHard
- In a shopping cart application, how can you prevent a 'Cart' from directly modifying a 'Product'?Multiple choiceHard
- How would you explain a key reason for making a class immutable in a Java interview?Multiple choiceMedium
- 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
- Which of the following describes a 'constructor' in Java?Multiple choiceEasy
- Which Java keyword would you use to explicitly invoke a superclass's method when it has been overridden in a subclass?Multiple choiceMedium
- What does it mean if a method in Product is static?Multiple choiceMedium
- 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
- In the context of method overriding, what does 'same signature' mean?Multiple choiceEasy
- What role does encapsulation play in method validation?Multiple choiceMedium
- In what scenario might polymorphism cause unexpected behavior in Java applications?Multiple choiceHard
- When should you use a constructor in a Java class?Multiple choiceMedium
- What should a method in an encapsulated class do to allow data modification?Multiple choiceEasy
- What design issue might arise if CartItem modifies the Product quantity directly?Multiple choiceHard