What is the primary role of the 'self' parameter in Python class methods?
It stores the return value of the method.
It refers to the current instance of the class, allowing access to instance variables and methods.
It refers to the class itself, allowing access to class variables.
It's a placeholder that doesn't have any specific purpose.
What does the Counter class from the collections module do?
Counter
collections
Implements a last-in, first-out (LIFO) data structure.
Counts the occurrences of elements in an iterable.
Stores key-value pairs in the order of insertion.
Creates dictionaries with a default value for missing keys.
What is the primary benefit of defining custom exceptions in Python?
To improve code readability by using specific exception types.
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.
What is the main goal of encapsulation in object-oriented programming?
To define a common interface for a set of related classes.
To create objects with multiple forms and behaviors.
To enable code reuse by inheriting from existing classes.
To hide the internal implementation details of a class from the outside world.
Which of the following is a valid way to define a function with both default and keyword arguments in Python?
def my_function(a=1, *args, b, **kwargs): # Function body
def my_function(a, b=2, **kwargs): # Function body
def my_function(a, b=2, **kwargs, c=3): # Function body
def my_function(a=1, b, c=3): # Function body
What will the following code snippet print?
d = {} d.setdefault('a', []).append(1) print(d['a'])
KeyError: 'a'
[]
[1]
1
Which of the following is a characteristic of encapsulation in Object-Oriented Programming?
Preventing the use of inheritance
Exposing all data and methods publicly
Allowing direct modification of an object's internal state from anywhere in the program
Bundling data and methods that operate on that data into a single unit (a class)
What is the purpose of using a default argument in a Python function?
To define a variable that can only be accessed within the function.
To specify a value for an argument if it is not passed in the function call.
To make the function return a default value if no arguments are provided.
To create a function that can accept any number of arguments.
What is the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6]] print(matrix[0][2])
IndexError: list index out of range
3
2
Which keyword is used to explicitly raise an exception in Python?
throw
raise
catch
try