Loading samples...
Loading samples...
py-sample-14
Classes, objects, inheritance, and encapsulation
Which keyword defines a class?
What is __init__?
What is self in instance methods?
Which creates instance of class Foo?
What is inheritance in OOP?
Which method is called when object created?
What does super() do?
Which naming convention indicates private?
What is @property decorator for?
What is __str__ used for?
Explain the difference between class and object with examples.
[3 marks]What are the four pillars of OOP? Explain each briefly.
[3 marks]Explain __init__, __str__, and __repr__ methods.
[3 marks]What is the difference between class methods and static methods?
[3 marks]Explain single vs multiple inheritance in Python.
[3 marks]What is encapsulation? How is it achieved in Python?
[3 marks]Explain the purpose of self and cls parameters.
[3 marks]What is method overriding? When is it useful?
[3 marks]Explain __name__ == '__main__' in OOP context.
[3 marks]What are abstract base classes? How to create them?
[3 marks]Create a BankAccount class with deposit, withdraw, and balance methods.
[5 marks]Write a Rectangle class with area and perimeter methods. Include validation.
[5 marks]Create a Student class with inheritance: Person -> Student -> GraduateStudent.
[5 marks]Implement a counter class using __iter__ and __next__ for custom iteration.
[5 marks]Create a Vector class supporting + and * operators using operator overloading.
[5 marks]Write a Singleton class ensuring only one instance exists.
[5 marks]Find error: class Foo: def __init__(): self.x = 10 f = Foo()
[2 marks]Explanation:__init__ needs self parameter. Should be def __init__(self):.
Predict output: class Counter: count = 0 def __init__(self): Counter.count += 1 a = Counter() b = Counter() print(Counter.count)
[3 marks]Explanation:count is class variable shared by all instances. Each __init__ increments it.
Find error: class Parent: def greet(self): print('Hello') class Child(Parent): def greet(self): super.greet() print('Hi') c = Child() c.greet()
[2 marks]Explanation:super() needs parentheses. Should be super().greet() in Python 3.
Predict output: class Box: def __init__(self, x): self.__x = x b = Box(5) print(b._Box__x)
[3 marks]Explanation:Name mangling: __x becomes _Box__x. Can still access via mangled name, though not recommended.
Find error: class Math: @staticmethod def add(a, b): return a + b m = Math() print(m.add(2, 3))
[2 marks]Explanation:Static methods can be called on instance or class. Both work fine.
Predict behavior: class A: x = 1 a1 = A() a2 = A() a1.x = 2 print(a2.x) print(A.x)
[3 marks]Explanation:a1.x creates instance variable. a2.x still refers to class variable (1). A.x is 1.
Find error: class Circle: def __init__(self, r): self.r = r def area(self): return 3.14 * r ** 2 c = Circle(5) print(c.area())
[2 marks]Explanation:Should use self.r to access instance variable. r alone refers to undefined local variable.
Predict output: class Num: def __init__(self, n): self.n = n def __str__(self): return f'Num({self.n})' def __repr__(self): return str(self.n) n = Num(42) print(n) print([n])
[3 marks]Explanation:print() uses __str__. List uses __repr__ for elements. Different representations.