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 1 of 10)
- When comparing two String objects, what's the difference between == and equals()?Multiple choiceEasy
- What does this program print?What does this code output?Medium
- Which statements about the equals/hashCode contract are true? Select all that apply.Select all that applyMedium
- In modern Java (8+), when would you choose an abstract class over an interface? Compare the two.Interview answerMedium
- What issue might arise if a constructor directly assigns a mutable object without copying it?Multiple choiceHard
- What happens if a subclass does not implement all abstract methods from its abstract superclass?Multiple choiceEasy
- What does the 'this' keyword refer to within an instance method of a class?Multiple choiceMedium
- Which Java keyword would you use to declare an abstract class in Java?Multiple choiceEasy
- Which statement correctly creates an instance of the 'Product' class?Multiple choiceMedium
- How does using interfaces in Java promote flexibility in application design?Multiple choiceEasy
- In Java, why might composition be preferred over inheritance when designing a class to calculate discounts for different product categories?Multiple choiceEasy
- What issue might arise from using deep inheritance hierarchies?Multiple choiceHard
- In an e-commerce application, why might you choose composition over inheritance for a 'ShoppingCart' class?Multiple choiceHard
- How can you prevent a class from being subclassed in Java?Multiple choiceHard
- How would you explain a common problem with using deep inheritance hierarchies in Java application design in an interview?Multiple choiceHard
- Which Java feature allows a class to inherit behavior from multiple interfaces?Multiple choiceEasy
- Which of these statements is true about abstract classes and interfaces in Java?Multiple choiceMedium
- Why does a constructor in a Java class matter in practice?Multiple choiceEasy
- Which of the following scenarios demonstrates a poor use of inheritance?Multiple choiceMedium
- Why is it useful to declaring a class as abstract in Java?Multiple choiceEasy
- Consider the following class. Which statement accurately describes the constructor's role in object creation?Multiple choiceHard
- In which scenario would an abstract class be more appropriate than an interface?Multiple choiceMedium
- How would you explain a method in a Java class in an interview?Multiple choiceMedium
- Which of the following is true about the constructor name in Java?Multiple choiceEasy
- How would you explain a drawback of using inheritance over composition when implementing a 'NotificationService' that supports multiple channels like email,…Multiple choiceHard
- Which access modifier should be used for a method that is only intended to be used within its own class?Multiple choiceEasy
- What keyword is used to create a new object in Java?Multiple choiceEasy
- How does an abstract class differ from a concrete class?Multiple choiceMedium
- Why does the difference between a class and an object matter in practice?Multiple choiceEasy
- Which practice is beneficial for maintaining encapsulation when designing a class's API?Multiple choiceMedium
- Why is composition often favored over inheritance in object-oriented design?Multiple choiceEasy
- Which Java keyword would you use to allow a subclass in a different package to access a superclass's member?Multiple choiceMedium
- How can an abstract class enforce a contract for its subclasses?Multiple choiceMedium
- In Java, what is an object?Multiple choiceEasy
- In what scenario would using an abstract class be a limitation compared to an interface?Multiple choiceHard
- What issue might arise if the constructor of a class sets a mutable object field directly instead of a defensive copy?Multiple choiceHard
- Given a 'Customer' class with a 'name' field, which method would be most appropriate to include?Multiple choiceMedium
- How would you explain an effective way to ensure immutability in a 'Customer' class in a Java application in an interview?Multiple choiceHard
- Which design pattern is most closely associated with favoring composition over inheritance?Multiple choiceMedium
- Which Java keyword would you use to call one constructor from another in the same class?Multiple choiceEasy
- In an e-commerce application, why might a 'CartItem' class have a reference to a 'Product' class?Multiple choiceHard
- What will happen if you try to declare two fields with the same name in a Java class?Multiple choiceMedium
- In the context of the 'Open/Closed Principle', how does composition support this principle?Multiple choiceMedium
- Why should you prefer composition over inheritance when designing classes?Multiple choiceHard
- Which access modifier should you use for a method that should only be accessible by classes in the same package?Multiple choiceEasy
- What happens if a Java class has a constructor with parameters but no default constructor?Multiple choiceMedium
- How would you explain a key characteristic of a constructor in a Java interview?Multiple choiceEasy
- How does encapsulation contribute to safe object design?Multiple choiceMedium
- If a class has no constructor defined, what does Java provide by default?Multiple choiceMedium
- Consider the following code snippet. What is the issue with the constructor?Multiple choiceMedium