What is the primary purpose of decorators in Python?
To improve the performance of a function.
To change the behavior of a function without modifying its source code.
To define a function within another function.
To create recursive functions.
Which string method is used to determine if a string ends with a specified suffix?
find()
endswith()
startswith()
index()
What will the following code snippet print?
d = {} d.setdefault('a', []).append(1) print(d['a'])
[]
1
KeyError: 'a'
[1]
Which method is used to write a single line of text to a file in Python?
write()
writeline()
put()
writeln()
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 create a function that can accept any number of arguments.
To make the function return a default value if no arguments are provided.
Which principle of OOP is violated if you have to modify numerous parts of your code when making a small change to a class?
Polymorphism
Abstraction
Inheritance
Encapsulation
Which module is used to work with CSV (Comma Separated Values) files in Python?
data
text
file
csv
What does the 'sys.argv' list in Python store?
Contents of the current working directory
System error messages
Environment variables
Command line arguments passed to the script
How can you access the constant 'pi' from the 'math' module after importing it?
math->pi
math(pi)
pi
math.pi
What is the primary benefit of defining custom exceptions in Python?
To improve code readability by using specific exception types.
To automatically log error messages to a file.
To prevent all types of exceptions from occurring.
To reduce the amount of code required for error handling.