Access Specifiers Practice
Understand public, protected and private access levels in Python.
1
Public Attribute Access
Easy
Write a class Student with public attribute name. Create an object, set the name, and print it.
Input Format
A single line containing a name.
Output Format
Print the name.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Alice |
|
Sample Input 2
Bob |
Sample Output 2
Bob |
|
Sample Input 3
Sam |
Sample Output 3
Sam |
2
Public Method Call
Easy
Write a class Calculator with public method add(a, b). Call it and print the result.
Input Format
A single line containing two space-separated integers.
Output Format
Print the sum.
Sample Test Cases
|
Sample Input 1
4 5 |
Sample Output 1
9 |
|
Sample Input 2
10 5 |
Sample Output 2
15 |
|
Sample Input 3
-1 -2 |
Sample Output 3
-3 |
3
Private Attribute (__name)
Easy
Write a class Person with private attribute __age. Set and print it inside a method display().
Input Format
A single line containing the age.
Output Format
Print the age.
Sample Test Cases
|
Sample Input 1
30 |
Sample Output 1
30 |
|
Sample Input 2
40 |
Sample Output 2
40 |
|
Sample Input 3
5 |
Sample Output 3
5 |
4
Private Method (__method)
Easy
Write a class Demo with a private method __secret() that returns 'secret'. Call it from a public method show().
Input Format
No input.
Output Format
Print 'secret'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
secret |
|
Sample Input 2
|
Sample Output 2
secret |
|
Sample Input 3
|
Sample Output 3
secret |
5
Protected Attribute (Single _)
Easy
Write a class Employee with protected attribute _id. Set it in the class and print it.
Input Format
A single line containing an ID.
Output Format
Print the ID.
Sample Test Cases
|
Sample Input 1
101 |
Sample Output 1
101 |
|
Sample Input 2
202 |
Sample Output 2
202 |
|
Sample Input 3
1 |
Sample Output 3
1 |
6
Access Private via Name Mangling
Easy
Write a class Demo with private __value = 42. Access it from outside using _Demo__value and print it.
Input Format
No input.
Output Format
Print 42.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
42 |
|
Sample Input 2
|
Sample Output 2
42 |
|
Sample Input 3
|
Sample Output 3
42 |
7
Public vs Private Display
Easy
Write a class Account with public name and private __balance. Use a public method to print both.
Input Format
First line: name. Second line: balance.
Output Format
Print 'Name: [name]' and 'Balance: [balance]'.
Sample Test Cases
|
Sample Input 1
Alice 1000 |
Sample Output 1
Name: Alice Balance: 1000 |
|
Sample Input 2
Bob 2000 |
Sample Output 2
Name: Bob Balance: 2000 |
|
Sample Input 3
Sam 0 |
Sample Output 3
Name: Sam Balance: 0 |
8
Public Getter for Private
Easy
Write a class Book with private __title and a public method get_title() that returns it.
Input Format
A single line containing a title.
Output Format
Print the title.
Sample Test Cases
|
Sample Input 1
Python |
Sample Output 1
Python |
|
Sample Input 2
Java |
Sample Output 2
Java |
|
Sample Input 3
C++ |
Sample Output 3
C++ |
9
Call Public Method on Object
Easy
Write a class Greeter with public method hello() that prints 'Hi!'. Create an object and call it.
Input Format
No input.
Output Format
Print 'Hi!'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Hi! |
|
Sample Input 2
|
Sample Output 2
Hi! |
|
Sample Input 3
|
Sample Output 3
Hi! |
10
Set Private Through Method
Easy
Write a class Student with private __marks and a public method set_marks(m). Set and then get it.
Input Format
A single line containing the marks.
Output Format
Print the marks.
Sample Test Cases
|
Sample Input 1
92 |
Sample Output 1
92 |
|
Sample Input 2
50 |
Sample Output 2
50 |
|
Sample Input 3
100 |
Sample Output 3
100 |
1
Private Attributes with Getters/Setters
Medium
Write a class Employee with private __name and __salary, and public getters and setters for both.
Input Format
First line: name. Second line: salary.
Output Format
Print name and salary on separate lines.
Sample Test Cases
|
Sample Input 1
Ravi 45000 |
Sample Output 1
Ravi 45000 |
|
Sample Input 2
Bob 20000 |
Sample Output 2
Bob 20000 |
|
Sample Input 3
Sam 0 |
Sample Output 3
Sam 0 |
2
Protected Access in Child Class
Medium
Write a base class Vehicle with protected _speed, and a child class Car that prints it via a method.
Input Format
A single line containing the speed.
Output Format
Print the speed.
Sample Test Cases
|
Sample Input 1
60 |
Sample Output 1
60 |
|
Sample Input 2
100 |
Sample Output 2
100 |
|
Sample Input 3
1 |
Sample Output 3
1 |
3
Name Mangling in Derived Class
Medium
Write a class A with private __x = 10, and a class B(A) that tries to access it via _A__x and prints it.
Input Format
No input.
Output Format
Print 10.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
10 |
|
Sample Input 2
|
Sample Output 2
10 |
|
Sample Input 3
|
Sample Output 3
10 |
4
Public Method Calling Private Method
Medium
Write a class Calculator with private __helper(n) returning n*2, and public double(n) that calls it.
Input Format
A single line containing integer n.
Output Format
Print the doubled value.
Sample Test Cases
|
Sample Input 1
21 |
Sample Output 1
42 |
|
Sample Input 2
50 |
Sample Output 2
100 |
|
Sample Input 3
0 |
Sample Output 3
0 |
5
Access Levels in One Class
Medium
Write a class Bank with public bank_name, protected _branch, and private __balance. Use a method to print all three.
Input Format
First line: bank name. Second line: branch. Third line: balance.
Output Format
Print all three, one per line.
Sample Test Cases
|
Sample Input 1
SBI Main 5000 |
Sample Output 1
SBI Main 5000 |
|
Sample Input 2
ICICI East 7000 |
Sample Output 2
ICICI East 7000 |
|
Sample Input 3
HDFC West 100 |
Sample Output 3
HDFC West 100 |
6
Attempt Private Access from Outside
Medium
Write a class Demo with private __secret = 'hidden'. Try printing Demo().__secret directly; handle the AttributeError by printing 'Cannot access private'.
Input Format
No input.
Output Format
Print 'Cannot access private'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Cannot access private |
|
Sample Input 2
|
Sample Output 2
Cannot access private |
|
Sample Input 3
|
Sample Output 3
Cannot access private |
7
Private with Property
Medium
Write a class Person with private __age and a @property age getter to read it.
Input Format
A single line containing the age.
Output Format
Print the age.
Sample Test Cases
|
Sample Input 1
27 |
Sample Output 1
27 |
|
Sample Input 2
30 |
Sample Output 2
30 |
|
Sample Input 3
1 |
Sample Output 3
1 |
8
Three-Level Access Demo
Medium
Write a class Example with public pub = 1, protected _prot = 2, and private __priv = 3. Print the public and protected values.
Input Format
No input.
Output Format
Print 1 and 2 on separate lines.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
1 2 |
|
Sample Input 2
|
Sample Output 2
1 2 |
|
Sample Input 3
|
Sample Output 3
1 2 |
9
Getter Validates Private Data
Medium
Write a class Student with private __marks and a getter that returns 'Invalid' if marks < 0.
Input Format
A single line containing the marks.
Output Format
Print the marks, or 'Invalid'.
Sample Test Cases
|
Sample Input 1
-10 |
Sample Output 1
Invalid |
|
Sample Input 2
-5 |
Sample Output 2
Invalid |
|
Sample Input 3
100 |
Sample Output 3
100 |
10
Protected Method in Inheritance
Medium
Write a base class Animal with protected method _sound() returning 'generic sound', and a child class Dog that calls it and prints it.
Input Format
No input.
Output Format
Print 'generic sound'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
generic sound |
|
Sample Input 2
|
Sample Output 2
generic sound |
|
Sample Input 3
|
Sample Output 3
generic sound |
1
Full Access Control — Bank
Hard
Write a class BankAccount with public account_name, protected _bank, and private __balance with deposit() and get_balance().
Input Format
First line: account name. Second line: bank. Third line: initial balance. Fourth line: deposit.
Output Format
Print all fields and the final balance.
Sample Test Cases
|
Sample Input 1
Alice SBI 5000 2000 |
Sample Output 1
Alice SBI 7000 |
|
Sample Input 2
Bob HDFC 1000 500 |
Sample Output 2
Bob HDFC 1500 |
|
Sample Input 3
Sam SBI 0 0 |
Sample Output 3
Sam SBI 0 |
2
Private Method in Inheritance Chain
Hard
Write a class Parent with private method __secret() returning 'parent secret', and a child class Child that calls it using _Parent__secret.
Input Format
No input.
Output Format
Print 'parent secret'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
parent secret |
|
Sample Input 2
|
Sample Output 2
parent secret |
|
Sample Input 3
|
Sample Output 3
parent secret |
3
Setter Guards (Access Specifiers)
Hard
Write a class Employee with private __salary and a setter that allows salary change only if the new value is within 20% of the old, else prints 'Not allowed'.
Input Format
First line: initial salary. Second line: new salary.
Output Format
Print the new salary, or 'Not allowed'.
Sample Test Cases
|
Sample Input 1
50000 60000 |
Sample Output 1
Not allowed |
|
Sample Input 2
10000 11000 |
Sample Output 2
11000 |
|
Sample Input 3
10000 12000 |
Sample Output 3
Not allowed |
4
Protected Attribute Across Modules
Hard
Write a class Database with protected _connection = 'connected' and a method status() that prints it.
Input Format
No input.
Output Format
Print 'connected'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
connected |
|
Sample Input 2
|
Sample Output 2
connected |
|
Sample Input 3
|
Sample Output 3
connected |
5
Bank with Private Ledger
Hard
Write a class Account with private __balance and private __history list, recording every deposit/withdrawal.
Input Format
First line: initial balance. Second line: deposit. Third line: withdrawal.
Output Format
Print the balance and the history list.
Sample Test Cases
|
Sample Input 1
1000 500 200 |
Sample Output 1
1300 ['dep 500', 'wd 200'] |
|
Sample Input 2
100 50 20 |
Sample Output 2
130 ['dep 50', 'wd 20'] |
|
Sample Input 3
0 0 0 |
Sample Output 3
0 [] |
6
Encapsulated Student Database
Hard
Write a class StudentDB with private __students dict and methods add(name, marks) and get(name).
Input Format
First line: N. Next N lines: name and marks. Last line: query name.
Output Format
Print the marks of the query name.
Sample Test Cases
|
Sample Input 1
2 Alice 90 Bob 80 Alice |
Sample Output 1
90 |
|
Sample Input 2
1 A 80 A |
Sample Output 2
80 |
|
Sample Input 3
2 A 10 B 20 C |
Sample Output 3
Not found |
7
Protected Member with Override
Hard
Write a class Base with protected _value, and a class Derived that overrides a method compute() to return _value * 2.
Input Format
A single line containing the value.
Output Format
Print the doubled value.
Sample Test Cases
|
Sample Input 1
15 |
Sample Output 1
30 |
|
Sample Input 2
100 |
Sample Output 2
200 |
|
Sample Input 3
0 |
Sample Output 3
0 |
8
Access Specifier Audit
Hard
Write a class User with public username, protected _email, and private __password_hash. Provide a method display() that prints username and email.
Input Format
First line: username. Second line: email. Third line: password hash.
Output Format
Print the username and email only.
Sample Test Cases
|
Sample Input 1
alice alice@mail.com hashed123 |
Sample Output 1
alice alice@mail.com |
|
Sample Input 2
bob b@m.com hash |
Sample Output 2
bob b@m.com |
|
Sample Input 3
sam s@m.com h |
Sample Output 3
sam s@m.com |
9
Multi-Level Name Mangling
Hard
Write a class A with __v = 1, class B(A) with __v = 2, and class C(B) that reads both using mangling and prints their sum.
Input Format
No input.
Output Format
Print 3.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
3 |
|
Sample Input 2
|
Sample Output 2
3 |
|
Sample Input 3
|
Sample Output 3
3 |
10
Security System with Access Levels
Hard
Write a class Vault with private __code and public method check(code) returning True only for the correct code '1234'.
Input Format
A single line containing a code attempt.
Output Format
Print True or False.
Sample Test Cases
|
Sample Input 1
9999 |
Sample Output 1
False |
|
Sample Input 2
1234 |
Sample Output 2
True |
|
Sample Input 3
0000 |
Sample Output 3
False |