Q. 1

What is the output of the following code ?

class Foo:
    data = 'welcome'

    def __init__(self, value):
        self.data = value

    def printme(self):
        print(self.data, Foo.data)


p = Foo('python')
e = Foo('easy')
p.printme()
e.printme()

Options:

Explanation

Correct answer is : B

In def __init__ we are attaching the name data to the instance here self.data = value.
When we call p = Foo('python') the data attribute value for "p" will become "python" (self.data) and the "Foo.data" value will always remains same are we are calling it through class name "Foo"
Same will be for "e"
So the result will be

python welcome
easy welcome

Discuss It

Q. 2

What is the output of the following code ?

class Foo:
    data = 'python'

    def set_data(self, value):
        self.data = value

    def printme(self):
        print(self.data, Foo.data)

p = Foo()
p.printme()
p.set_data('easy')
p.printme()

Options:

Explanation

Correct answer is : A

The class attribute "data" will be shared by the instances.
When we set "data" to "self" in "def set_data" it attaches the "data" attribute to the particular instance only
Before calling p.set_data('easy') , the value self.data refers to class attribute "data" and the value will be "python"
So p.printme() prints "python python"
But once reassign self.data to a value "easy", the value of "self.data" will become "easy" for the particular instance "p", but Foo.data value remain unchanged
So the second printme() prints "easy python"

Discuss It

Q. 3

What is the output of the following code ?

class Foo:
    def printme(self, data):
        print(data)

p = Foo()
p.printme('hello')
Foo.printme(p, 'hello')

Options:

Explanation

Correct answer is : D

instance.method(args...)is same as class.method(instance, args...)
p.printme('hello') is same as Foo.printme(p, 'hello')
So the output will be

hello
hello

Discuss It

Q. 4

What is the output of the following code ? 

class TestAttr:
    def __init__(self):
        self.X = 'Pre'
        self.value_change(self.X)
    
    def value_change(self, var):
        var = 'Post'


obj = TestAttr()
print(obj.X)

 

Options:

Explanation

Correct answer is : A

Even though value_change() is called from __intit_() , there is no change in self.X value.

So selef.X will remain unchanged

Discuss It

Q. 5

What is the output of the following code ?

class MyClass:
    def __init__(self, cost):
        self.cost = cost


obj = MyClass(70)
obj.count = 20
obj.containers = 5

print(obj.count + len(obj.__dict__))

 

Options:

Explanation

Correct answer is : C

obj.__dict__ is namespace dictionary of the class which contains {'cost': 70, 'count': 20, 'containers': 5}.

so len(obj.__dict__) will be 3 

obj.count + 3 will be 23

Discuss It

Q. 6

What is the output of the following code ?

class MyClass:
    def __init__(self, cost):
        self.cost = cost

    def print_name(self):
        print(__name__, MyClass.__name__)


obj = MyClass(70)
obj.print_name()

 

Options:

Explanation

Correct answer is : B

__name__ will print __main__ and MyClass.__name__ will print class name i.e "MyClass"

Discuss It

Q. 7

What does the following code print in the interpreter ?

>>> class Sample():
...     def __repr__(self):
...         return '__repr__ is called'
...     def __str__(self):
...         return '__str__ is called'
...
>>> p = Sample()
>>> p

 

Options:

Explanation

Correct answer is : B

The __repr__ is used to provide a string representation of the object. So "p" on the interpreter will invoke __repr__()

Discuss It

Q. 8

What does the following code print in the interpreter ?

>>> class Sample():
...     def __repr__(self):
...         return '__repr__ is called'
...     def __str__(self):
...         return '__str__ is called'
...
>>> p = Sample()
>>> print(p)

 

Options:

Explanation

Correct answer is : None

When an object is used in print() , it invokes __str__() method

Discuss It

Q. 9

Which special method overloads + operator ?

Options:

Explanation

Correct answer is : B

+ operator invokes __add__() special method 

Discuss It

Q. 10

Which operator is overloaded by __invert__() special method?

Options:

Explanation

Correct answer is : B

~ operator is overloaded by __invert__() special method

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