Classes and Objects in Python: book back answers
21 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Classes and Objects in PythonChoose the best answer (1 Mark)
Which of the following are the key features of an Object Oriented Programming language?
- (a) Constructor and Classes
- (b) Constructor and Object
- (c) Classes and Objects
- (d) Constructor and Destructor
Try it, then show the answerAnswer
(c) Classes and Objects
The chapter identifies classes and objects as key features of Object Oriented Programming.
From the book: “Classes and Objects are the key features of Object Oriented Programming.”
Functions defined inside a class:
- (a) Functions
- (b) Module
- (c) Methods
- (d) section
Try it, then show the answerAnswer
(c) Methods
Functions defined inside a class are called methods.
From the book: “Variables defined inside a class are called as “Class Variable” and functions are called as “Methods”.”
Class members are accessed through which operator?
- (a) &
- (b) .
- (c) #
- (d) %
Try it, then show the answerAnswer
(b) .
The dot operator is used with an object to access a class member.
From the book: “Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.”
Which of the following method is automatically executed when an object is created?
- (a) __object__( )
- (b) __del__( )
- (c) __func__( )
- (d) __init__( )
Try it, then show the answerAnswer
(d) __init__( )
The constructor is executed automatically when an object is created, and the chapter identifies __init__ as the constructor.
From the book: “In Python, there is a special function called “init” which act as a Constructor.”
A private class variable is prefixed with
- (a) __
- (b) &&
- (c) ##
- (d) **
Try it, then show the answerAnswer
(a) __
A variable becomes private when it is prefixed with a double underscore.
From the book: “A variable prefixed with double underscore becomes private in nature.”
Which of the following method is used as destructor?
- (a) __init__( )
- (b) __dest__( )
- (c) __rem__( )
- (d) __del__( )
Try it, then show the answerAnswer
(d) __del__( )
The chapter states that Python uses the __del__ method as a destructor.
From the book: “In Python, _ _del_ _( ) method is used as destructor.”
Which of the following class declaration is correct?
- (a) class class_name
- (b) class class_name<>
- (c) class class_name:
- (d) class class_name[ ]
Try it, then show the answerAnswer
(c) class class_name:
A class definition uses the class keyword, a class name, and a colon.
From the book: “Every class has a unique name followed by a colon ( : ).”
Which of the following is the output of the following program? class Student: def __init__(self, name): self.name=name print (self.name) S=Student(“Tamil”)
- (a) Error
- (b) Tamil
- (c) name
- (d) self
Try it, then show the answerAnswer
(b) Tamil
Creating S calls the constructor with “Tamil”; the constructor prints the value of self.name.
From the book: “This function will act as an ordinary function; but only difference is, it is executed automatically when the object is created.”
Which of the following is the private class variable?
- (a) __num
- (b) ##num
- (c) $$num
- (d) &&num
Try it, then show the answerAnswer
(a) __num
A double-underscore prefix makes a variable private.
From the book: “A variable prefixed with double underscore becomes private in nature.”
The process of creating an object is called as:
- (a) Constructor
- (b) Destructor
- (c) Initialize
- (d) Instantiation
Try it, then show the answerAnswer
(d) Instantiation
The chapter calls the process of creating an object class instantiation.
From the book: “The process of creating object is called as “Class Instantiation”.”
Part -II — Answer the following questions (2 Marks)
What is class?
Try it, then show the answerAnswer
A class is a template for an object and is the main building block in Python.
A class provides a template from which objects, or instances, are created.
From the book: “Class is the main building block in Python. Object is a collection of data and function that act on those data. Class is a template for the object.”
What is instantiation?
Try it, then show the answerAnswer
Instantiation is the process of creating an object or instance of a class.
An object is created from a class through class instantiation.
From the book: “The process of creating object is called as “Class Instantiation”.”
What is the output of the following program? class Sample: __num=10 def disp(self): print(self.__num) S=Sample() S.disp() print(S.__num)
Try it, then show the answerAnswer
10 is printed by S.disp(). Then the final print statement raises an AttributeError because __num is private and cannot be accessed from outside the class.
The method accesses __num from inside the class, so it prints 10. The last statement tries to access the private variable through the object outside the class, which causes an error.
From the book: “Because, private variable cannot be accessed from outside the class.”
How will you create constructor in Python?
Try it, then show the answerAnswer
Define the special method __init__(self, ...) inside the class. It is executed automatically when an object is created and can initialize class variables.
The constructor is written as an __init__ method with self as its first parameter; it may also take arguments.
From the book: “In Python, there is a special function called “init” which act as a Constructor.”
What is the purpose of Destructor?
Try it, then show the answerAnswer
A destructor is used to destroy objects; in Python, __del__() is used as the destructor.
The chapter describes the destructor as the opposite of the constructor and says it is called when an object reference is deleted.
From the book: “Destructor is also a special method to destroy the objects.”
Part -III — Answer the following questions (3 Marks)
What are class members? How do you define it?
Try it, then show the answerAnswer
Class members are the class variables and methods defined inside a class. Define variables and functions within the class body; variables are class variables and functions are methods.
Together, the class variables and methods form the members of a class.
From the book: “Class variable and methods are together known as members of the class.”
Write a class with two private class variables and print the sum using a method.
Try it, then show the answerAnswer
class Sample: __num1 = 10 __num2 = 20 def display(self): print(self.__num1 + self.__num2) S = Sample() S.display()
The two variables are private because they have double-underscore prefixes. The method adds and prints them from inside the class.
From the book: “These variables can be accessed only within the class.”
Find the error in the following program to get the given output? class Fruits: def __init__(self, f1, f2): self.f1=f1 self.f2=f2 def display(self): print("Fruit 1 = %s, Fruit 2 = %s" %(self.f1, self.f2)) F = Fruits ('Apple', 'Mango') del F.display F.display() Fruit 1 = Apple, Fruit 2 = Mango
Try it, then show the answerAnswer
The error is del F.display, which deletes the display method from the object. Remove that statement and call F.display().
Deleting F.display prevents the following call from working. Calling the method without deleting it prints the required fruit names.
From the book: “Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.”
What is the output of the following program? class Greeting: def __init__(self, name): self.__name = name def display(self): print("Good Morning ", self.__name) obj=Greeting('Bindu Madhavan') obj.display()
Try it, then show the answerAnswer
Good Morning Bindu Madhavan
The constructor stores the given name in the instance variable __name. The display method prints that name after “Good Morning ”.
From the book: “Instance variables are declared inside a method using the self keyword.”
How to define constructor and destructor in Python?
Try it, then show the answerAnswer
Define a constructor as def __init__(self, ...): and a destructor as def __del__(self): inside the class.
The constructor runs automatically when an object is created; the destructor is used to destroy objects.
From the book: “In Python, _ _del_ _( ) method is used as destructor.”
Part -IV — Answer the following questions (5 Marks)
Explain about constructor and destructor with suitable example.
Try it, then show the answerAnswer
A constructor and a destructor are special methods in a Python class. The constructor initializes an object when it is created, while the destructor is used to destroy an object.
- Constructor: A constructor is automatically executed when an object of a class is created. Python uses the special method `__init__` as a constructor.
- Purpose of constructor: The constructor is used to initialize class variables. Instance variables can be declared inside a method using the `self` keyword.
- Example of constructor: In the Sample class, `__init__` receives `num`, assigns it to `self.num`, and prints the value. It runs automatically when `S=Sample(10)` creates the object.
- Destructor: A destructor is a special method used to destroy objects. Python uses the `__del__()` method as a destructor, which is opposite to a constructor.
- Execution of destructor: The destructor is called automatically when the object reference is deleted using `del`. The example prints a message when each object exits the scope.
Example: class Sample: def __init__(self, num): print("Constructor of class Sample...") self.num = num print("The value is :", num) S = Sample(10)
Thus, `__init__` initializes an object at creation, and `__del__()` is used when the object is deleted.
From the book: “Constructor is the special function that is automatically executed when an object of a class is created.”