🐍 Python Interview Questions

Master Python Concepts for Technical Interviews

Duration: 30 Minutes
30:00
Questions: 15
15
Real Interview Questions
3
Difficulty Levels
30
Minutes Duration
8+
Python Topics

🎯 Ready for Your Python Interview?

This comprehensive test simulates real Python interview questions asked by top tech companies.

🔧 Core Python

Data types, variables, operators, control flow
🏗️ Data Structures

Lists, dicts, sets, tuples, comprehensions
🎯 OOP Concepts

Classes, inheritance, polymorphism
⚡ Advanced Topics

Decorators, generators, context managers
💡 Interview Tips: Take your time, read each question carefully, and think through the logic before selecting your answer.
Question 1/15
Beginner
What is the output of the following Python code?
def func(x=[]): x.append(1) return x print(func()) print(func()) print(func())
Mutable Default Arguments
A
[1]
[1]
[1]
B
[1]
[1, 1]
[1, 1, 1]
C
Error
D
Error
Question 7/15
Intermediate
What is the most Pythonic way to swap two variables?
Pythonic Practices
A
temp = a; a = b; b = temp
B
a, b = b, a
C
a = a + b; b = a - b; a = a - b
D
a = a ^ b; b = a ^ b; a = a ^ b
Question 8/15
Advanced
What will this generator function return?
def fibonacci(n): a, b = 0, 1 for _ in range(n): yield a a, b = b, a + b fib_gen = fibonacci(5) print(list(fib_gen))
Generators
A
[0, 1, 1, 2, 3]
B
[1, 1, 2, 3, 5]
C
[0, 1, 2, 3, 4]
D
[1, 2, 3, 5, 8]
Question 9/15
Beginner
What is the correct way to check if a key exists in a dictionary?
Dictionary Operations
A
if key in dict.keys():
B
if key in dict:
C
if dict.has_key(key):
D
if dict[key] is not None:
Question 10/15
Intermediate
What will be the output of this lambda function with map?
numbers = [1, 2, 3, 4, 5] result = list(map(lambda x: x * 2 if x % 2 == 0 else x * 3, numbers)) print(result)
Lambda Functions
A
[3, 4, 9, 8, 15]
B
[2, 4, 6, 8, 10]
C
[3, 6, 9, 12, 15]
D
[1, 4, 3, 8, 5]
Question 11/15
Advanced
What is the purpose of the `__new__` method in Python classes?
Advanced OOP
A
It initializes the object after creation
B
It creates and returns a new instance of the class
C
It's called when the object is deleted
D
It's used for inheritance only
Question 12/15
Intermediate
What will be the output of this set operation?
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} result = set1 & set2 print(result)
Set Operations
A
{1, 2, 3, 6, 7, 8}
B
{4, 5}
C
{1, 2, 3, 4, 5, 6, 7, 8}
D
set()
Question 13/15
Advanced
What is the correct way to implement a context manager using the `with` statement?
Context Managers
A
Implement __enter__ and __exit__ methods
B
Implement __with__ and __without__ methods
C
Implement __start__ and __stop__ methods
D
Implement __open__ and __close__ methods
Question 14/15
Intermediate
What is the difference between `is` and `==` operators in Python?
Operators & Comparison
A
No difference, they work the same way
B
`is` compares identity, `==` compares equality
C
`is` compares equality, `==` compares identity
D
`is` is for numbers, `==` is for strings
Question 15/15
Advanced
What will be the output of this metaclass example?
class Meta(type): def __new__(mcs, name, bases, attrs): attrs['class_id'] = f"{name}_ID" return super().__new__(mcs, name, bases, attrs) class MyClass(metaclass=Meta): pass obj = MyClass() print(obj.class_id)
Metaclasses
A
MyClass_ID
B
Meta_ID
C
class_id
D
Error

🎉 Interview Test Completed!

0/15

Great job!

0
Correct Answers
0
Incorrect Answers
0%
Interview Score
F
Grade
D
[]
[]
[]
Question 2/15
Intermediate
Which of the following is the correct way to handle multiple exceptions in Python?
Exception Handling
A
try: ... except (ValueError, TypeError): ...
B
try: ... except ValueError, TypeError: ...
C
try: ... except ValueError or TypeError: ...
D
try: ... except [ValueError, TypeError]: ...
Question 3/15
Advanced
What will be the output of this code involving closures?
def outer(): x = 10 def inner(): nonlocal x x += 1 return x return inner f = outer() print(f()) print(f()) print(outer()())
Closures & Scope
A
11
12
11
B
11
11
11
C
10
11
12
D
Error
Question 4/15
Intermediate
What is the difference between `__str__` and `__repr__` methods in Python classes?
Object-Oriented Programming
A
No difference, they are the same
B
__str__ is for end users, __repr__ is for developers/debugging
C
__repr__ is for end users, __str__ is for developers/debugging
D
__str__ returns bytes, __repr__ returns string
Question 5/15
Beginner
What will be the result of this list slicing operation?
lst = [1, 2, 3, 4, 5] print(lst[-2::-1])
List Operations
A
[4, 3, 2, 1]
B
[5, 4, 3, 2, 1]
C
[4, 5]
D
[3, 2, 1]
Question 6/15
Advanced
What is the output of this decorator example?
def my_decorator(func): def wrapper(*args, **kwargs): print("Before") result = func(*args, **kwargs) print("After") return result return wrapper @my_decorator def greet(name): print(f"Hello {name}") return f"Greeted {name}" result = greet("Alice") print(result)
Decorators
A
Hello Alice
Greeted Alice
B
Before
Hello Alice
After
Greeted Alice
C
Before
After
Hello Alice