How can you best describe the functionality of a decorator in Python?
It modifies the behavior of a function without directly altering its source code.
It creates a copy of a function with a different name.
It enforces type checking on function arguments.
It defines a new class dynamically.
Which method is used to write a single line of text to a file in Python?
put()
writeln()
write()
writeline()
What is the purpose of using a default argument in a Python function?
To make the function return a default value if no arguments are provided.
To create a function that can accept any number of arguments.
To specify a value for an argument if it is not passed in the function call.
To define a variable that can only be accessed within the function.
What is the primary advantage of using a list comprehension over a traditional for loop when creating a new list in Python?
List comprehensions can be used with any iterable, while for loops are limited to lists.
List comprehensions offer significant performance improvements, especially for large lists.
List comprehensions are more readable and concise for simple list creations.
List comprehensions allow you to modify existing lists, while for loops can only create new ones.
What does the csv.reader() function in Python return?
csv.reader()
A list of dictionaries.
A list of lists, representing rows and cells.
A string containing the entire CSV file content.
A list of strings.
Which of the following is NOT a built-in exception in Python?
ValueError
IndexError
FileError
TypeError
How do you create a custom exception class in Python?
By defining a new class that inherits from the BaseException class or one of its subclasses.
BaseException
By using the custom_exception() function.
custom_exception()
By using the define_exception() keyword.
define_exception()
By creating a new instance of the Exception class.
Exception
Which string method is used to determine if a string ends with a specified suffix?
index()
find()
startswith()
endswith()
What is the primary purpose of decorators in Python?
To improve the performance of a function.
To define a function within another function.
To change the behavior of a function without modifying its source code.
To create recursive functions.
What is the purpose of the finally block in Python exception handling?
finally
It specifies the type of exception to catch.
It always executes after a try block, regardless of whether an exception occurred.
try
It executes only if an exception occurs in the try block.
It defines a custom exception to be raised.