Which string method is used to determine if a string ends with a specified suffix?
endswith()
find()
startswith()
index()
What is the output of the following code snippet?
data = [[1, 2, 3], [4, 5, 6]] result = [x * 2 for x in data[1]] print(result)
[1, 2, 3, 4, 5, 6]
[2, 4, 6, 8, 10, 12]
[4, 5, 6]
[8, 10, 12]
Which principle of OOP is violated if you have to modify numerous parts of your code when making a small change to a class?
Inheritance
Polymorphism
Encapsulation
Abstraction
What is the purpose of the OrderedDict class in Python?
OrderedDict
It optimizes dictionary lookups for faster retrieval of values.
It prevents duplicate keys from being added to a dictionary.
It allows you to create dictionaries with keys as immutable data types only.
It provides a dictionary that remembers the order in which keys were inserted.
What is the output of the following Python code?
with open('data.txt', 'w') as file: file.write('Hello, world!') print(file.read())
The code will raise an IOError.
Hello, world!
The code will print nothing.
The code will raise a ValueError.
What is the purpose of the 'r' prefix in a regular expression pattern?
It enables multiline matching.
It performs a greedy match.
It makes the pattern case-insensitive.
It treats the pattern as a raw string, ignoring escape sequences.
In Python, how do you access an instance variable within a class method?
Using the 'self' keyword followed by the variable name.
Directly using the variable name.
Instance variables cannot be accessed within class methods.
Using the class name followed by the variable name.
In Python, what is the difference between a class variable and an instance variable?
There is no difference; they are interchangeable terms.
Class variables can only store integers, while instance variables can store any data type.
Class variables are declared inside a method, while instance variables are declared outside methods.
Class variables are shared by all instances of a class, while instance variables are unique to each instance.
How do you create an instance of a class named 'Dog' in Python?
Dog.new()```
create Dog()```
Dog = new()```
my_dog = Dog()```
What is the primary advantage of using the with statement for file operations in Python?
with
All of the above.
It provides better performance for large files.
It improves code readability.
It automatically closes the file, even if exceptions occur.