In which scenario would the @classmethod decorator be more appropriate than @staticmethod?

  • When the method doesn't require access to class-specific data
  • When the method is called on an instance
  • When the method needs no arguments
  • When the method requires access to class-specific data
The @classmethod decorator is more appropriate than @staticmethod when the method needs access to class-specific data, such as class attributes or methods. @staticmethod is used when the method doesn't rely on any class-specific data.

Imagine you are developing a game and you have a Player class. Every player has a unique ID and a score. Which method would be best suited to fetch the highest score across all players without needing an instance of the player class?

  • classmethod
  • instance method
  • property
  • staticmethod
To fetch the highest score across all players without needing an instance of the player class, you should use a staticmethod. Static methods are called on the class itself and don't require an instance, making them suitable for tasks that don't involve instance-specific data.

If you want to check multiple conditions and execute a block when the first true condition is encountered, you use the ______ keyword after the initial if statement.

  • elif
  • elseif
  • or else
  • or if
To check multiple conditions and execute a block when the first true condition is encountered, you use the 'elif' keyword. 'elif' is short for 'else if' and allows you to chain multiple conditions together, with the block associated with the first true condition being executed.

What will be the result of the comparison 5 != 6 in Python?

  • 1
  • Error
  • FALSE
  • TRUE
The expression 5 != 6 compares whether 5 is not equal to 6. Since this is true, the result is 'True.'

Python files have the extension _______.

  • .code
  • .py
  • .script
  • .txt
Python files typically have the extension '.py'. It's the standard file extension for Python source code files.

You're developing an application where user input determines what discount they receive. Which structure would be most suitable for this kind of decision-making?

  • for loop
  • if-elif-else chain
  • nested if statements
  • switch-case statement
For this type of decision-making based on user input, an 'if-elif-else' chain is the most suitable structure. It allows you to check multiple conditions in sequence and execute the corresponding block of code when a condition is met.

You're debugging a script and encounter an empty function that crashes the program because there's no code inside it. Which statement can be temporarily added to the function to avoid this error?

  • continue
  • pass
  • raise Exception
  • return None
You can use the pass statement to temporarily add empty code to a function. It does nothing and allows the program to continue without any errors, making it useful as a placeholder during debugging or when the function is not yet implemented.

Which of the following best describes the behavior of the continue statement inside a loop?

  • It exits the loop and resumes execution from the beginning of the loop.
  • It is used to define an exception handling block within the loop.
  • It skips the rest of the current iteration and proceeds to the next iteration of the loop.
  • It terminates the loop entirely.
The continue statement, when encountered, skips the current iteration of a loop and proceeds to the next iteration. It does not exit the loop. This is often used to skip specific elements or conditions within a loop without terminating it.

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.

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'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.

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.