Which of the following lines will throw an indentation error in Python?
- if True:
- print("Hello, world!")
- print("Python is fun!")
- print("Python is easy!")
The correct option is 4. It lacks proper indentation, which is essential in Python to define code blocks. An indentation error would occur in this case.
Which method is responsible for cleaning up resources in a context manager?
- __cleanup__()
- __close__()
- __deallocate__()
- __exit__()
The __exit__() method is responsible for cleaning up resources in a context manager. It's called automatically when exiting the 'with' block, allowing you to release resources or handle exceptions.
Which Python conditional statement allows for the checking of multiple conditions sequentially until one is found to be true?
- if-elif
- if-else if
- switch-case
- while
In Python, the 'if-elif' (short for "else if") statement allows for checking multiple conditions sequentially until one is found to be true. This is useful for handling multiple exclusive cases.
You have a list of numbers, and you want to compute the cumulative sum of elements. Which looping structure is best suited for this?
- do-while loop
- for loop
- list comprehension
- while loop
The 'for loop' is best suited for computing the cumulative sum of elements in a list. It allows you to iterate through the list and accumulate the sum of elements as you go, making it an efficient choice for this task.
If a class in Python has a method named _protectedMethod, it's a convention that this method is intended to be _______.
- internal
- private
- protected
- public
In Python, methods with a single leading underscore like '_protectedMethod' are considered protected, indicating they should be used with caution.
What happens if the continue statement is used outside any loop?
- It causes an error
- It exits the program
- It has no effect
- It skips the next statement
Using 'continue' outside of a loop in Python will result in a syntax error. The 'continue' statement is meant to be used within loop structures to skip the current iteration and proceed to the next one.
What is the primary purpose of the try and except blocks in Python?
- Creating loops
- Declaring variables
- Defining custom exception classes
- Handling and gracefully managing exceptions
The primary purpose of the try and except blocks in Python is to handle and gracefully manage exceptions. These blocks allow you to specify how your program should respond when an exception is raised.
Why are tuples generally faster than lists when it comes to iteration?
- Lists are stored in a linked list structure.
- Lists have more built-in functions.
- Tuples are immutable.
- Tuples use less memory.
Tuples are generally faster for iteration because they are immutable. This immutability means that the interpreter can optimize memory usage and access elements directly, leading to faster iteration.
A method inside a class that does not access or modify class-specific data is often marked as a _______.
- abstract
- instance
- non-static
- static
A method inside a class that does not access or modify class-specific data is often marked as a static method.
Your application needs to write user data into a file. To ensure data consistency, even if the application crashes during a write operation, what measure would you take?
- Enable automatic file versioning to track changes and ensure consistency.
- Use a try-except block to handle crashes and continue writing.
- Use file locking to prevent other processes from accessing the file during writes.
- Write data to a temporary file and then replace the original file.
To ensure data consistency, write data to a temporary file first, and then replace the original file. This way, if the application crashes during writing, the original file remains intact.