yoklainterview sim

Backend Python Typing Oop Interview Questions

75 verified Backend Python Typing Oop interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Python Typing OopDifficulty 1
def add(x: int, y: int) -> int:
    return x + y

result = add("3", "4")

What happens when this code runs?
  • aIt runs fine; result becomes the string "34"
  • bIt raises a TypeError as soon as the function is defined
  • cIt raises a TypeError when the function is called
  • dIt runs fine; result becomes the integer 7
Explanation:Type hints are purely advisory metadata for tools like mypy; the interpreter never checks them at runtime. "3" + "4" is plain string concatenation, so result is "34".
Python Typing OopDifficulty 1
In terms of the typing module's semantics, what does the annotation Optional[int] mean for a variable?
  • aThe value must always be an int, and mypy enforces this at runtime
  • bThe value is either an int or None, shorthand for Union[int, None]
  • cDeclaring the variable in the function signature is itself optional
  • dThe value is a generic type usable only as a class attribute
Explanation:Optional[X] is defined by the typing module as an alias for Union[X, None] — the value may be X or it may be None, nothing more.
Python Typing OopDifficulty 1
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p1 = Point(1, 2)
p2 = Point(1, 2)
print(p1 == p2)
print(p1)

What is printed?
  • aFalse, then <__main__.Point object at 0x...>
  • bTrue, then a Point object with fields hidden
  • cTrue, then Point(x=1, y=2)
  • dFalse, then Point(x=1, y=2)
Explanation:@dataclass auto-generates __init__, a field-by-field __eq__, and a readable __repr__. Since p1 and p2 have equal fields, == is True, and printing p1 shows Point(x=1, y=2).
Python Typing OopDifficulty 1
class Circle:
    def __init__(self, radius):
        self._radius = radius

    @property
    def area(self):
        return 3.14159 * self._radius ** 2

c = Circle(2)
print(c.area)

What is printed and how is area accessed?
  • aA TypeError, because area must be called as c.area()
  • b3.14159, treated as a plain attribute set in __init__
  • cAn AttributeError, because area was never set
  • d12.56636, recomputed on each access via @property
Explanation:@property turns a method into something accessed like an attribute (no parentheses). Each access to c.area recomputes 3.14159 2 * 2 = 12.56636.
Python Typing OopDifficulty 1
In a Python class, what is the key difference between a class variable defined directly in the class body and an instance variable assigned via self in __init__?
  • aClass variables can only hold integers, unlike instance variables
  • bA class variable is shared across instances unless one shadows it
  • cInstance variables run at import time, class ones at call time
  • dThere is no real difference between the two in Python
Explanation:A class variable lives on the class object itself and is looked up by every instance that doesn't shadow it; an instance variable is stored per-instance and never shared.
Python Typing OopDifficulty 1
def add_item(item, basket=[]):
    basket.append(item)
    return basket

print(add_item("apple"))
print(add_item("banana"))

What is printed on the second call?
  • a["banana"], a fresh empty list each call
  • bA TypeError, since default arguments can't be mutable
  • c["apple", "banana"], the default list is reused
  • d["banana", "apple"], calls run in reverse order
Explanation:Default argument values are evaluated once, when the function is defined, and that same list object is reused on every call that omits basket — so items accumulate across calls.

Test yourself against the 3300-question Backend bank.

Start interview