Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
Object-Oriented Programming: Classes and objects, class variables and object variables,
class methods and self-arguments, the init method (class constructor), class methods, static
methods, the concept of inheritance, polymorphism
MCQ Questions
1. Which of the following is not a feature of 2. What is the keyword used to create a class in
OOP? Python?
a) Encapsulation a) object
b) Inheritance b) class
c) Compilation c) def
d) Polymorphism d) type
3. Which of the following is not a class method? 4. What is used to create an object?
a) Non-Static a) constructor
b) Static b) class
c) Bounded c) user-defined functions
d) Unbounded d) in-built functions
5. What will be the output of the following 6. What will be the output of the following
Python code? Python code?
class test: class fruits:
def __init__(self,a=""Hello World""): def __init__(self, price):
self.a=a [Link] = price
def display(self): obj=fruits(50)
print(self.a) [Link]=10
obj=test() [Link]=2
[Link]() print([Link]+len(obj.__dict__))
a) The program has an error: constructor can’t a) 12
have default arguments b) 52
b) Nothing is displayed c) 13
c) “Hello World” is displayed d) 60
d) The program has an error: display function
doesn’t have parameters
7. What represents an entity in the real world 8. Which of the following is/are the advantage of
with its identity and behaviour. object-oriented programming?
a) A method a) Code Reusability
b) An object b) Can create more than one instance of the
c) A class class without interference
d) An operator c) Platform Independent
d) All of the above
9. In OOP, what is the relation of the object to 10. Which method is called when an object is
the class? created?
a) class is an instance of object a) __new__()
b) object is a child of class b) __init__()
c) object is method of class c) Any
d) object is an instance of class d) None
11. How is an object of the class created? 12. What is a constructor in Python?
a) object_name=ClassName a) A function
b) object_name=ClassName() b) A method
Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
c) object_name=new ClassName c) A special method that initializes object
d) None d) A object
13. The constructor method __init__() is called 14. Which of the following is used to define a
automatically when: class in Python?
a) for (i = 0; i < 5; i++) a) object
b) for i in range(5): b) function
c) for i = 0 to 5 c) class
d) foreach i in 5 d) define
15. What is the output: 16. How many instances are created here?
class A: class Car:
def show(self): pass
print("Hi")
obj = A() c1 = Car()
[Link]() c2 = Car()
a) Hi a) 0
b) Error b) 1
c) Nothing c) 2
d) obj d) None
17. The break statement is used to: 18. The continue statement:
a) Exit only the current iteration a) Skips current iteration
b) Exit the entire loop b) Exits loop
c) Skip to next iteration c) Stops program
d) Exit program d) Does nothing
19. What does ‘self’ represent in a class method? 20. Which statement is true about classes?
a) The class a) We can define multiple classes in single file
b) The module b) We can’t define multiple methods in class
c) The instance calling the method c) Classes can’t have variables
d) A global variable d) A class must be named “Class”
21. Class variables are shared by: 22. Object variables are:
a) Only one object a) Shared across all instances
b) All objects of class b) Independent for each object
c) Only parent class c) Defined using static
d) None d) Deleted when class is deleted
23. What will be the output of given code? 24. Which of the following is invalid?
class A: class A:
count = 0 def __init__(self, x, y):
a1 = A() self.x = x
a2 = A() self.y = y
print([Link], [Link]) a) A(1,2)
a) 0 0 b) A()
b) 1 2 c) A(x=1, y=2)
c) 0 1 d) a=A(1,2)
d) 0 0 (shared)
25. What is [Link]=name doing in __init__? 26. The __init__ method is called:
a) Creating a class variable a) Before object is created
b) Creating an object variable b) After class is deleted
c) Assigning a constant c) Immediately after object creation
d) Creating a method d) Only once for all objects
27. Which decorator is used to define class 28. Which of the following is true about static
method? methods?
a) @classmethod a) They require self
b) @staticmethod b) They require cls
c) @class c) They require neither
d) @objectmethod d) Can only be called from outside the class
Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
29. What is the first parameter of a class method? 30. Class methods are bound to:
a) self a) Instance
b) class b) Class
c) cls c) Module
d) object d) Static Context
31. What will be the output of given code? 32. What will be the output of given code?
class Demo: class A:
@staticmethod val = 10
def say(): @classmethod
print("Hello") def show(cls):
print([Link])
[Link]() [Link]()
a) Error a) 10
b) Hello b) Error
c) say() c) None
d) None d) val
33. Which is not a valid use of @staticmethod? 34. How do you call a class method?
a) Defining a helper method a) [Link]()
b) Accessing class variables b) [Link]()
c) Avoiding object access c) [Link]()
d) Reducing coupling d) Both b and c
35. Which statement can be used as a placeholder 36. Which of these stops iteration in an iterator?
in loops? a) StopIteration exception
a) break b) break
b) continue c) continue
c) pass d) end
d) stop
37. Inheritance promotes: 38. A derived class is also known as:
a) Redundancy a) Superclass
b) Code duplication b) Subclass
c) Code reuse c) Root class
d) Overloading d) Parent class
39. Polymorphism allows: 40. Method overloading in Python is:
a) many forms of method names a) supported by default
b) static variables b) not supported directly
c) multiple return types c) only in base class
d) none of these d) must use @overload
41. What will be printed? 42. What is the output?
class A: class A:
def show(self): def greet(self):
print("A") print("Hello")
class B(A):
def show(self): class B(A):
print("B") pass
obj = B() b = B()
[Link]() [Link]()
a) A a) Hello
b) B b) Error
c) Error c) Nothing
d) none of these d) greet
43. What is an example of polymorphism? 44. What happens if init method is missing?
a) Same method name, different behavior a) Syntax Error occurs
Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
b) Same class reused b) Object cannot be created
c) Same object usage c) Python uses default constructor
d) Global functions only d) none of these
45. What will be the output? 46. What will be the output?
class A: class A:
def __init__(self): def show(self):
print("Init A") print("Class A")
obj = A()
a = A() [Link](obj)
a) A a) show
b) INIT A b) Class A
c) Error c) self
d) No output d) Error
47. What will be the output? 48. What will be the output?
class A: class A:
x = 10 count = 0
def __init__(self):
a1 = A() [Link] += 1
a2 = A() a1 = A()
a1.x = 20 a2 = A()
print(A.x) print([Link])
a) 10 a) 1
b) 20 b) 2
c) Error c) 0
d) None d) Error
49. What will be the output? 50. What will the output?
class A: class A:
def display(self): def show(self):
print("A") print("A")
class B(A):
class B(A): def show(self):
def display(self): print("B")
print("B") obj = A()
[Link]()
obj = B() obj = B()
[Link]() [Link]()
a) A a) A A
b) B b) B A
c) AB c) A B
d) Error d) B B
Coding Questions
1. Create class and object: Write a python program to create a class Car with attributes brand and model, and
create an object to print its values.
Expected Output:
Output: Brand: Toyota Model: Corolla (Example)
2. Use of __init__ constructor: Write a Python program to Create a class Person that takes name and age as
input and displays them using a method display().
Note: Use __init__ method to initialize the object.
Expected Output:
Input: Alice 25
Output: Name: Alice Age: 25
Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
3. Object vs class variable: Write a Python program to create a class Student with a class variable school =
'ABC' and instance variables name, grade.
Note: Demonstrate difference between instance and class variable.
Expected Output:
Input: Bob 8
Output: Name: Bob Grade: 8 School: ABC
4. Using self argument: Write a Python program to Create a class Calculator with method add(self, a, b) that
returns the sum of a and b.
Expected Output:
Input: 5 8
Output: 13
5. Square of number using class method: Write a Python program to create a class Math with a class method
square() that returns square of the number.
Expected Output:
Input: 4
Output: 16
6. Polymorphism with common method: Write a Python program to Create two classes Cat and Dog with a
common method sound(). Call the method from both objects.
Expected Output:
Input: Call sound() on Cat and Dog objects
Output: Meow Bark
7. Class with static method: Write a Python program to create a class Utility with a static method is_even()
that returns True if number is even.
Expected Output:
Input: 10
Output: True
8. Override __str__ method: Write a Python program to create a class Book and override __str__ method to
print title and author.
Expected Output:
Input: Create object with title= ‘Python’ and author= ‘Guido’
Output: Python by Guido
9. Class variable usage: Write a Python program to create a class Student that tracks number of students using
class variable count.
Expected Output:
Input: Create two student objects
Output: Total Students: 2
10. Temperature converter class: Write a Python program to create a class Temperature with methods to
convert Celsius to Fahrenheit and vice versa.
Expected Output:
Input: Temp in Celsius: 0
Output: Temp in F: 32
11. Employee class with salary calculation: Write a Python program to Create class Employee with method to
create monthly salary from hourly wage.
Expected Output:
Input: Hourly wage: 100 Hours: 160
Output: 16000
12. Use of super(): Write a Python program to create Parent and Child classes. Use super() to initialize parent
attributes.
Expected Output:
Input: Child(name= ‘Tom’, Age = 10)
Output: Name: Tom Age: 10
13. Inheritance Example: Write a Python program to create a class that inherits from two parent classes and
uses their methods.
Expected Output:
Programming for Artificial Intelligence (24CAI1101)
Practice questions on Object-Oriented Programming
Input: Call methods from both parents
Output: Parent1 method Parent2 method
14. Implement polymorphism with duck typing: Write a Python program to use duck typing to create a
function that works on multiple unrelated classes.
Expected Output:
Input: Pass objects with method fly()
Output: Each object flies
15. Employee class with bonus static method: Write a Python program to create a static method that returns
bonus based on salary.
Expected Output:
Input: 50000
Output: 5000
16. Marks and Average of Student: Write a Python program to create a Student class with attributes for name
and marks in three subjects. Add a method to calculate the average marks.
Expected Output:
Input: Ravi 80 75 90
Output: 81.67
17. Class Inheritance: Shape and Circle: Write a Python program to create a base class Shape with an area
method. Inherit it in Circle class and override area method using πr2
Expected Output:
Input: Radius: 7
Output: Area: 153.94
18. Employee class with Salary Raise method: Write a Python program to create an Employee class with
method apply_raise() that increases salary by 10%.
Expected Output:
Input: 40000
Output: 44000
19. Operator Overloading: Write a Python program to create a class Point to represent a point in 2 D space
with x and y coordinates. Overload the + operator to add two Point objects.
Expected Output:
Input: p1= Point(1,2) p2= Point(4,1) p3=p1+p2
Output: Point(5,3)
20. Employee class with Salary Raise method: Write a Python program to create a class Student with instance
attributes: name, age, and grade. Add a method display_info() to print all student details. Create two student
objects with different data and display their information.
Expected Output:
Input: Student(‘Alice’, 18, ‘A’) Student(‘Bob’, 19, ‘B’)
Output: Name: Alice Age: 18 Grade: A
Name: Bob Age: 19 Grade: B