INT 213 Quiz Questions
INT 213 Quiz Questions
class change:
def __init__(self, x, y, z):
self.a = x + y + z
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)
a) 6
b) 7
c) Error
d) 0
b
Explanation: First, a=1+2+3=6. Then, after setattr() is invoked,
x.a=6+1=7.
What will be the output of the following Python code?
class test:
def __init__(self,a):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
A. 2 and 1
B. 3 and 3
C. 3 and 1
D. 3 and 2
C
Explanation: obj is the reference variable here
and an object will be created each time A() is
called.So there will be 3 objects created.
Which of the following is False with respect
Python code?
class Student:
def __init__(self,id,age):
self.id=id
self.age=age
std=Student(1,20)
A. 1
1
B. 1
2
C. 2
1
D. 2
2
B
Explanation: When object with id =1 is created for Student, constructor is invoked and it
prints 1. Later, id has been changed to 2, so next print statement prints 2.
Which of the following is correct?
class A:
def __init__(self,name):
self.name=name
a1=A("john")
a2=A("john")
A. Both book1 and book2 will have reference to two different objects of class Book.
B. id(book1) and id(book2) will have same value.
C. It will throw error as multiple references to same object is not possible.
D. None of the above
B
Explanation: book1 and book2 will reference to the same object. Hence, id(book1) and
id(book2) will have same value.
• Write a program that has a base class Shape.
Derive two classes Rectangle and Circle from
class Shape and write methods to get the
details of their dimensions and calculate their
circumference.