NEW
Font size
WorksheetsPython-Sem-II
Total questions: 10
Worksheet time: 15mins
1. What is the purpose of the (__init__) method in a Python class?
A. To initialize class attributes
B. To define class methods
C. To create a class object
D. To destroy class instances
2. How do you call a method named display inside an object obj of class Student?
A. Student.display(obj)
B. obj.display(Student)
C. obj.display()
D. Student(obj).display()
A. Buddy says Woof!
Buddy is fetching the ball.
B. Woof!
Buddy is fetching the ball.
C. Buddy says Woof!
D. Error: Labrador class does not have a bark method.
A. Buddy is 3 years old.
Max is 2 years old.
B. Buddy is 3 years old.
Woof!
C. Buddy Woof!
Max is 2 years old.
D. Error: Dog class does not have a name or bark attribute.
5. What will be the output of the below code?
class Rectangle:
def init(self, length, width):
self.length = length
self.width = width
def calculate_area(self):
return self.length * self.width
rectangle = Rectangle(4, 5)
print(rectangle.calculate_area())
A. 20
B. 9
C. 45
D. Error: Rectangle class does not have a calculate_area method.
A. Alice, 20
B. Student: Alice, Age: 20
C. Error: Student class does not have a display_info method.
D. Student: Age: Alice, 20
7. Fill in the missing code to properly initialize the “Person” class -
class Person:
def init(self, name, age):
# Your code here
def display_info(self):
return f"Name: {self.name}, Age: {self.age}"
person = Person("John", 25)
print(person.display_info())
8. Fill in the missing code to calculate the area of the circle in the “Circle” class.
class Circle:
def init(self, radius):
self.radius = radius
def calculate_area(self):
# Your code here
circle = Circle(4)
print(circle.calculate_area())
A. return Circle.pi * self.radius * self.radius
B. return self.radius * self.radius
C. return 2 * 3.14 * self.radius
D. return 3.14 * self.radius * self.radius
9. Fill in the blank to properly initialize the “Animal” class.
class Animal:
def init(self, species, sound):
____________________
____________________
def make_sound(self):
print(f"The {self._species} makes a {self._sound} sound.")
lion = Animal("Lion", "roar")
dog = Animal("Dog", "bark")
lion.make_sound()
dog.make_sound()
A. self.Species = species
self._sound = sound
B. self._species = species
self._sound = sound
C. self._species = species
D. self._species = Species
self._Sound = sound
10. Fill in the missing code for the below snippet code -
class Car:
def set_car_info(self, make, model, year):
self._make = make
self._model = model
self._year = year
self._is_running = False
car1 = Car()
#Your code here
A. Car1 = set_car_info("Toyota", "Camry", 2020)
B. car1->set_car_info("Toyota", "Camry", 2020)
C. Car1 = Car(set_car_info("Toyota", "Camry", 2020))
D. car1.set_car_info("Toyota", "Camry", 2020)
