Site icon i2tutorials

Python-Encapsulation-Quiz

This Quiz contains totally 10 Questions each carry 1 point for you.

1. Which of the below is the utmost appropriate definition for encapsulation?
Ability of a class to derive members of another class as a part of its own
Means of bundling instance variables and methods in order to restrict access to certain class members
All of the above
None of the above

Correct!

Wrong!

2. What will be the output of the following Python code?
class Demo:
    def __init__(self):
       self.a = 1
       self.__b = 1
    def display(self):
        return self.__b
obj = Demo()
print(obj.a)
Error as there is no function to return self.a
Error as b is private and display(self) is returning a private member
The program runs well and 1 is the result
Error because you can’t name a class member using __b

Correct!

Wrong!

3. Methods of a class that offer access to private members of the class are termed as ______ and ______
getters/setters
__repr__/__str__
user-defined functions/in-built functions
__init__/__del__

Correct!

Wrong!

4. Which of these is a private data field?
def Demo:
def __init__(self):
   __a = 1
   self.__b = 1
    self.__c__ = 1
   __d__= 1
__a
__c__
__d__
__b

Correct!

Wrong!

5. The purpose of name mangling is to avoid unintentional access of private class members.
True
False

Correct!

Wrong!

6.  Which of the following is false about protected class members?
They begin with one underscore
They can be accessed by subclasses
They can be accessed by using name mangling method
They can be accessed within a class

Correct!

Wrong!

7. What will be the output of the following Python code?
class objects:
    def __init__(self):
       self.colour = None
       self._shape = "Circle"
     def display(self, s):
       self._shape = s
obj=objects()
print(obj._objects_shape)
The program executes well as name mangling has been correctly implemented
Error as the member shape is a protected member
Error as the appropriate syntax for name mangling is not implemented
Error as the member shape is a private member

Correct!

Wrong!

8. Which of these is not a fundamental feature of OOP?
Encapsulation
Inheritance
Instantiation
Polymorphism

Correct!

Wrong!

9. Access specifiers are keywords that determine the accessibility of methods, classes.
True
False

Correct!

Wrong!

10. What will be the output of the following Python code?
class Demo:
    def __init__(self):
       self.a = 1
       self.__b = 1
     def display(self):
        return self.__b
obj = Demo()
print(obj.__b)
Error as there isn’t any function to return self.a
Error as b is private and display(self) is returning a private member
Error as b is private and hence can’t be printed
The program runs well and 1 is printed

Correct!

Wrong!

Share the quiz to show your results !

Subscribe to see your results

Ignore & go to results

Python-Encapsulation-Quiz

You got %%score%% of %%total%% right

%%description%%

%%description%%

Where to go ?

Quizzes

Loading...

Exit mobile version