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 4 of 10)
- In modern Java, how can interfaces provide additional functionality without breaking existing implementations?Multiple choiceMedium
- Why might a Java class have private fields and public methods?Multiple choiceMedium
- Consider the following code snippet. What is a potential issue with the 'updatePrice' method?Multiple choiceHard
- What does calling List.copyOf on a mutable list evaluate to?Multiple choiceEasy
- Consider the following code snippet. What is one way to ensure that the 'Customer' class is immutable?Multiple choiceHard
- How can defensive copying help in protecting internal state of a class?Multiple choiceMedium
- In an e-commerce application, how can you safely expose a list of available products to a caller?Multiple choiceMedium
- What should a developer consider when adding a method to update object state?Multiple choiceMedium
- Which Java keyword would you use to refer to the current object within an instance method?Multiple choiceEasy
- In Java, what is the effect of making a class's fields protected?Multiple choiceMedium
- How would you explain the real benefit of hiding fields behind meaningful methods in an interview?Multiple choiceMedium
- Why do teams use encapsulation in Java in production code?Multiple choiceMedium
- How would you explain a consequence of not using encapsulation in Java classes in an interview?Multiple choiceMedium
- Why does a constructor in a Java class matter in practice?Multiple choiceMedium
- Given two interfaces with default methods of the same signature, how can a class implementing both resolve the method conflict?Multiple choiceHard
- When is it more appropriate to use composition over inheritance in Java?Multiple choiceMedium
- Given the following code, what is the output of calling product.checkStock()? Assume stock is 10?Multiple choiceHard
- In Java, what role does a constructor play in a class?Multiple choiceEasy
- Why is it often unnecessary to make a method static in a class like CartItem?Multiple choiceMedium
- Why might you choose to implement a domain-specific method instead of a generic setter for updating a field?Multiple choiceMedium
- What would be the result of calling product.updatePrice(50) with the code provided?Multiple choiceMedium
- How would you explain an invariant in the context of Java classes in an interview?Multiple choiceEasy
- Why do teams use encapsulation in Java in production code?Multiple choiceEasy
- Which scenario might justify exposing a public setter for a 'description' field in a Product class?Multiple choiceHard
- When designing a 'User' class for an e-commerce application, what is a key consideration for maintaining encapsulation?Multiple choiceHard
- Consider a class representing a customer's order in an e-commerce application. Which of the following practices helps ensure the class is immutable?Multiple choiceMedium
- In Java, what role do constructor parameters play in ensuring immutability for a class?Multiple choiceMedium
- Which of the following statements about encapsulation is true?Multiple choiceMedium
- In a 'CartItem' class, why is it important to have a method to update 'quantity'?Multiple choiceEasy
- Consider a class with a private list of items in a shopping cart. What is a safe way to allow external classes to view these items?Multiple choiceMedium
- Why does the difference between a field and a local variable in Java matter in practice?Multiple choiceMedium
- How does using interfaces in Java improve testability of code?Multiple choiceMedium
- How would you explain a common technique to avoid unrestricted setters in a Java interview?Multiple choiceEasy
- Which principle is violated if class fields are accessed directly?Multiple choiceEasy
- In Java, what is the effect of declaring a field as 'final'?Multiple choiceEasy
- Consider an e-commerce application with a Product class. Which fields are most appropriate for this class?Multiple choiceMedium
- What keyword is used to refer to the current object within an instance method?Multiple choiceEasy
- Which of the following is a feature of immutable objects?Multiple choiceEasy
- In an e-commerce application, which instance method would be most appropriate for a CartItem class?Multiple choiceEasy
- In a well-encapsulated class, why might a method be preferred over a setter for updating a field?Multiple choiceHard
- Which of the following is true about abstract classes in Java?Multiple choiceMedium
- In an e-commerce application, why is it important to encapsulate the logic for adding an item to a cart?Multiple choiceMedium
- Why is it important to validate inputs when setting an internal collection in a class?Multiple choiceHard
- How would you explain a key reason to use private fields in a Java class in an interview?Multiple choiceMedium
- Given the following code snippet, what will be the output when cartItem.calculateLineTotal() is called?Multiple choiceMedium
- Consider the following code: What is a flaw in using public getters and setters for the 'balance' field in a BankAccount class?Multiple choiceHard
- Why does declaring a class as final in Java change runtime behavior?Multiple choiceEasy
- When designing an immutable class in Java, which principle should you follow to ensure encapsulation?Multiple choiceHard
- In Java, how can you enforce an invariant in a class?Multiple choiceMedium
- In Java, what is the purpose of a constructor?Multiple choiceEasy