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