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 7 of 10)
- Which Java keyword would you use to declare an interface in Java?Multiple choiceEasy
- Why might a developer choose to use an abstract class instead of an interface in Java?Multiple choiceMedium
- How would you explain a key advantage of defining a method to check stock availability within the Product class in an interview?Multiple choiceMedium
- Which of the following is not a characteristic of interfaces in Java?Multiple choiceEasy
- Given the following code, which statement is true about the class OnlineOrder?Multiple choiceMedium
- Why might it be problematic to allow direct access to an object's fields?Multiple choiceHard
- In Java, which of the following best describes the concept of encapsulation?Multiple choiceEasy
- How does the concept of immutability relate to encapsulation in Java?Multiple choiceMedium
- Consider an e-commerce application where you need to process different types of payments. How can interfaces help in designing the payment system?Multiple choiceMedium
- In an e-commerce system, why should a method to apply discounts be placed in the CartItem class?Multiple choiceMedium
- What is one common practice to achieve immutability in Java classes?Multiple choiceEasy
- Which access modifier in Java ensures that a field is only accessible within its own class?Multiple choiceEasy
- Which of the following best describes the benefit of moving behavior closer to data in object-oriented programming?Multiple choiceHard
- What happens if a subclass in Java does not provide an implementation for an abstract method from its abstract superclass?Multiple choiceMedium
- In the context of shared mutable state, which technique helps prevent data races in a multithreaded environment?Multiple choiceHard
- What does trying to access a non-static field from a static method in Java evaluate to?Multiple choiceEasy
- What access level should be provided to mutable fields in a class that follows encapsulation principles?Multiple choiceMedium
- What can be a consequence of poor field design in a Java class?Multiple choiceHard
- Why would a 'Product' class in an e-commerce application include a field for 'price'?Multiple choiceMedium
- What does the 'is-a' relationship signify in object-oriented programming?Multiple choiceEasy
- Why can not using encapsulation become a problem in practice?Multiple choiceEasy
- Why doesn't every field deserve a setter?Multiple choiceEasy
- In Java, what access level allows a field to be accessed only within its own class?Multiple choiceEasy
- Why does an instance method in a Java class matter in practice?Multiple choiceEasy
- If a class implements two interfaces with methods that have the same signature, what must the class do?Multiple choiceHard
- In designing a 'Customer' class, what could be a downside of using auto-generated getters and setters for all fields?Multiple choiceHard
- When designing a class for an e-commerce application, under which condition would you choose to make it immutable?Multiple choiceHard
- Consider a 'CartItem' class with a field 'quantity'. How might changing the 'quantity' field affect object behavior?Multiple choiceHard
- In terms of encapsulation, why is it important for a class to control the visibility of its fields?Multiple choiceEasy
- How does the state of an object affect its behavior in Java?Multiple choiceMedium
- Why does a getter method in a well-encapsulated class matter in this design?Multiple choiceEasy
- In Java, which of the following statements correctly describes a subclass?Multiple choiceEasy
- Why is immutability a desirable property for objects representing value-like data, such as a customer's profile?Multiple choiceMedium
- Why might a developer choose to use an interface over an abstract class in Java?Multiple choiceHard
- Why should setters in a class be avoided or minimized?Multiple choiceMedium
- Why might the Liskov Substitution Principle be violated in an inheritance hierarchy?Multiple choiceHard
- Why do teams use private fields in a Java class in production code?Multiple choiceMedium
- What keyword is used to specify that a class implements an interface in Java?Multiple choiceEasy
- In a typical e-commerce application, how should a CartItem object access the price of a Product?Multiple choiceEasy
- What design principle is violated if a Cart class directly manipulates a Product's price?Multiple choiceMedium
- Why might CartItem hold a Product reference instead of copying every product field into loose variables?Multiple choiceMedium
- In the context of e-commerce, which class is most likely to contain a method called calculateTotalPrice()?Multiple choiceEasy
- What access modifier should be used for fields to adhere to encapsulation principles?Multiple choiceEasy
- How would you explain a key reason to use encapsulation when designing an object with multiple responsibilities in an interview?Multiple choiceMedium
- How does encapsulation contribute to a class's responsibility?Multiple choiceMedium
- Given the code snippet, what is the result of calling 'getOrderDetails' on a 'SpecialOrder' object?Multiple choiceHard
- Consider a method that accepts an 'Order' interface. What is a benefit of this design?Multiple choiceMedium
- Given a class with a price field, how can you ensure the price is never negative?Multiple choiceHard
- Consider a Product class with a price field. Which approach ensures the price is always positive?Multiple choiceMedium
- Which Java keyword would you use to define that a subclass method is providing its own implementation of a superclass method?Multiple choiceEasy