Python Quiz
1. Which of the following is used to create a generator in Python?
Step-by-Step Explanation
The yield
keyword is used within a function to create a generator, which produces a sequence of values on demand.
Answer: a) yield ✅
2. What is the purpose of the __init__
method in a Python class?
Step-by-Step Explanation
The __init__
method is a special method (constructor) that is called when an object is created and is used to initialize the object's attributes.
Answer: c) To initialize object attributes. ✅
3. Which of the following is a mutable data type in Python?
Step-by-Step Explanation
A list
is a mutable data type, meaning its elements can be changed after it is created. tuple
, string
, and frozenset
are immutable.
Answer: b) list ✅
4. What is the purpose of the try...except
block in Python?
Step-by-Step Explanation
The try...except
block is used to catch and handle exceptions (errors) that might occur during the execution of a program.
Answer: d) To handle exceptions. ✅
5. Which function is used to open a file in Python?
Step-by-Step Explanation
The open()
function is used to open a file in Python, allowing you to read from or write to it.
Answer: a) open() ✅
6. What does the *args
parameter in a function definition allow?
Step-by-Step Explanation
The *args
parameter allows a function to accept a variable number of positional arguments, which are passed as a tuple.
Answer: b) Passing a variable number of positional arguments. ✅
7. Which of the following modules is used for regular expressions in Python?
Step-by-Step Explanation
The re
module provides regular expression matching operations in Python.
Answer: d) re ✅
8. What is the purpose of the __name__ == '__main__'
condition?
Step-by-Step Explanation
The __name__ == '__main__'
condition is used to check if a script is being run directly (as the main program) or being imported as a module.
Answer: c) To execute code only when the script is run directly. ✅
9. Which function is used to get the length of a list or string in Python?
Step-by-Step Explanation
The len()
function returns the length (number of items) of an object, such as a list or string.
Answer: a) len() ✅
10. What is the purpose of the with
statement in Python?
Step-by-Step Explanation
The with
statement is used for resource management, ensuring that resources like files are properly closed or released after use.
Answer: d) To manage resources (like files) and ensure they are properly cleaned up. ✅
11. Which of the following is an immutable data type in Python?
Step-by-Step Explanation
A tuple
is an immutable data type, meaning its elements cannot be changed after it is created. list
, dict
, and set
are mutable.
Answer: b) tuple ✅
12. What does the lambda
keyword do in Python?
Step-by-Step Explanation
The lambda
keyword is used to create anonymous functions (functions without a name) in Python.
Answer: c) Creates an anonymous function. ✅
13. Which method is used to add an element to the end of a list in Python?
Step-by-Step Explanation
The append()
method adds an element to the end of a list.
Answer: a) append() ✅
14. What is the purpose of the pass
statement in Python?
Step-by-Step Explanation
The pass
statement is a null operation; nothing happens when it is executed. It is used as a placeholder.
Answer: d) To act as a placeholder where a statement is syntactically required but no action is needed. ✅
15. Which method is used to remove an element from a specific index in a list?
Step-by-Step Explanation
The pop()
method removes and returns the element at a specific index in a list.
Answer: c) pop() ✅
16. What is the purpose of the __str__
method in a Python class?
Step-by-Step Explanation
The __str__
method is used to return a string representation of an object, which is useful for printing or displaying the object.
Answer: a) To return a string representation of an object. ✅
17. Which module is used for working with operating system dependent functionality?
Step-by-Step Explanation
The os
module provides functions for interacting with the operating system, such as file system operations.
Answer: c) os ✅
18. What does the **kwargs
parameter in a function definition allow?
Step-by-Step Explanation
The **kwargs
parameter allows a function to accept a variable number of keyword arguments, which are passed as a dictionary.
Answer: b) Passing keyword arguments as a dictionary. ✅
19. Which method is used to remove the first occurrence of a specific value from a list?
Step-by-Step Explanation
The remove()
method removes the first occurrence of a specific value from a list.
Answer: d) remove() ✅
20. What is the purpose of the enumerate()
function in Python?
Step-by-Step Explanation
The enumerate()
function returns an enumerate object that yields pairs of indices and values when iterating over a sequence.
Answer: b) To iterate over a sequence with index and value. ✅