When might you choose to raise an exception manually using raise?
raise
To automatically retry a failed operation without any intervention.
To replace all occurrences of built-in exceptions in your code.
To silently ignore errors and prevent the program from crashing.
When you want to signal that an unexpected condition has occurred that your code cannot handle directly.
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, *args, b, **kwargs): # Function body
def my_function(a=1, b, c=3): # Function body
Which of the following is a characteristic of encapsulation in Object-Oriented Programming?
Bundling data and methods that operate on that data into a single unit (a class)
Exposing all data and methods publicly
Allowing direct modification of an object's internal state from anywhere in the program
Preventing the use of inheritance
What is the purpose of the finally block in Python exception handling?
finally
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.
It specifies the type of exception to catch.
Which regular expression will match any string that starts with 'a' and ends with 'z'?
'^a.*z$'
'^a.+z$'
'a.+z'
'a.*z'
Which data structure would be most appropriate for representing a graph in Python?
All of the above options can be used to represent a graph.
A dictionary where keys are nodes, and values are lists of their neighbors.
A list of lists, where each inner list represents a node and its connections.
A set of tuples, where each tuple represents an edge between two nodes.
How can you best describe the functionality of a decorator in Python?
It enforces type checking on function arguments.
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 defines a new class dynamically.
What will the following code snippet print?
d = {} d.setdefault('a', []).append(1) print(d['a'])
[1]
KeyError: 'a'
1
[]
What does the 'sys.argv' list in Python store?
Environment variables
System error messages
Contents of the current working directory
Command line arguments passed to the script
What does the 're.findall()' function return?
The first match of the pattern in the string.
None of the above.
A list of all non-overlapping matches of the pattern in the string.
A boolean value indicating whether the pattern is present in the string.