PythonFan.org
Toggle Menu
Home
Online Python Compiler
Tutorials
Python FastAPI
Python Pandas
Python PyTorch
Python Seaborn
Blog
All Posts
Intermediate Python Concepts
Test your grasp of functions,modules,and object-oriented programming.
1. What is the output of [x**2 for x in range(3)]?
[0, 1, 4]
[1, 4, 9]
[0, 1, 2]
[2, 4, 6]
2. Which of the following are valid ways to handle exceptions in Python?
try/except
try/finally
if/else
raise
3. A generator function returns a generator object when called, not the yielded values directly.
True
False
4. What keyword is used to define a decorator in Python (when applying it to a function)?
5. Which method is required to make a class iterable?
__iter__
__next__
__getitem__
__call__
6. Which of the following are examples of Python context managers?
with open()
try/except
threading.Lock()
for loop
7. Lambda functions in Python can contain multiple return statements.
True
False
8. What is the output of: list(filter(lambda x: x%2==0, [1,2,3,4]))
9. What does the 'super()' function do in Python?
Calls a method from the parent class
Creates a new instance of a class
Defines a static method
Raises an exception
10. Which of the following are immutable data types in Python?
list
tuple
string
set
11. Type hints in Python are enforced at runtime by default.
True
False
12. What term describes a function that takes another function as an argument and returns a new function?
13. Which syntax creates a generator expression?
(x for x in range(5))
[x for x in range(5)]
{x for x in range(5)}
{x: x*2 for x in range(5)}
14. Which methods must an iterator implement?
__iter__
__next__
__len__
__getitem__
15. Python supports multiple inheritance (a class inheriting from multiple parents).
True
False
16. What is the output of: ''.join(['P', 'y', 't', 'h', 'o', 'n'])
17. What is the role of the 'yield' keyword in a function?
Return a value and pause the function
Exit the function immediately
Define a class attribute
Handle exceptions
18. Which are valid ways to create a dictionary?
dict(key=value)
{key: value}
list(key, value)
tuple(key: value)
19. The 'finally' block in try/except runs only if an exception occurs.
True
False
20. What built-in function returns the type of an object?
Reset
Answered 0 of 0 — 0 correct