Q. 1

What is setattr() used for?

Options:

Explanation

Correct answer is : D

setattr() used to set an attribute

Discuss It

Q. 2

What is getattr() used for?

Options:

Explanation

Correct answer is : C

getattr() used to access the attribute of the object

Discuss It

Q. 3

What is the output of the following code ?

class AttrTest:
    def __init__(self, a, b, c):
        self.X = a + b + c


p = AttrTest(7, 8, 9)
b = getattr(p, 'X')
setattr(p, 'X', b + 1)
print(p.X)

 

Options:

Explanation

Correct answer is : B

a + b + c will be 7 + 8 + 9 = 24 

b = getattr(p, 'X') will be 24 
setattr(p, 'X', b + 1) will set X to 24 + 1

Discuss It

Q. 4

What is hasattr() used for?

Options:

Explanation

Correct answer is : A

hasattr() used to check if an attribute exists or not on a particular instance

Discuss It

Q. 5

What is the output of the following code ?

class AttrTest:
    def __init__(self, a, b):
        self.a = a
        self.b = b


obj = AttrTest(34, 'S')
obj.X = 7
print(hasattr(obj, 'X'))

 

Options:

Explanation

Correct answer is : A

obj.X = 7 creates a new attribute. so hasattr() will return True

Discuss It

Q. 6

What is the output of the following code ? 

class A:
    def __init__(self):
        self.X = 10


a = A()
a.X = 20
delattr(a, 'X')
print(a.X)

 

Options:

Explanation

Correct answer is : D

delattr(a, 'X') - deletes attribute X reference from object "a" .

So it will raise the error

Discuss It

Q. 7

What is the output of the following code ? 

class A:
    x = 10
    def __getattr__(self, item):
        print("__getattr__ is called")

a = A()
a.Y

 

Options:

Explanation

Correct answer is : B

__getattr__() method is invoked for each undefined attribute

As we are trying to fetch attribute "Y which is not available in instance scope. it invokes __getattr__() method

Discuss It

Q. 8

What is the output of the following code ? 

class A:
    X = 10
    def __getattribute__(self, item):
        print("__getattribute__ is called")

a = A()
print(a.X, a.Y)

 

Options:

Explanation

Correct answer is : None

Whenever we are trying to fetch an attribute __getattribute__() is called everytime and the value of the attribute will be the return value of __getattribute__() method

Here we are fetching 2 attributes a.X, a.Y, so __getattribute__() will eb called 2 times and return None each time

So value of a.X and a.Y will be None 

Discuss It

Q. 9

What is the output of the following code ? 

class A:
    X = 10

    def __setattr__(self, key, value):
        print("__setattr__ is called")


a = A()
a.Y = 10
A.k = 90
print(a.X, a.k)

 

Options:

Explanation

Correct answer is : D

Whenever we set a avalue to an attribute of an instance __setattr__ () is called .

But it will never be invoked when we set class attribute for A.k = 90

a.k can fetch class atrribute vale k and prints 90 

Discuss It

Q. 10

What is the output of the following code ? 

class A:
    X = 10
    def __getattribute__(self, item):
        print("__getattribute__ is called")
    def __getattr__(self, item):
        print("__getattr__ is called")
a = A()
a.Y

 

Options:

Explanation

Correct answer is : A

All the attribute fetch invokes __getattribute__() method.

But when __getattribute__() and __getattr__() both available, only __getattribute__() will be invoked 

Discuss It
Submit Your Answers

Interview Questions

on
Classes

Tutorial

on
Classes

Programs

on
Classes
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description