Python Java SQL Course C C++ HTML CSS JS

Python Practice Questions

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

Abstraction Practice

Use abstract classes, ABC and interfaces to hide implementation details.

1
Abstract Class with ABC
Easy
Write an abstract class Shape with an abstract method area(). Attempting to instantiate it should fail.
No input.
Print 'Cannot instantiate abstract class'.
Sample Input 1

                      
Sample Output 1
Cannot instantiate abstract class
Sample Input 2

                      
Sample Output 2
Cannot instantiate abstract class
Sample Input 3

                      
Sample Output 3
Cannot instantiate abstract class
2
Abstract Method Implementation
Easy
Write an abstract class Shape with abstract area(), and a class Circle(Shape) implementing it.
No input (radius = 5).
Print the area rounded to 2 decimal places.
Sample Input 1

                      
Sample Output 1
78.50
Sample Input 2

                      
Sample Output 2
78.50
Sample Input 3

                      
Sample Output 3
78.50
3
Abstract Class with Concrete Method
Easy
Write an abstract class Vehicle with a concrete method start() printing 'Vehicle started', and abstract method fuel().
A single line containing a vehicle type.
Print the start message.
Sample Input 1

                      
Sample Output 1
Vehicle started
Sample Input 2

                      
Sample Output 2
Vehicle started
Sample Input 3

                      
Sample Output 3
Vehicle started
4
Simple ABC Usage
Easy
Write an abstract class Animal with abstract method sound(). Define Dog(Animal) implementing it.
No input.
Print 'Bark'.
Sample Input 1

                      
Sample Output 1
Bark
Sample Input 2

                      
Sample Output 2
Bark
Sample Input 3

                      
Sample Output 3
Bark
5
Abstract Method Must Be Overridden
Easy
Write an abstract class Appliance with abstract method turn_on(). Creating an object of a subclass that does not implement it raises TypeError.
No input.
Print 'Cannot instantiate'.
Sample Input 1

                      
Sample Output 1
Cannot instantiate
Sample Input 2

                      
Sample Output 2
Cannot instantiate
Sample Input 3

                      
Sample Output 3
Cannot instantiate
6
Hide Implementation Details
Easy
Write a class BankAccount with a public method get_balance() that hides how the balance is stored (private attribute).
No input (balance = 5000).
Print 5000.
Sample Input 1

                      
Sample Output 1
5000
Sample Input 2

                      
Sample Output 2
5000
Sample Input 3

                      
Sample Output 3
5000
7
Use ABC from abc Module
Easy
Write a program that imports ABC and abstractmethod and defines an abstract class. Print the abstract status of the class.
No input.
Print True.
Sample Input 1

                      
Sample Output 1
True
Sample Input 2

                      
Sample Output 2
True
Sample Input 3

                      
Sample Output 3
True
8
Concrete Method in Abstract Class
Easy
Write an abstract class Document with a concrete method open() printing 'Document opened'.
No input.
Print 'Document opened'.
Sample Input 1

                      
Sample Output 1
Document opened
Sample Input 2

                      
Sample Output 2
Document opened
Sample Input 3

                      
Sample Output 3
Document opened
9
Abstract Class with Multiple Subclasses
Easy
Write an abstract class Shape with abstract area(), and classes Circle and Square implementing it.
No input.
Print both areas on separate lines.
Sample Input 1

                      
Sample Output 1
78.5
16
Sample Input 2

                      
Sample Output 2
78.5
16
Sample Input 3

                      
Sample Output 3
78.5
16
10
Call Concrete Method on Subclass
Easy
Write an abstract class Machine with concrete method start() and abstract method run(). A subclass Robot implements run().
No input.
Print 'Machine started'.
Sample Input 1

                      
Sample Output 1
Machine started
Sample Input 2

                      
Sample Output 2
Machine started
Sample Input 3

                      
Sample Output 3
Machine started
1
Abstraction with Inheritance
Medium
Write an abstract class Shape with abstract area() and concrete description() returning 'This is a shape'.
A single line containing 'circle' or 'square'.
Print the description and area.
Sample Input 1
circle
Sample Output 1
This is a shape
78.5
Sample Input 2
square
Sample Output 2
This is a shape
16
Sample Input 3
circle
Sample Output 3
This is a shape
78.5
2
Abstract Bank with Rate
Medium
Write an abstract class Bank with abstract rate(), and classes SBI and HDFC implementing it.
A single line containing 'sbi' or 'hdfc'.
Print the interest rate.
Sample Input 1
sbi
Sample Output 1
6.5
Sample Input 2
hdfc
Sample Output 2
7.5
Sample Input 3
sbi
Sample Output 3
6.5
3
Interface Simulation with ABC
Medium
Write an abstract class Payment with abstract methods pay() and refund(). A class CreditCard implements both.
No input.
Print 'Paid' and 'Refunded' on separate lines.
Sample Input 1

                      
Sample Output 1
Paid
Refunded
Sample Input 2

                      
Sample Output 2
Paid
Refunded
Sample Input 3

                      
Sample Output 3
Paid
Refunded
4
Data Hiding for Abstraction
Medium
Write a class Login with a method authenticate(user, pw) that checks against a hidden private __credentials dict.
First line: username. Second line: password.
Print 'Success' or 'Failed'.
Sample Input 1
admin
1234
Sample Output 1
Success
Sample Input 2
root
wrong
Sample Output 2
Failed
Sample Input 3
admin
1234
Sample Output 3
Success
5
Abstract Class with Property
Medium
Write an abstract class Vehicle with abstract property speed, and a class Car(Vehicle) implementing it.
A single line containing the speed value.
Print the speed.
Sample Input 1
90
Sample Output 1
90
Sample Input 2
120
Sample Output 2
120
Sample Input 3
0
Sample Output 3
0
6
Multiple Abstract Methods
Medium
Write an abstract class SmartDevice with abstract methods connect() and disconnect(). A class WifiDevice implements both.
No input.
Print 'Connected' and 'Disconnected' on separate lines.
Sample Input 1

                      
Sample Output 1
Connected
Disconnected
Sample Input 2

                      
Sample Output 2
Connected
Disconnected
Sample Input 3

                      
Sample Output 3
Connected
Disconnected
7
Abstraction in a Library System
Medium
Write an abstract class LibraryItem with abstract get_details(). Classes Book and DVD implement it.
A single line containing 'book' or 'dvd'.
Print the item details.
Sample Input 1
book
Sample Output 1
Title: Python, Author: Guido
Sample Input 2
dvd
Sample Output 2
Title: Inception, Duration: 148
Sample Input 3
book
Sample Output 3
Title: Python, Author: Guido
8
Abstract Game Unit
Medium
Write an abstract class GameUnit with abstract attack() and concrete health attribute. A class Soldier implements attack().
No input.
Print 'Attacks with rifle'.
Sample Input 1

                      
Sample Output 1
Attacks with rifle
Sample Input 2

                      
Sample Output 2
Attacks with rifle
Sample Input 3

                      
Sample Output 3
Attacks with rifle
9
Constructor in Abstract Class
Medium
Write an abstract class Employee with __init__(name) storing the name, and abstract salary(). A class Developer implements salary().
A single line containing a name.
Print the name and salary.
Sample Input 1
Alice
Sample Output 1
Alice
70000
Sample Input 2
Bob
Sample Output 2
Bob
70000
Sample Input 3
Sam
Sample Output 3
Sam
70000
10
Abstract vs Concrete Separation
Medium
Write an abstract class Database with abstract connect() and concrete close() printing 'Connection closed'.
No input.
Print 'Connection closed'.
Sample Input 1

                      
Sample Output 1
Connection closed
Sample Input 2

                      
Sample Output 2
Connection closed
Sample Input 3

                      
Sample Output 3
Connection closed
1
Abstract Notification Framework
Hard
Write an abstract class Notifier with abstract send(message) and a template method notify() that logs 'Sending...' before sending.
A single line containing a message.
Print 'Sending...' then the sent message.
Sample Input 1
Hello
Sample Output 1
Sending...
Sending Hello
Sample Input 2
Hi
Sample Output 2
Sending...
Sending Hi
Sample Input 3
Bye
Sample Output 3
Sending...
Sending Bye
2
Abstract Shape with Validation
Hard
Write an abstract class Shape with abstract area(), and a class Triangle that validates (base, height > 0) or raises ValueError.
A single line containing two space-separated values.
Print the area, or 'Invalid dimensions'.
Sample Input 1
0 5
Sample Output 1
Invalid dimensions
Sample Input 2
3 4
Sample Output 2
6.00
Sample Input 3
-1 5
Sample Output 3
Invalid dimensions
3
Plugin Architecture with ABC
Hard
Write an abstract class Plugin with abstract run(), and classes GreetPlugin and MathPlugin implementing it.
A single line containing 'greet' or 'math'.
Print the plugin output.
Sample Input 1
math
Sample Output 1
Result: 42
Sample Input 2
greet
Sample Output 2
Hello from plugin
Sample Input 3
math
Sample Output 3
Result: 42
4
Abstract Cache System
Hard
Write an abstract class Cache with abstract get(key) and set(key, value), and a class InMemoryCache implementing both.
First line: key. Second line: value.
Print the stored value.
Sample Input 1
name
Alice
Sample Output 1
Alice
Sample Input 2
k
v
Sample Output 2
v
Sample Input 3
x
1
Sample Output 3
1
5
Multiple Inheritance with ABC
Hard
Write abstract classes Flyer with abstract fly(), and Swimmer with abstract swim(). A class Duck(Flyer, Swimmer) implements both.
No input.
Print 'Flying' and 'Swimming' on separate lines.
Sample Input 1

                      
Sample Output 1
Flying
Swimming
Sample Input 2

                      
Sample Output 2
Flying
Swimming
Sample Input 3

                      
Sample Output 3
Flying
Swimming
6
Abstract Authentication System
Hard
Write an abstract class Auth with abstract login(user, pw), and classes BasicAuth and OAuthAuth implementing it.
First line: auth type ('basic' or 'oauth'). Second line: user. Third line: password.
Print the login result message.
Sample Input 1
basic
admin
pass123
Sample Output 1
Basic login success
Sample Input 2
oauth
u
p
Sample Output 2
OAuth login success
Sample Input 3
basic
admin
x
Sample Output 3
Basic login success
7
Strategy Pattern with Abstraction
Hard
Write an abstract class SortStrategy with abstract sort(data), and classes BubbleSort and MergeSort implementing it.
A single line containing 'bubble' or 'merge'.
Print the strategy name used.
Sample Input 1
merge
Sample Output 1
Merge sort applied
Sample Input 2
bubble
Sample Output 2
Bubble sort applied
Sample Input 3
merge
Sample Output 3
Merge sort applied
8
Abstract with Factory Method
Hard
Write an abstract class Report with abstract generate(), and a factory function create_report(type) returning the right subclass.
A single line containing 'pdf' or 'html'.
Print the generated report type.
Sample Input 1
pdf
Sample Output 1
PDF report generated
Sample Input 2
html
Sample Output 2
HTML report generated
Sample Input 3
pdf
Sample Output 3
PDF report generated
9
Deep Abstraction — Payment Gateway
Hard
Write an abstract class Gateway with abstract charge(amount), and classes Stripe and PayPal implementing it.
A single line containing 'stripe' or 'paypal'.
Print the charge confirmation.
Sample Input 1
stripe
Sample Output 1
Charged 100 via Stripe
Sample Input 2
paypal
Sample Output 2
Charged 100 via PayPal
Sample Input 3
stripe
Sample Output 3
Charged 100 via Stripe
10
Abstract Machine with Steps
Hard
Write an abstract class Pipeline with abstract step1() and step2(), and a concrete run() that calls both in order.
A single line containing 'a' or 'b' to choose a pipeline.
Print both steps executed.
Sample Input 1
a
Sample Output 1
Step1: A
Step2: B
Sample Input 2
b
Sample Output 2
Step1: A
Step2: B
Sample Input 3
a
Sample Output 3
Step1: A
Step2: B
Competitive MCQs — Abstraction
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 33
Q1
What is abstraction in OOP?
Correct!
Wrong — correct answer is .
Abstraction exposes what an object does without exposing how.
Q2
Which module provides ABC (Abstract Base Classes)?
Correct!
Wrong — correct answer is .
from abc import ABC, abstractmethod enables abstract classes.
Q3
Which decorator marks a method as abstract?
Correct!
Wrong — correct answer is .
@abstractmethod forces subclasses to implement the method.
Q4
Can you create an object of an abstract class directly?
Correct!
Wrong — correct answer is .
Classes with abstract methods cannot be instantiated directly.
Q5
What is the output of this code?
python
1
2
3
4
5
6
7
8
9
from abc import ABC, abstractmethod
class A(ABC):
    @abstractmethod
    def go(self):
        pass
class B(A):
    def go(self):
        print("Going")
B().go()
Correct!
Wrong — correct answer is .
B implements go(), so it is concrete and prints "Going".
Q6
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def go(self):
pass
class B(A):
def go(self):
print("Going")
B().go()
Correct!
Wrong — correct answer is .
B implements go(), so it is concrete and prints "Going".
Q7
python
1
An abstract method has:
Correct!
Wrong — correct answer is .
Abstract methods define a contract; subclasses supply the behaviour.
Q8
An abstract method has:
Correct!
Wrong — correct answer is .
Abstract methods define a contract; subclasses supply the behaviour.
Q9
What happens if a subclass fails to implement an abstract method?
Correct!
Wrong — correct answer is .
A subclass missing an abstract method remains abstract itself.
Q10
Which is an example of abstraction?
Correct!
Wrong — correct answer is .
Interfaces that hide internals are abstraction in action.
Q11
What error is raised by this code?
python
1
2
3
4
5
6
from abc import ABC, abstractmethod
class A(ABC):
    @abstractmethod
    def f(self):
        pass
A()
Correct!
Wrong — correct answer is .
Instantiating an abstract class raises TypeError.
Q12
What error is raised by this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
A()
Correct!
Wrong — correct answer is .
Instantiating an abstract class raises TypeError.
Q13
Abstract classes are useful for:
Correct!
Wrong — correct answer is .
They define what subclasses must do, enabling consistent design.
Q14
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
class B(A):
def f(self):
return "B"
print(B().f())
Correct!
Wrong — correct answer is .
B implements the abstract f(), so it can be instantiated and called.
Q15
What does ABC stand for?
Correct!
Wrong — correct answer is .
ABC is the Abstract Base Class helper from the abc module.
Q16
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
class B(A):
pass
B()
Correct!
Wrong — correct answer is .
B never implements f(), so it is still abstract and cannot be instantiated.
Q17
Which decorator belongs to the abc module?
Correct!
Wrong — correct answer is .
abstractmethod marks a method that subclasses must implement.
Q18
What is the purpose of an abstract method?
Correct!
Wrong — correct answer is .
Abstract methods define a contract; concrete subclasses supply bodies.
Q19
What is the output of this code?
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s ** 2
print(Square(4).area())
Correct!
Wrong — correct answer is .
Square implements area(), so Square(4).area() returns 16.
Q20
Which of these can be instantiated?
Correct!
Wrong — correct answer is .
Only concrete classes implementing every abstract method may be instantiated.
Q21
What does an abstract class represent?
Correct!
Wrong — correct answer is .
Abstract classes provide shared structure and a required interface.
Q22
What is the output of this code?
from abc import ABC
class A(ABC):
pass
print(A())
Correct!
Wrong — correct answer is .
Without abstract methods, even an ABC subclass is concrete and instantiable.
Q23
Which is an example of abstraction in real life?
Correct!
Wrong — correct answer is .
Remotes expose only controls, hiding complex internals.
Q24
What must a subclass do when inheriting an abstract class?
Correct!
Wrong — correct answer is .
Missing implementations leave the subclass abstract.
Q25
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
class B(A):
def f(self):
return 1
class C(B):
pass
print(C().f())
Correct!
Wrong — correct answer is .
C inherits the concrete f() from B, so it is instantiable and returns 1.
Q26
Why use abstract classes instead of plain classes?
Correct!
Wrong — correct answer is .
Abstract classes guarantee subclasses implement required behaviors.
Q27
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
print("body")
class B(A):
pass
B().f()
Correct!
Wrong — correct answer is .
B is abstract because f() is unimplemented, so creating B() fails before any call.
Q28
Can an abstract class contain concrete methods?
Correct!
Wrong — correct answer is .
Abstract classes often provide concrete helpers alongside abstract requirements.
Q29
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
def g(self):
return "g"
@abstractmethod
def f(self):
pass
class B(A):
def f(self):
return "f"
b = B()
print(b.g(), b.f())
Correct!
Wrong — correct answer is .
B inherits concrete g() and implements abstract f().
Q30
What does abstractmethod do to a method body?
Correct!
Wrong — correct answer is .
The decorator marks the method abstract; subclass must override it.
Q31
Which is the difference between abstract class and interface in Python?
Correct!
Wrong — correct answer is .
Python lacks a formal interface keyword; ABCs with abstract methods act as interfaces.
Q32
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
print(hasattr(A, "f"))
Correct!
Wrong — correct answer is .
The abstract method f exists on class A itself.
Q33
Which is TRUE about abstract classes?
Correct!
Wrong — correct answer is .
Abstract classes are templates: inherit to use, instantiate to fail unless concrete.