OOP & SOLID Junior interview questions and answers
75 questions for a OOP & SOLID (Junior) interview - with correct answers, explanations and links to the docs.
- 1.What is the difference between a class and an object?
- 2.Which four ideas are usually called the pillars of OOP?
- 3.What does this code print?
- 4.What is a constructor for?
- 5.Which statement about encapsulation is correct?
- 6.Why is the
balancefield better kept private here? - 7.What is the difference between
privatein TypeScript and#fieldin JavaScript? - 8.Who can access a
protectedmember in TypeScript or Java? - 9.What is abstraction in OOP?
- 10.What is inheritance?
- 11.What does this code print?
- 12.What is polymorphism?
- 13.What is the difference between overriding and overloading?
- 14.What does
super.method()do inside an overriding method? - 15.Why does this subclass constructor fail?
- 16.What is an abstract class?
- 17.What happens with this code in TypeScript?
- 18.What is an interface in OOP?
- 19.In Java, how many classes can a class extend and how many interfaces can it implement?
- 20.When is an abstract class a better choice than an interface?
- 21.Which relation is modeled with inheritance and which with composition?
- 22.Which design is better for a car that needs an engine?
- 23.What does "favor composition over inheritance" mainly protect against?
- 24.What is a static method?
- 25.What does this code print?
- 26.What are getters and setters for?
- 27.Why is a class with a public getter and setter for every field weakly encapsulated?
- 28.What does this code print?
- 29.In Java, what is the difference between
==andequals()for objects? - 30.In Java, if you override
equals(), what else must you override? - 31.What does
instanceofcheck? - 32.What is an immutable object?
- 33.Why are immutable objects easier to work with?
- 34.What problem does this code have?
- 35.What does the S in SOLID stand for?
- 36.A
UserServiceclass validates input, saves users to the database, sends welcome emails and builds PDF reports. Which principle does it break most clearly? - 37.How does Robert Martin phrase the Single Responsibility Principle?
- 38.What does the Open/Closed Principle say?
- 39.Which code violates the Open/Closed Principle?
- 40.What does the Liskov Substitution Principle require?
- 41.Why does this hierarchy break LSP?
- 42.What does the Interface Segregation Principle say?
- 43.A
Printerinterface hasprint(),scan()andfax(). A simple printer class has to throw fromscan()andfax(). What is the fix? - 44.What does the Dependency Inversion Principle say?
- 45.Which version follows the Dependency Inversion Principle?
- 46.What is dependency injection?
- 47.Why does dependency injection make a class easier to test?
- 48.What does "high cohesion" mean for a class?
- 49.What does "low coupling" between classes mean?
- 50.What is a "God object"?
- 51.What does DRY stand for?
- 52.A method takes
(name, street, city, zip, country, phone, email). Which refactoring helps? - 53.What is "primitive obsession"?
- 54.What is
thisinside a method called asuser.greet()? - 55.What does this code print?
- 56.In Python, what is
selfin a method? - 57.In Python, what does
__init__do? - 58.In Python, what does a leading underscore in
self._cachemean? - 59.What is duck typing?
- 60.Does this TypeScript code compile?
- 61.What does
implementsgive a TypeScript class? - 62.What does
readonlyon a TypeScript field do? - 63.What does
finalon a Java class mean? - 64.What is the difference between aggregation and composition?
- 65.What does this code print?
- 66.What is a code smell?
- 67.What is refactoring?
- 68.Why does a
switchon an object's type repeated in many places hint at missing polymorphism? - 69.What does an interface give the code that uses it?
- 70.In Java, what is the default
toString()of an object that doesn't override it? - 71.What is method overloading in TypeScript?
- 72.What is a Java record?
- 73.Which statement about OOP and classes is correct?
- 74.Which line hints at a Law of Demeter violation?
- 75.In Python, how does a
@classmethoddiffer from a regular method?