Which method is used to write a single line of text to a file in Python?
write()
put()
writeline()
writeln()
What does the Counter class from the collections module do?
Counter
collections
Stores key-value pairs in the order of insertion.
Creates dictionaries with a default value for missing keys.
Implements a last-in, first-out (LIFO) data structure.
Counts the occurrences of elements in an iterable.
How can you handle potential errors when opening a file in Python?
Using a for loop.
Using a try-except block.
Using an if-else statement.
Using a while loop.
What will the following code snippet print?
d = {} d.setdefault('a', []).append(1) print(d['a'])
1
KeyError: 'a'
[1]
[]
How can you get the current date and time using the 'datetime' module?
datetime.now
datetime.currentTime()
datetime.datetime.now()
datetime.today()
What is the primary advantage of using the with statement for file operations in Python?
with
It improves code readability.
All of the above.
It automatically closes the file, even if exceptions occur.
It provides better performance for large files.
What does the csv.reader() function in Python return?
csv.reader()
A list of strings.
A string containing the entire CSV file content.
A list of dictionaries.
A list of lists, representing rows and cells.
Consider the function definition: def my_func(a, b=5, *args, **kwargs): .... What does *args achieve?
def my_func(a, b=5, *args, **kwargs): ...
*args
It defines a variable number of default arguments.
It collects any number of positional arguments into a tuple.
It collects any number of keyword arguments into a dictionary.
It enforces that the function must be called with at least two arguments.
What is the primary benefit of defining custom exceptions in Python?
To reduce the amount of code required for error handling.
To automatically log error messages to a file.
To prevent all types of exceptions from occurring.
To improve code readability by using specific exception types.
Which module is used to work with CSV (Comma Separated Values) files in Python?
data
text
file
csv