Theory Questions
What is a class in Python?
Define an object in Python.
What is the purpose of the __init__
method in a class?
How is inheritance implemented in Python classes?
What is method overriding in Python classes?
What is the self
keyword in Python class methods?
How can you create an instance of a class in Python?
What is the significance of the __str__
method in Python classes?
How can you check if an object is an instance of a particular class?
What is the difference between a constructor and a destructor in Python?
Class and Object PPT
Programming Questions
Create a Simple Class
Class and Object Initialization
Method in a Class
Accessing Class Attributes
Using Class Methods
Constructor and Destructor
Creating an Object
Using Class Variables
Using self
in Methods
Create a Bank Account Class
Output Questions
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person(“Alice”, 25)
print(person1.name, person1.age)
class Student:
school = “XYZ School”
def __init__(self, name):
self.name = name
student1 = Student(“Bob”)
print(student1.name)
print(student1.school)
class MathOperations:
def add(self, a, b):
return a + b
calculator = MathOperations()
result = calculator.add(10, 20)
print(result)
class Animal:
def speak(self):
print(“Animal speaks”)
class Dog(Animal):
def speak(self):
print(“Dog barks”)
dog = Dog()
dog.speak()
class MyClass:
def show(self):
print(“Hello, world!”)
obj = MyClass()
obj.show()
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p1 = Point(1, 2)
p2 = Point(1, 2)
print(p1 == p2)
print(p1.x == p2.x and p1.y == p2.y)
class MathUtility:
@staticmethod
def add(a, b):
return a + b
result = MathUtility.add(10, 20)
print(result)
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
@classmethod
def get_count(cls):
return cls.count
obj1 = MyClass()
obj2 = MyClass()
print(MyClass.get_count())
class MyClass:
def __init__(self):
print(“Constructor called”)
def __del__(self):
print(“Destructor called”)
obj = MyClass()
del obj
class Shape:
def area(self):
print(“Calculate area”)
class Circle(Shape):
def area(self):
print(“Calculate circle area”)
circle = Circle()
circle.area()