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 5 of 10)
- Why is it beneficial to use getters and setters in Java classes?Multiple choiceMedium
- How does implementing interfaces contribute to loose coupling in Java applications?Multiple choiceMedium
- Why can providing public setter methods for all fields in a class become a problem in production code?Multiple choiceMedium
- In Java, how can instance methods affect the state of an object?Multiple choiceEasy
- In the context of encapsulation, why is it important to declare fields as private in a Java class?Multiple choiceEasy
- In an e-commerce application, a Product class has a discount field. Why is it important to encapsulate this field?Multiple choiceMedium
- Why is it generally a good practice to make fields private in a Java class?Multiple choiceEasy
- Given the code snippet below, how can you make the 'orders' list immutable to protect it from external modifications?Multiple choiceHard
- How does making a class immutable help reduce mutation bugs in an e-commerce application?Multiple choiceMedium
- Consider the following code. Which interface method must be implemented by the Product class?Multiple choiceMedium
- A Customer class has fields for name, email, and password. Which field should be handled with extra security considerations?Multiple choiceHard
- In a Java class, what best defines the concept of 'object state'?Multiple choiceEasy
- Given the following code snippet, what is the issue with the 'changeQuantity' method?Multiple choiceHard
- Why do teams use interfaces in Java in production code?Multiple choiceEasy
- Why does encapsulation in object-oriented programming matter in practice?Multiple choiceEasy
- Why does having a well-defined API surface for a class help in practice?Multiple choiceEasy
- What potential issue arises when a mutable object is returned from a method of an immutable class?Multiple choiceHard
- Which feature of Java interfaces allows implementing classes to have method implementations?Multiple choiceEasy
- Which of the following is a reason to use instance methods over static methods?Multiple choiceMedium
- Why might a developer choose to use a method like 'adjustStock' instead of a public setter for inventory?Multiple choiceMedium
- In the context of object-oriented programming, why should behavior be moved closer to the data it operates on?Multiple choiceMedium
- How can a class ensure that its objects remain immutable?Multiple choiceMedium
- Which Java keyword would you use to implement an interface in a Java class?Multiple choiceEasy
- How does encapsulation help in maintaining a trustworthy store model in an e-commerce application?Multiple choiceMedium
- Which of the following best describes an instance method in Java?Multiple choiceEasy
- Why might a 'Product' class in an e-commerce system include a 'category' field?Multiple choiceMedium
- Why can exposing a public getter for a 'password' field in a User class become a problem in production code?Multiple choiceMedium
- In Java, how would you ensure that a Product class's price cannot be changed once it's set at construction?Multiple choiceMedium
- Which of the following is true about implementing multiple interfaces in Java?Multiple choiceMedium
- Which statement best describes the relationship between fields and instance methods in a well-designed Java class?Multiple choiceHard
- Why is it beneficial for a CartItem class to have a method that calculates its own line total?Multiple choiceMedium
- Why is encapsulation important in maintaining class invariants in Java?Multiple choiceEasy
- Given a CartItem class, why might it be beneficial to include a Product field within it?Multiple choiceMedium
- Which keyword in Java is used to restrict access to members of a class?Multiple choiceEasy
- How would you explain a limitation of using inheritance for code reuse in an interview?Multiple choiceMedium
- Why is returning a raw internal List<CartItem> from Cart risky?Multiple choiceHard
- What role does encapsulation play in maintaining invariants in a class?Multiple choiceMedium
- What does declaring a class as final in Java evaluate to?Multiple choiceEasy
- Which of the following is NOT an advantage of encapsulation?Multiple choiceEasy
- Which approach best protects a mutable internal list from external modification in a multi-threaded environment?Multiple choiceHard
- In designing a 'Customer' class, which field should ideally be immutable?Multiple choiceHard
- In the context of an e-commerce application, which method would most appropriately belong within a 'Product' class?Multiple choiceMedium
- In Java, which access modifier would you use to ensure a field is only accessible within its own class?Multiple choiceEasy
- If a class method conflicts with an interface default method of the same signature, which implementation wins?Multiple choiceMedium
- Why is lineTotal() usually a better method on CartItem than one giant procedural utility that takes many separate parameters?Multiple choiceHard
- Given the following code, what will be the output of calling product.displayDetails()?Multiple choiceMedium
- Why should setter methods be avoided in an immutable class?Multiple choiceMedium
- How does the state of an object affect method execution in Java?Multiple choiceMedium
- Which of the following is a benefit of using immutable objects?Multiple choiceMedium
- When is an interface usually a better fit than an abstract class?Multiple choiceMedium