UNIT-5 Classes & Objects
UNIT-5 Classes & Objects
defgetq(xyz):
returnself._q
a=A()
a.p=20
print(a.p)
a. The program has an error because p is private
b. The program has an error because q is private
c. The program prints 1
d. The program prints 20
5. Analyse the following code:
class A:
def__init__(self,p):
self.p=p
def print(self):
print(p)
a=A(“Python”)
a.print()
a. The program has an error because class A does not have a constructor.
b. The program has an error because class A should have a print(self,p) method.
c. The program has an error because class A should have a print(p) method.
d. The program will execute fine if print(p) is replaced by print(self.p).
6. Analyse the following code:
class A:
def__init__(self,p=“Python”):
self.p=p
def print(self):
print(self.p)
a=A()
a.print()
324 Problem Solving and Python Programming
a. The program has an error because class A does not have a constructor.
b. The program has an error because class A should have a print(self,p) method.
c. The program executes fine and prints nothing.
d. The program executes fine and prints Python.
7. Which of the following is used to create an object?
a. Constructor b. Class
c. Method d. Data field
8. Which of the following statements is not correct?
a. Each object must have unique id.
b. Same kind objects must have same type.
c. Same type objects must have same id.
d. A variable that holds a value is the reference to an object of that value.
9. What is the output of the following code?
Print(type((‘US’,‘India’,’Africa’)))
a. <class,’set’> b. <class,’list’>
c. <class,’dict’> d. <class,’tuple’>
10. What is the output of the following code?
Print(type(1J))
a. <class,’int’> b. <class,’list’>
c. <class,’float’> d. <class,’command’>
11. defMyFunction():
“Python is an interesting language”
return 1
print(MyFunction.__doc__[10:12])
What will be the output?
a. Is b. an
c. te d. er
12. What is the output of the following code?
Class Employee:
def__init__(self):
pass
defgetEmpId(self):
print(__name__)
s=Employee()
s.getEmpId()
a. __name__ b. __main__
c. Employee d. Error
13. What is the output of the following code?
Print(type(1/5))
a. <class,’int’> b. <class,’list’>
c. <class,’float’> d. <class,’command’>
14. Which of the following is known as an instance of class?
a. Object b. Program
c. Data d. Method
Classes and Objects 325
15. Which of the following is a blueprint that defines objects of the same type?
a. A class b. An object
c. A method d. A program
16. Which of the following is most accurate for the given declaration:
x=Square()
a. x contains an int value. b. x contains an object of square type.
c. An int value can be assigned to x. d. x contains a reference to a square object.
17. Which of the following is the description of a set of objects that share the same attributes, operations,
and semantics?
a. Class b. Constructor
c. Function d. Method
18. Which of the following is responsible for initialising the objects of its class?
a. Constructor b. Destructor
c. Iterator d. None of the above
Short Questions
1. What is the OOP concept? Define classes and objects in Python.
2. Explain the OOP principle inheritance in Python.
3. What is docstring in Python?
4. Differentiate between method overloading and method overriding.
5. What is the use of pass in Python?
6. What is __init__.py? Give an example.
7. How can we count the number of instances in a program?
8. How can we copy an object in Python?
9. What will be the output of the given code? Explain your answer.
classParent (object):
a=1
class Child1(Parent):
pass
class Child2(Parent):
pass
print Parent.a,Child1.a,Child2.a
Child2.a=5
print Parent.a,Child1.a,Child2.a
Parent.a=4
print Parent.a,Child1.a,Child2.a
10. Consider the code of dictionary:
classSubDict(dict):
def __missing__(self, key):
return[]
326 Problem Solving and Python Programming