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 6 of 10)
- Which of the following statements is true regarding immutability and thread safety in Java?Multiple choiceHard
- Which of the following is a characteristic of well-designed fields in a Java class?Multiple choiceEasy
- How can you ensure that a collection inside an immutable class cannot be modified?Multiple choiceHard
- How can encapsulation be achieved in Java?Multiple choiceMedium
- Why would you prefer a method like 'calculateTotalPrice()' in an Order class over a public getter for a 'totalPrice' field?Multiple choiceMedium
- Why do teams use private setters in a class in production code?Multiple choiceMedium
- How would you explain a potential issue when forcing inheritance for reuse in an interview?Multiple choiceHard
- If a method needs to modify the state of an object, what type of method should it be?Multiple choiceMedium
- Why might you use Collections.unmodifiableList in your Java code?Multiple choiceMedium
- In an e-commerce application, what is the primary reason for encapsulating a product's price field?Multiple choiceEasy
- When is immutability often a strong choice?Multiple choiceMedium
- Why does making a class's fields private in Java change runtime behavior?Multiple choiceEasy
- In the context of an e-commerce platform, why might you use a method like 'updateStock(int quantity)' instead of a generic setter for a 'stock' field?Multiple choiceMedium
- Why might a developer use an interface with a single method, known as a functional interface, in Java?Multiple choiceHard
- How does using private fields in a class contribute to encapsulation?Multiple choiceMedium
- What will be the output of calling orderItem.calculateTotal() given the following code?Multiple choiceHard
- Which Java keyword is used to ensure a class's field cannot be modified after initialization?Multiple choiceEasy
- A product can appear in many carts with different quantities. Where does the quantity for one cart line belong most naturally?Multiple choiceMedium
- Why might using inheritance be considered a poor design choice in this scenario?Multiple choiceHard
- In the context of a Customer class, why might exposing an email field with a public setter be problematic?Multiple choiceHard
- Why do teams use interfaces in Java over abstract classes in production code?Multiple choiceEasy
- Why do teams use interfaces in Java in production code?Multiple choiceEasy
- In Java, why should field variables typically be private?Multiple choiceHard
- In the context of Java, what does it mean for a class to be immutable?Multiple choiceMedium
- Which of the following statements correctly defines a final field in a Java class?Multiple choiceEasy
- Why is reserve(int amount) usually safer than a generic setStock(int newStock) method?Multiple choiceMedium
- In the context of encapsulation, what is a potential downside of using too many getter methods?Multiple choiceHard
- In an e-commerce application, which class is a good candidate for immutability?Multiple choiceEasy
- Consider the following code snippet. Why does this inheritance hierarchy violate good design practices?Multiple choiceHard
- If a Product class in an e-commerce system includes a 'discountRate' field, why might you prefer a method like 'applyDiscount()' over a setter?Multiple choiceHard
- Which technique can help maintain class invariants when encapsulating fields in a Java class?Multiple choiceHard
- How would you explain a potential risk of exposing internal mutable collections directly in an interview?Multiple choiceMedium
- How would you explain a valid reason to use default methods in interfaces in a Java interview?Multiple choiceHard
- In an e-commerce application, how would you ensure that a customer's address list cannot be modified after initialization?Multiple choiceHard
- Which of these features was introduced to interfaces in Java 9?Multiple choiceEasy
- How can business operations help in maintaining an object's encapsulation?Multiple choiceMedium
- Why can setting all fields in a Java class to public become a problem in practice?Multiple choiceHard
- Why might you choose to use a getter method instead of making a field publicly accessible?Multiple choiceMedium
- Why do teams use List.copyOf when returning a collection from a method in production code?Multiple choiceMedium
- Which of the following statements about Java interfaces is correct?Multiple choiceMedium
- In an e-commerce application, which of the following fields in a Product class is least likely to need a setter?Multiple choiceEasy
- Why is it generally a bad idea to expose a class's internal fields as public?Multiple choiceMedium
- In a Java class representing a product, why might you choose to make the 'price' field private with no public setter?Multiple choiceEasy
- When is inheritance an honest design choice in Java?Multiple choiceEasy
- Which strategy would best ensure the immutability of a class?Multiple choiceHard
- In an e-commerce application, why is it important to use meaningful update methods like 'restock' instead of direct field manipulation?Multiple choiceHard
- Which principle of OOP is demonstrated by a 'Product' class having a method that checks its own stock level?Multiple choiceHard
- You have a class with a list of orders in an e-commerce system. How can you safely allow external classes to access these orders?Multiple choiceHard
- Which Java keyword would you use to restrict access to class members, ensuring encapsulation?Multiple choiceEasy
- How can private methods in interfaces be useful in Java?Multiple choiceHard