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 2 of 10)
- How does Java distinguish between different methods with the same name in a class?Multiple choiceMedium
- Why does declaring a class as 'public' in Java change runtime behavior?Multiple choiceMedium
- How can you ensure that a class cannot be instantiated in Java?Multiple choiceHard
- Why does the 'static' keyword in a field declaration matter in this design?Multiple choiceHard
- Why does encapsulation in Java matter in practice?Multiple choiceEasy
- Which statement is true about constructor overloading?Multiple choiceEasy
- In an e-commerce application, why might you prefer to use composition over inheritance for implementing a 'DiscountStrategy'?Multiple choiceMedium
- Why should a class's internal state not be exposed via public mutable fields?Multiple choiceHard
- Given an abstract class with a concrete method, which statement is true?Multiple choiceHard
- How can constructor chaining be achieved within the same class?Multiple choiceMedium
- In Java, which of the following best describes the relationship between a class and an object?Multiple choiceEasy
- Why does a constructor in a Java class matter in this design?Multiple choiceEasy
- In which scenario is it beneficial to use multiple constructors in a class?Multiple choiceMedium
- Why does using super() in a constructor change runtime behavior?Multiple choiceMedium
- Why does having a private constructor in a Java class change runtime behavior?Multiple choiceEasy
- In a Java class 'Order', which constructor signature is most appropriate for initializing an order with a list of items and a customer?Multiple choiceHard
- Why do teams use interfaces over abstract classes in a Java application in production code?Multiple choiceMedium
- Why are constructors important in beginner object modeling?Multiple choiceMedium
- What happens when a subclass does not implement an abstract method from its superclass?Multiple choiceEasy
- In terms of encapsulation, what is a potential problem with a public field in a class?Multiple choiceMedium
- Why can using public fields in a class become a problem in practice?Multiple choiceMedium
- Which design decision is the best fit for representing a 'Discount' that may apply to multiple products in a store model?Multiple choiceMedium
- In an e-commerce application, you have a class 'Order' that contains a list of 'Product' items. Which of the following responsibilities is best suited for the…Multiple choiceMedium
- Consider this code snippet: What is the visibility of the 'price' field in the Product class?Multiple choiceMedium
- What will be the result if a method tries to modify a field of a class without using the 'this' keyword?Multiple choiceMedium
- Consider a class 'Product'. How can you ensure every 'Product' object has a unique identifier upon creation?Multiple choiceHard
- Given the following class, how can you improve encapsulation?Multiple choiceHard
- When designing a 'Product' class, which attribute is least likely to belong directly to the class?Multiple choiceMedium
- Consider a 'Product' class in an e-commerce application. What is a potential risk if its price field is public?Multiple choiceMedium
- Given the following code snippet, which line will cause a compilation error?Multiple choiceHard
- Why do teams use the 'private' access modifier in a class in production code?Multiple choiceEasy
- What will be the output of the following code snippet if the Product class is used to create multiple objects?Multiple choiceMedium
- Which of the following scenarios best fits the use of an abstract class?Multiple choiceMedium
- What problem can occur if a constructor performs complex logic or throws exceptions?Multiple choiceHard
- In a store model, which class should be responsible for calculating the total price of items in a shopping cart?Multiple choiceMedium
- Why does a method within a Java class matter in practice?Multiple choiceEasy
- Why might an abstract class contain both abstract and concrete methods?Multiple choiceHard
- Which of the following best describes an advantage of using methods in a Java class?Multiple choiceMedium
- Why is it a good design practice to minimize the public surface of a class?Multiple choiceMedium
- How does the use of composition support the open/closed principle in Java?Multiple choiceEasy
- In the context of immutability, why is it important to make defensive copies of internal mutable objects?Multiple choiceHard
- Can an abstract class be instantiated directly in Java?Multiple choiceEasy
- Which of the following best describes a class in Java?Multiple choiceEasy
- Which access modifier allows a class member to be accessed within the same package but not from outside the package?Multiple choiceEasy
- In an e-commerce system, how can encapsulation help manage complex interactions between 'CartItem' objects?Multiple choiceHard
- Consider the following constructor: public Product(String name, double price). What is required to create a new Product object?Multiple choiceEasy
- Consider an e-commerce system where different types of users, such as Admin and Customer, have common behaviors but also distinct actions. Which design choice…Multiple choiceMedium
- How would you explain a key limitation of using abstract classes in a Java interview?Multiple choiceMedium
- How would you explain a constructor's role regarding class fields in an interview?Multiple choiceEasy
- Which of the following best describes a scenario where inheritance is appropriately used in Java?Multiple choiceMedium