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.

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.

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.

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.

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.

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.

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.

How can you access a method from a base class in a derived class if the method is overridden in the derived class?

  • By redefining the base class method in the derived class
  • By using the 'base' keyword
  • By using the 'super()' function
  • You cannot access the base class method
To access a method from a base class that is overridden in the derived class, you can use the 'super()' function. This allows you to call the base class method explicitly and execute its code alongside the derived class method. It's a common practice when you want to extend the functionality of the base class method in the derived class without completely replacing it.

You are given a list of words and you need to create a dictionary where keys are words and values are their respective lengths. Which Python construct will be most efficient for this?

  • word_lengths = {word: len(word) for word in words}
  • word_lengths = {} for i in range(len(words)): word_lengths[words[i]] = len(words[i])
  • word_lengths = {} for word in words: word_lengths[word] = len(word)
  • word_lengths = {} len(words, key=lambda x: word_lengths[word])
The most efficient way to create a dictionary with words as keys and their respective lengths as values is to use a dictionary comprehension. It is concise and Pythonic, iterating over the list of words and calculating the length of each word in a single step.