What is the primary purpose of the try and except blocks in Python?

  • Creating loops
  • Declaring variables
  • Defining custom exception classes
  • Handling and gracefully managing exceptions
The primary purpose of the try and except blocks in Python is to handle and gracefully manage exceptions. These blocks allow you to specify how your program should respond when an exception is raised.

Why are tuples generally faster than lists when it comes to iteration?

  • Lists are stored in a linked list structure.
  • Lists have more built-in functions.
  • Tuples are immutable.
  • Tuples use less memory.
Tuples are generally faster for iteration because they are immutable. This immutability means that the interpreter can optimize memory usage and access elements directly, leading to faster iteration.

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.

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.

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.

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

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.

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.

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.

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.