What keyword is used to stop the execution of the current iteration and proceed to the next in a loop?

  • break
  • continue
  • skip
  • stop
The 'continue' keyword is used to stop the execution of the current iteration in a loop and proceed to the next iteration. It allows you to skip specific iterations.

In a project where memory efficiency and speed are of utmost priority and the data doesn't need any modification once set, which data structure would be a better choice?

  • Dictionary
  • List
  • Set
  • Tuple
Tuples are an excellent choice when memory efficiency and speed are crucial because they are immutable and consume less memory compared to lists. Their immutability also ensures that data integrity is maintained, making them suitable for scenarios where data should not be modified after initialization.

When importing a module, what is the primary purpose of using the as keyword?

  • To create an alias for the module
  • To hide the module's functions
  • To prevent the module from being imported again
  • To specify the module's version
The 'as' keyword is used to create an alias for a module when importing it. This alias can make it easier to reference the module in your code, especially if the module name is long or conflicts with other names in your script.

You have a function that needs to return multiple values, which data structure is more commonly used for such a purpose?

  • Dictionary
  • List
  • Set
  • Tuple
Tuples are commonly used to return multiple values from a function because they are immutable (cannot be changed), ensuring data integrity. This makes them a safer choice than lists, which are mutable. Using a tuple also communicates that the returned values should not be modified.

You can use the 'try...finally' block in Python to ensure that the database connection is always closed.

  • Implement a custom function to manage connections
  • Manually close the connection in each place
  • Rely on Python's automatic garbage collection
  • Use a context manager like 'with' statements
The 'try...finally' block ensures that the database connection is closed even if an exception occurs. It's a robust way to manage resource cleanup.

You're reviewing a Python class and notice that many attributes are accessed and modified directly, without any checks. What could be introduced to provide controlled access and validation to these attributes?

  • Creating a separate module to handle attribute access and modification.
  • Defining the attributes within a function for validation.
  • Encapsulating the attributes by making them private and providing getter and setter methods.
  • Using global variables to track and validate attribute changes.
To provide controlled access and validation to attributes, you should encapsulate the attributes by making them private and providing getter and setter methods to enforce checks and validation.

You're working with a dictionary where keys are country names and values are their capitals. You want to invert this dictionary. What potential issue might you encounter?

  • No potential issue, dictionaries are always invertible.
  • Potential issue: Overwriting keys if country names are not unique.
  • Potential issue: Overwriting values if capitals are not unique.
  • Potential issue: Python dictionaries cannot be inverted.
When inverting a dictionary, if the values (capitals) are not unique, you may encounter an issue where some values get overwritten. This results in the loss of information, as multiple countries may have the same capital. Inverting dictionaries assumes unique values for keys in the original dictionary.

What is the primary reason to use method overriding in object-oriented programming?

  • To create a new method
  • To extend a class's functionality
  • To hide a method's implementation
  • To improve code readability
The primary reason to use method overriding is to hide a method's implementation in the base class and provide a specialized implementation in the derived class. This allows for polymorphism and abstraction.

Sets in Python are _______ which means they cannot contain duplicate elements.

  • immutable
  • mutable
  • ordered
  • unhashable
Sets in Python are 'immutable,' which means they cannot contain duplicate elements. Once a set is created, you cannot change its elements, but you can add or remove elements from it.

A variable name in Python cannot start with a _______.

  • Letter
  • Number
  • Special
  • Underscore
In Python, variable names cannot start with a number, as they must begin with a letter or an underscore (_).