26 Python Access Modifiers Quiz Questions for Practice
Welcome to this challenging Python access modifiers quiz! In this quiz, we have compiled the top 26 multiple-choice questions based on Python access modifiers. These 26 MCQ quizzes will test your knowledge from basic to advanced levels, as well as level up your understanding and knowledge in access modifiers.
Whether you’re a beginner or an experienced Python programmer, this quiz is perfect for interviews preparation, competitive exams, and enhancing your knowledge in access modifiers. So, are you ready to score 26/26? Let’s see, how well do you really understand modifiers concepts in Python? Let’s begin with question number 1.👇
1. What are access modifiers in Python used for?
A. To define data types.
B. To control the visibility and accessibility of class members (attributes/methods).
C. To handle exceptions.
D. None of the above
b
Access modifiers determine the visibility and accessibility of class members.
2. Which access modifier allows a class member to be accessed from anywhere?
A. Private
B. Protected
C. Public
D. Default
c
Public members are accessible from anywhere, both inside and outside the class.
3. Which of the following is a public member in Python?
A. __var
B. _var
C. var
D. __var__
c
Variables without underscores are public and can be accessed from anywhere in the program.
4. What does a single leading underscore (_var) signify in Python?
A. Private member
B. Protected member
C. Public member
D. Magic method
b
Python uses a single leading underscore to denote a protected member by convention.
5. How is a private member represented in Python?
A. Using the private keyword
B. Using the # symbol
C. Using a single underscore _var
D. Using a double underscore __var
d
In Python, prefixing an attribute name with a double underscore ( __ ) denotes a “private” member, and this triggers a mechanism called name mangling.
6. Which access modifier allows access within the class and its subclasses?
A. Public
B. Private
C. Protected
D. Static
c
Protected members (_var) are accessible within the class and its subclasses.
7. Which of the following is the correct syntax to define a private method?
A. def __myMethod(self):
B. def _myMethod(self):
C. def __myMethod__(self):
D. def myMethod(self):
a
A double underscore (without trailing) defines a private method in the class.
8. What is the default access modifier in Python?
A. Private
B. Protected
C. Public
D. Final
c
If you define an attribute or method without any special naming conventions, it is treated as public and can be accessed from anywhere in your program. In Python, the default access modifier for class members (attributes and methods) is public. In simple words, by default, all class members are public unless specified.
9. What will be the expected output of the following Python code?
class Test: def __init__(self): self.__x = 5 t = Test() print(t.__x)
A. 5
B. SyntaxError
C. AttributeError
D. None
b
__var is private and cannot be accessed directly from outside the class. If try, you will get AttributeError.
10. How to access __x from outside the class in the question 9?
A. object.x
B. object.__x
C. object.__dict__
D. object._ClassName__x
d
Private variables are accessed as ObjRef._ClassName__varName.
11. Can a subclass access the private members (__var) of its parent class?
A. Yes, directly
B. No, unless using name mangling
C. Only if the parent class allows it
D. Only if the variable is also protected
b
Private members are not inherited, but can be accessed via ObjRef._ParentClassName__varName.
12. Which of the following keyword is used to enforce private access in Python?
A. private
B. __private__
C. #private
D. Python does not enforce strict private access.
d
Python relies on conventions (underscores) rather than strict access enforcement.
13. Choose the correct output of the following code.
class A: def __init__(self): self._x = 45 a = A() print(a._x)
A. 45
B. AttributeError
C. NameError
D. None
a
The variable x prefixed with a single underscore (_x) is protected, but still accessible.
14. If there is no error in the below code, find out the output.
class Test: def __init__(self): self.x = 5 self.y = 10 self.__z = self.y * self.x + (self.x + self.y / 3) class Demo(Test): t = Test() print(t._Test__z)
A. 58
B. 58.333
C. 50.333
D. AttributeError
b
No AttributeError in the above code. The private variable z prefixed with double underscore is accessible in the subclass using ObjRef._ClassName__VarName.
15. What is __slots__ used for in the context of access modifiers in Python?
A. To dynamically add new attributes to an object at runtime.
B. To restrict the creation of new attributes that are not defined in the __slots__ list.
C. To make all attributes of a class private automatically.
D. To allow multiple inheritance by resolving naming conflicts.
b
In Python, the __slots__ mechanism is used to restrict the creation of new instance attributes that are not explicitly listed in the __slots__ declaration. This not only prevents accidental additions of new attributes but also improves memory efficiency, especially when creating many instances of a class.
16. What is the output of this code?
class Test: __var = 70 obj = Test() obj.__var = 80 print(Test._Test__var)
A. 70
B. 80
C. AttributeError
D. None
a
obj.__var = 80 creates a new instance attribute, not modifying the class private variable.
17. What is the output of the below Python code?
class Person: __name = "Jack" @staticmethod def get_name(): return __name print(Person.get_name())
A. Jack
B. None
C. AttributeError
D. NameError
d
Static methods cannot directly access class variables without the class name.
18. Which access modifier does not follow naming convention in Python?
A. var
B. _var
C. __var
D. __var__
d
__var__ is reserved for special methods, not modifiers.
19. What will be the expected output of the below program code?
class Test: __var = 40 @classmethod def get_var(cls): return cls.__var print(Test.get_var())
A. 40
B. None
C. AttributeError
D. None
a
Class methods can access private class variables using cls argument.
20. What will be the output of the following code?
class Test: __x = 10 def print_x(self): return self.__x t = Test() print(t.print_x())
A. 10
B. AttributeError
C. NameError
D. None
a
__x = 10 is a private class variable, not an instance variable. Inside the print_x() method, self.__x cannot access the class variable directly. Therefore, Python internally performs name mangling to __x, and converts it to self._Test__x. Since there’s no instance variable named _Test__x, Python then looks for a class variable _Test__x. The original class variable __x was mangled to _Test__x during class creation. Thus, it finds _Test__x = 10 and prints the output 10.
21. Which one correctly accesses a class-level private variable?
class A: __val = 99
A. A.__val
B. A.val
C. A.__dict__[‘__val’]
D. A._A__val
d
Class-level private variables are also name-mangled, so the correct access is A._A__val.
22. What does this Python code print?
class A: def __init__(self): self.__data = 100 def get_data(self): return self.__data class B(A): def get_data(self): return self.__data + 1 b = B() print(b.get_data())
A. 101
B. 100
C. NameError
D. AttributeError
d
In class B, __data is not accessible because it is name-mangled in class A to _A__data. So, self.__data in B causes an AttributeError. You can access it using self. _A__data + 1.
23. Which of these variable names is considered a Python magic method name?
A. __x
B. _x
C. __x__
D. x__
c
Double underscores before and after (__x__) indicate a magic method like __init__, __str__, etc.
24. What will this code output?
class Parent: def __init__(self): self.__x = 20 def get(self): return self.__x class Child(Parent): def __init__(self): super().__init__() self.__x = 50 c = Child() print(c.get())
A. 20
B. 50
C. AttributeError
D. None
a
The c.get() method will call from the Parent class and access the private variable self.__x. In Python, __x is name-mangled to _Parent__x. Meanwhile, if the Child class defines its own private instance variable __x, it will be name-mangled to _Child__x, making it a completely separate variable. Therefore, the private instance variable __x in the Child class does not affect the get() method in the Parent class.
25. What will be the output of the following Python code?
class Parent: def __init__(self): self._x = 50 def print_x(self): return self._x class Child(Parent): def __init__(self): super().__init__() self._x += 100 print(Child().print_x())
A. 50
B. 100
C. 150
D. AttributeError
c
_x is a protected variable, and Child class overrides it. So, the print_x() method returns the overridden value 100 + 50 = 150.
26. Will the following Python code successfully execute? If yes, what will be the output?
class Test: __slots__ = ['x'] t = Test() t.x = 10 t.y = 20 print(t.x)
A. Works fine
B. AttributeError on t.x
C. AttributeError on t.y
D. 10
c
Only x is allowed as an attribute because of __slots__. Setting t.y raises an AttributeError.
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score