Q. 1

What is the output of the following code ?

class Foo:
    X = 10
    def hello(self):
        self.X = 20

print(Foo.X)

Options:

Explanation

Correct answer is : B

"X" is a class attribute can be called using class name

Discuss It

Q. 2

What is the output of the following code ?

class Foo:
    X = 10
    def hello(self):
        self.X = 20

f = Foo()
print(Foo.X, f.X)

Options:

Explanation

Correct answer is : A

"X" is a class attribute.
Class attribute will be shared by all instances
"f" is an instance here. So has access to "X"

Discuss It

Q. 3

What is the output of the following code ?

class Foo:
    X = 10
    def hello(self):
        self.X = 20

f = Foo()
f.hello()
print(Foo.X, f.X)

Options:

Explanation

Correct answer is : D

Assigning to an object’s attribute always changes that object.
In method hello() , we are setting value 20 to self.X i.e. self.X = 20.
But the class attribute "X" value will be remain same as 10

Discuss It

Q. 4

What is the output of the following code ?

class Foo:
    X = 10
    def hello(self):
        self.X = 20

f = Foo()
f.hello()
p = Foo()
print(Foo.X, f.X, p.X)

Options:

Explanation

Correct answer is : B

Assigning to an object’s attribute always changes that object only, not others.
Here the method hello() is called using "f" object/instance only. So the value of "X" for instance "f" will only be changed (in this statement self.X = 20)
The "X" value for the class and other instances "p" will remain unchanged

Discuss It

Q. 5

What is the output of the following code ?

class Foo:
    X = 10

f = Foo()
Foo.X = 30
p = Foo()
print(Foo.X, f.X, p.X)

Options:

Explanation

Correct answer is : D

"X" is a class attribute and shared by all the instances.
Here Foo.X = 30 , we are changing this class attribute using class name.
So the same value will changed for all the instances

Discuss It

Q. 6

What is the output of the following code ?

class MyClass:
    def __init__(self):
        self.__a = 1


obj = MyClass()
print(obj.__a)

 

Options:

Explanation

Correct answer is : D

__a is a pseudoprivate attribute which can not be accessed outside of the class directly.

Although it can be accessed using class name. print(obj._MyClass__a)  will print 1

 

Discuss It

Q. 7

Which of the following is a attribute of the instance obj and also private.which can not be retrieved by obj.attributname ?

class MyClass:
    def __init__(self):
        __a = 1
        self.__b = 1
        self.__c__ = 1
        __d__ = 1

obj = MyClass()

 

Options:

Explanation

Correct answer is : B

The variables begins with double underscore are private and  can not be retrieved by obj.attributname

so __b is psedoprivate for the instance.

__a__ and __d__ are local to __init__ () method

__c__ can be accessed using obj.attributname

 

Discuss It

Q. 8

What is the output of the following code ?

class MyClass:
    def __init__(self):
        self.__b = 1

    def display(self):
        print(self.__b)

obj = MyClass()
obj.display()

 

Options:

Explanation

Correct answer is : A

The methods inside class can access the pseudo private members/attributes .i.e. __b in this case

So it will print 1

Discuss It

Q. 9

What is the output of the following code ?

class MyClass:
    def __init__(self):
        self.__a = 1


obj = MyClass()
print(obj._MyClass__a)

 

Options:

Explanation

Correct answer is : A

The attribute names starts with 2 underscores are pseudo private attributes and they eventually converted to _ClassName__attrname.

So here __a will be converted to _MyClass__a and can be accessed using obj

Discuss It

Q. 10

What is the output of the following code ? 

class MyClass:
    def __init__(self):
        self._a = 1


obj = MyClass()
print(obj._MyClass_a)

 

Options:

Explanation

Correct answer is : B

The attributes with single underscores are considered as general attribute so can be directly accessed by obj.attributename.

So obj._a will give 1 . But _MyClass_a is undefined name here

Traceback (most recent call last):
  File "C:/pythoneasy/test1.py", line 7, in <module>
    print(obj._MyClass_a)
AttributeError: 'MyClass' object has no attribute '_MyClass_a'

 

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