SchoolWhool
Quizzes and saved progress, freeSign in freeSign in
Python · Module 6Module material

Python OOP interview questions

25 questions interviewers ask on objects and classes, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.

Classes and objects

  1. What is the difference between a class and an instance?●

    A class is a definition of a kind of object, class Review:. An instance is one object created from it, review = Review("kettle", 5, "…"), with its own attributes. type(review) is <class '__main__.Review'>.

  2. What is self?●

    The conventional first parameter of an instance method: the object the method was called on. review.is_positive() is the same call as Review.is_positive(review), and both give True. self is a convention, not a keyword, but every Python style guide uses it.

  3. What does __init__ do? Is it the constructor?●

    It initialises a new object: Python creates the object, then calls __init__ on it with the arguments passed to the class. It is usually called the constructor. It must return None; its job is to set attributes such as self.rating = rating.

  4. What happens if a method has no self parameter?●●
  5. Where are an object's attributes stored?●●
  6. Why is setting a misspelt attribute dangerous?●●
  7. What is the difference between == and is for your own objects?●●
  8. What are special methods (dunder methods)? Give examples.●●
  9. What is the difference between __repr__ and the default display?●●
  10. "Everything in Python is an object." What does that mean in practice?●

Inheritance

  1. What is inheritance?●
  2. What does super().__init__(...) do, and what happens without it?●●
  3. How do you define a custom exception, and why subclass a built-in one?●●

Dataclasses

  1. What does @dataclass generate?●
  2. Does a dataclass check its type hints?●●
  3. Why can a dataclass field not have [] as its default?●●
  4. What does frozen=True do, and when would you use it?●●
  5. What is a decorator?●●

The estimator pattern

  1. What is the estimator pattern?●
  2. Why do learned attributes end in an underscore?●●
  3. Why does fit return self?●●
  4. What is the difference between a parameter and a hyperparameter?●●
  5. How does k-nearest neighbours regression predict?●●
  6. Why should fit store a copy of the data it is given?●●
  7. Why is a model's error on its own training data a poor measure?●●