1. What is the output of print(type(5/2)) in Python 3?
- a) <class 'int'>
- b) <class 'float'>
- c) <class 'double'>
- d) <class 'number'>
2.5 in this case).
Test your knowledge of Python with these interactive multiple-choice questions.
1. What is the output of print(type(5/2)) in Python 3?
2.5 in this case).
2. Which method is used to add an element at the end of a list?
append() adds a single element to the end: my_list.append(item).
3. What does the __init__ method do in a class?
4. Which operator performs integer division in Python?
// returns the floor value (e.g., 5//2 gives 2).
5. What is the output of print("Hello" * 3)?
6. Which collection is unordered and mutable in Python?
{1, 2, 3}.
7. What does range(1, 10, 2) generate?
range(start, stop, step) generates numbers from 1 to 9 (exclusive) with step 2.
8. How do you open a file for reading in Python?
open("file.txt", "r")open("file.txt", "read")open("file.txt", "w")open("file.txt")"r" mode opens a file for reading (default mode if omitted).
9. Which decorator is used to define a class method?
@classmethod receives the class (cls) as first argument.
10. What is the output of print([i for i in range(5) if i%2==0])?
11. Which function converts a string to lowercase?
lower() and casefold() convert strings to lowercase (casefold is more aggressive).
12. What is the output of print("Python"[1:4])?
13. Which module is used for working with regular expressions?
re module provides regex support: import re.
14. How do you create a virtual environment in Python?
venv (built-in) and virtualenv (third-party) can create virtual environments.
15. What is the purpose of __name__ == "__main__"?
16. Which method is used to remove a key from a dictionary?
my_dict.pop(key) removes the key and returns its value.
17. What does lambda x: x**2 create?
square = lambda x: x**2.
18. Which exception is raised when a key is not found in a dictionary?
KeyError.
19. What is the output of print("Hello".upper())?
upper() converts all characters to uppercase.
20. Which Python built-in is used to iterate over a sequence with indexes?
for i, item in enumerate(my_list): provides index-value pairs.