Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

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.
A single line containing a name.
Print the name.
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.
A single line containing two space-separated integers.
Print the sum.
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().
A single line containing the age.
Print the age.
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().
No input.
Print 'secret'.
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.
A single line containing an ID.
Print the ID.
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.
No input.
Print 42.
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.
First line: name. Second line: balance.
Print 'Name: [name]' and 'Balance: [balance]'.
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.
A single line containing a title.
Print the title.
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.
No input.
Print 'Hi!'.
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.
A single line containing the marks.
Print the marks.
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.
First line: name. Second line: salary.
Print name and salary on separate lines.
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.
A single line containing the speed.
Print the speed.
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.
No input.
Print 10.
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.
A single line containing integer n.
Print the doubled value.
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.
First line: bank name. Second line: branch. Third line: balance.
Print all three, one per line.
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'.
No input.
Print 'Cannot access private'.
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.
A single line containing the age.
Print the age.
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.
No input.
Print 1 and 2 on separate lines.
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.
A single line containing the marks.
Print the marks, or 'Invalid'.
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.
No input.
Print 'generic sound'.
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().
First line: account name. Second line: bank. Third line: initial balance. Fourth line: deposit.
Print all fields and the final balance.
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.
No input.
Print 'parent secret'.
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'.
First line: initial salary. Second line: new salary.
Print the new salary, or 'Not allowed'.
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.
No input.
Print 'connected'.
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.
First line: initial balance. Second line: deposit. Third line: withdrawal.
Print the balance and the history list.
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).
First line: N. Next N lines: name and marks. Last line: query name.
Print the marks of the query name.
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.
A single line containing the value.
Print the doubled value.
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.
First line: username. Second line: email. Third line: password hash.
Print the username and email only.
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.
No input.
Print 3.
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'.
A single line containing a code attempt.
Print True or False.
Sample Input 1
9999
Sample Output 1
False
Sample Input 2
1234
Sample Output 2
True
Sample Input 3
0000
Sample Output 3
False