0% found this document useful (0 votes)
460 views17 pages

INT 213 Quiz Questions

Here is the program with base class Shape and derived classes Rectangle and Circle with methods to get dimensions and calculate circumference: class Shape: def __init__(self): pass class Rectangle(Shape): def __init__(self, length, breadth): self.length = length self.breadth = breadth def getArea(self): return self.length * self.breadth def getPerimeter(self): return 2 * (self.length + self.breadth) class Circle(Shape): def __init__(self, radius): self.radius = radius def getArea(self): return 3.14 * self.radius ** 2 def get

Uploaded by

boombam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
460 views17 pages

INT 213 Quiz Questions

Here is the program with base class Shape and derived classes Rectangle and Circle with methods to get dimensions and calculate circumference: class Shape: def __init__(self): pass class Rectangle(Shape): def __init__(self, length, breadth): self.length = length self.breadth = breadth def getArea(self): return self.length * self.breadth def getPerimeter(self): return 2 * (self.length + self.breadth) class Circle(Shape): def __init__(self, radius): self.radius = radius def getArea(self): return 3.14 * self.radius ** 2 def get

Uploaded by

boombam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

QUIZ

What will be the output of the following


Python code?
class test:
def __init__(self,a="Hello World"):
self.a=a  
def display(self):
print(self.a)
obj=test()
obj.display()
a) The program has an error because constructor can’t have
default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have
parameters
What is setattr() used for?

a) To access the attribute of the object


b) To set an attribute
c) To check if an attribute exists or not
d) To delete an attribute
What will be the output of the following Python code?

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) Runs normally, doesn’t display anything


b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the
object
d) Error as display function requires additional argument
c
Explanation: Since, the __init__
special method has another argument
a other than self, during object
creation, one argument is required.
For example: obj=test(“Hello”)
How many objects and reference variables
are there for the given Python code?
class A:
print("Inside class")
A()
A()
obj=A()

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. "std" is the reference variable for object


Student(1,20)
B. id and age are called the parameters.
C. Every class must have a constructor.
D. None of the above
What will be the output of below Python
code?
class Student:
def __init__(self,name,id):
self.name=name
self.id=id
print(self.id)
std=Student("Simon",1)
std.id=2
print(std.id)

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. id(a1) and id(a2) will have same value.


B. id(a1) and id(a2) will have different values.
C. Two objects with same value of attribute cannot be created.
D. None of the above
B
Explanation: Although both a1 and a2 have same value of attributes,but
these two point to two different object. Hence, their id will be different.
Which of the following is correct?
class Book:
def __init__(self,author): self.author=author
book1=Book("V.M.Shah")
book2=book1

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.

You might also like