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.
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.
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.
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.
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.
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.
When creating a placeholder for a function or loop that you will implement in the future, you should use the _______ statement.
- hold
- ignore
- pass
- skip
The pass statement is used as a placeholder for code that will be implemented in the future. It essentially does nothing and is often used to avoid syntax errors.
To override a method in the derived class, the method must have the same _______ as the method in the base class.
- Implementation
- Name
- Return Type
- Signature
To override a method in the derived class, the method must have the same "Signature" as the method in the base class. The signature includes the method name and its parameters.
You're writing code for a game. How would you structure the conditional checks to determine the player's status?
- None of the above
- if score < 3000:
- if score > 5000:
- if score >= 3000 and score <= 5000:
To determine the player's status based on their score, you should use 'if-elif-else' statements. Checking for different score ranges in this manner ensures that the appropriate status is assigned based on the player's score.
The csv module's _______ class can be used to read rows from a CSV file as dictionaries.
- csv.DictFile
- csv.DictReader
- csv.Dictionary
- csv.Reader
The csv module's csv.DictReader class can be used to read rows from a CSV file as dictionaries, where keys are based on the header row, making it easy to work with structured CSV data.
Use context managers and the 'with' statement to encapsulate the setup and teardown actions.
- Create separate functions for setup and teardown
- Depend on the order of function execution
- Rely on decorators to handle setup and teardown logic
- Use global variables for setup and teardown
The Pythonic way to manage repetitive setup and teardown is to use context managers and the 'with' statement. It enhances code readability and maintainability.
Which of the following operators is a floor division in Python?
- % (Modulus)
- ** (Double Asterisk)
- / (Single Slash)
- // (Double Slash)
The floor division operator in Python is represented by // (double slash). It divides two numbers and returns the largest integer less than or equal to the result.