Imagine you have a Circle class where you want the radius to always be non-negative. Which approach would you use to ensure this while still allowing direct assignment like circle.radius = 5?
- Making the radius attribute private and providing a public method to set it.
- Not allowing direct assignment to the radius attribute.
- Using a property with a setter method that checks and enforces the non-negativity of the radius.
- Using the @property decorator without a setter method.
You would use a property with a setter method to validate the radius and ensure it's non-negative while still allowing direct assignment. The setter method can perform the check.
You are given a list of strings. You need to create a new list that contains the length of each string in the original list. Which Python construct would be best suited for this?
- Filter and lambda functions
- For loop
- List comprehensions
- Map and lambda functions
List comprehensions are well-suited for this task as they allow you to generate a new list by applying an expression to each item in the original list.
Consider nested if statements in Python. What could be a potential drawback of having many levels of nested conditions?
- Decreased code complexity
- Improved readability
- Increased maintainability
- Increased potential for logic errors
Having many levels of nested conditions can increase the potential for logic errors because it becomes harder to track and debug the code, leading to increased complexity and decreased maintainability.
How does Python interpret the following line: # This is a comment?
- It's a multiline string
- It's a syntax error
- It's executed as code
- It's treated as a comment
In Python, lines starting with '#' are considered comments. They are ignored by the interpreter and serve as documentation for human readers.
If you want to make a class attribute read-only, you'd typically use the _______ decorator.
- @readonly
- @property
- @readonly.setter
- @attribute.getter
To make a class attribute read-only, you typically use the @property decorator. This decorator allows you to define a method that can be used to access the attribute but not set it directly.
When raising a custom exception, the _______ method gets called to display the error message.
- display_error
- raise_message
- str()
- str()
When raising a custom exception, the __str__() method of the exception class gets called automatically to generate the error message. You can override this method to customize the error message displayed when the exception is raised.
For a single if condition, if you want to run one block for a true condition and another block for a false condition, you need the if along with the ______ block.
- elif
- else
- else if
- otherwise
For a single if condition, if you want to run one block for a true condition and another block for a false condition, you need the 'if' along with the 'else' block. The 'else' block contains the code that is executed when the 'if' condition evaluates to False.
How did Python get its name?
- Named after a TV show
- Named after a computer game
- Named after a mathematician
- Named after a snake
Python is named after the famous British comedy group Monty Python, not the snake. Guido van Rossum, Python's creator, was a fan of the group.
Which of the following is true regarding the mutability of tuples in Python?
- Tuples are immutable
- Tuples are mutable
- Tuples can be partially mutable
- Tuples can be resized
Tuples are immutable in Python, meaning their elements cannot be changed after creation. However, they can contain mutable objects like lists.
You're building a logging system that reads logs from a CSV file and converts them into JSON format. What's the best approach to handle a very large CSV file to ensure efficient memory usage?
- Read the CSV file line by line and convert each line.
- Read the entire CSV file into memory and then convert it.
- Use a database to store CSV data and then convert it to JSON.
- Use the 'with' statement to open the file and process it efficiently.
The best approach for handling large CSV files is to read them line by line. Reading the entire file into memory can lead to memory issues, while processing line by line ensures efficient memory usage.