To check if a dictionary d has a key "name," you can use the expression "name" in _______.

  • d
  • d.has_key()
  • d.has_name()
  • d.keys()
To check if a dictionary has a specific key, you can use the in keyword followed by the dictionary's keys() method. For example, if "name" in d.keys(): will check if the key "name" exists in the dictionary d.

In Python, to execute a block of code when a condition is false, you use the ______ statement.

  • elif
  • else if
  • else when
  • otherwise
In Python, to execute a block of code when a condition is false, you use the 'else' statement. The 'else' block is executed when the preceding 'if' condition evaluates to False, providing an alternative code path.

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.

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.