wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python-Sem-II

Total questions: 10

Worksheet time: 15mins

Name
Class
Date
1.

1. What is the purpose of the (__init__) method in a Python class?

a)

A. To initialize class attributes

b)

B. To define class methods

c)

C. To create a class object

d)

D. To destroy class instances

2.

2. How do you call a method named display inside an object obj of class Student?

a)

A. Student.display(obj)

b)

B. obj.display(Student)

c)

C. obj.display()

d)

D. Student(obj).display()

3.

3. What will be the output of the below code?

class Dog:

    def init(self, name):

        self.name = name

    def bark(self):

        return f"{self.name} says Woof!"

class Labrador(Dog):

    def fetch(self):

        return f"{self.name} is fetching the ball."

labrador = Labrador("Buddy")

print(labrador.bark())

print(labrador.fetch())

a)

A. Buddy says Woof!

     Buddy is fetching the ball.

b)

B. Woof!

     Buddy is fetching the ball.

c)

C. Buddy says Woof!

d)

D. Error: Labrador class does not have a bark method.

4.

4. What will be the output of the below code?

class Dog:

    def init(self, name, age):

        self.name = name

        self.age = age

    def bark(self):

        return "Woof!"

dog1 = Dog("Buddy", 3)

dog2 = Dog("Max", 2)

print(dog1.name + " is " + str(dog1.age) + " years old.")

print(dog2.bark())

a)

A. Buddy is 3 years old.

     Max is 2 years old.

b)

B. Buddy is 3 years old.

     Woof!

c)

C. Buddy Woof!

     Max is 2 years old.

d)

D. Error: Dog class does not have a name or bark attribute.

5.

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)

A. 20

b)

B. 9

c)

C. 45

d)

D. Error: Rectangle class does not have a calculate_area method.

6.

6. What will be the output of the below code?

class Student:

    def init(self, name, age):

        self.name = name

        self.age = age

    def display_info(self):

        return f"Student: {self.name}, Age: {self.age}"

student = Student("Alice", 20)

print(student.display_info())

a)

A. Alice, 20

b)

B. Student: Alice, Age: 20

c)

C. Error: Student class does not have a display_info method.

d)

D. Student: Age: Alice, 20

7.

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())

a)

A. self.name = name

b)

B. name = self.name 

     age = self.age

c)

C. self.name = name

     self.age = age

d)

D. Person.__init__(self, name, age)

8.

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)

A. return Circle.pi * self.radius * self.radius

b)

B. return self.radius * self.radius

c)

C. return 2 * 3.14 * self.radius

d)

D. return 3.14 * self.radius * self.radius

9.

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)

A. self.Species = species

   self._sound = sound

b)

B. self._species = species

   self._sound = sound

c)

C. self._species = species

d)

D. self._species = Species

   self._Sound = sound

10.

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)

A. Car1 = set_car_info("Toyota", "Camry", 2020)

b)

B. car1->set_car_info("Toyota", "Camry", 2020)

c)

C. Car1 = Car(set_car_info("Toyota", "Camry", 2020))

d)

D. car1.set_car_info("Toyota", "Camry", 2020)