Which collection module class is most suitable for implementing a queue data structure in Python?
OrderedDict
defaultdict
deque
Counter
Which string method is used to determine if a string ends with a specified suffix?
index()
endswith()
startswith()
find()
Which regular expression will match any string that starts with 'a' and ends with 'z'?
'a.+z'
'a.*z'
'^a.*z$'
'^a.+z$'
What is the purpose of the 'r' mode in the following Python code?
file = open('data.txt', 'r')
Create a new file for writing.
Open the file in read mode.
Read and write to the file.
Append data to an existing file.
How can you create a dictionary where the keys are numbers from 1 to 5 and the values are their squares using a dictionary comprehension?
[i: i**2 for i in range(1, 6)]
{i: i**2 for i in range(1, 6)}
(i: i**2 for i in range(1, 6))
{i**2: i for i in range(1, 5)}
Which of the following is NOT a built-in exception in Python?
ValueError
TypeError
FileError
IndexError
What is a key difference between instance methods and class methods in Python?
There is no practical difference; both can be used interchangeably.
Instance methods operate on specific instances of a class, while class methods operate on the class itself.
Class methods can access instance variables, while instance methods cannot access class variables.
Instance methods are defined using the 'classmethod' decorator, while class methods use the 'staticmethod' decorator.
Which method is used to write a single line of text to a file in Python?
write()
writeline()
put()
writeln()
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 collects any number of keyword arguments into a dictionary.
It enforces that the function must be called with at least two arguments.
It defines a variable number of default arguments.
It collects any number of positional arguments into a tuple.
Which of the following is a valid way to define a function with both default and keyword arguments in Python?
def my_function(a, b=2, **kwargs, c=3): # Function body
def my_function(a, b=2, **kwargs): # Function body
def my_function(a=1, b, c=3): # Function body
def my_function(a=1, *args, b, **kwargs): # Function body