What is the purpose of the finally block in Python exception handling?
finally
It executes only if an exception occurs in the try block.
try
It always executes after a try block, regardless of whether an exception occurred.
It defines a custom exception to be raised.
It specifies the type of exception to catch.
What will the following code snippet print?
def my_decorator(func): def wrapper(): print("Before function call") func() print("After function call") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()
Before function call Hello! After function call
Before function call After function call
Error
Hello!
What is the purpose of the 'r' prefix in a regular expression pattern?
It treats the pattern as a raw string, ignoring escape sequences.
It enables multiline matching.
It performs a greedy match.
It makes the pattern case-insensitive.
What is the main goal of encapsulation in object-oriented programming?
To enable code reuse by inheriting from existing classes.
To create objects with multiple forms and behaviors.
To hide the internal implementation details of a class from the outside world.
To define a common interface for a set of related classes.
What is the primary purpose of using inheritance in Python's Object-Oriented Programming?
To improve code execution speed
To reduce code duplication and promote reusability
To enhance data security within a program
To enforce strict data type checking
Which module is used to work with CSV (Comma Separated Values) files in Python?
file
csv
data
text
What is a closure in Python?
A function that can be accessed from anywhere in the program.
A function that takes another function as an argument.
A function that is defined inside another function and has access to the outer function's variables.
A function that returns another function.
What is the primary purpose of using classes in Python?
To store data in a structured format like dictionaries.
To control program flow and logic.
To define functions for specific tasks.
To create reusable blueprints for objects with shared attributes and behaviors.
Which statement correctly imports the 'math' module in Python?
using math
import math
include math
require math
What is the most efficient way to concatenate a large number of strings in Python?
Using the '%s' operator.
Using the '+' operator.
It depends on the size of the strings.
Using the ''.join() method.