What method is used to add a single element to a set in Python?

  • add()
  • append()
  • insert()
  • update()
The add() method is used to add a single element to a set in Python. It takes one argument, the element you want to add, and modifies the set in place.

The _______ decorator in a class is used to turn a method into a class-specific method.

  • @classmethod
  • @classmethodmethod
  • @instancemethod
  • @staticmethod
The '@staticmethod' decorator in a class is used to turn a method into a class-specific method. Class-specific methods do not depend on instance-specific data and can be called on the class itself, without creating an instance.

How would you define a class attribute that should not be overridden by subclasses?

  • Declare it as a constant
  • Declare it as a static method
  • Prefix it with an underscore (_)
  • Use the @final decorator
To define a class attribute that should not be overridden by subclasses, prefix it with an underscore (_). While Python doesn't enforce strict encapsulation, this convention signals to other programmers that the attribute should not be modified.

Tuples in Python are _______.

  • Constant
  • Dynamic
  • Immutable
  • Mutable
Tuples in Python are immutable, which means their elements cannot be changed after creation. You can't add, remove, or modify elements in a tuple once it's defined. They are useful for storing constant data.

How can you retrieve the value associated with the key "name" from a dictionary d?

  • d.get("name")
  • d.retrieve("name")
  • d.value("name")
  • d["name"]
You can retrieve the value associated with the key "name" from a dictionary 'd' using square brackets: d["name"]. This syntax directly accesses the value associated with the key.

In the context of method overriding, what is the role of the super() function?

  • It calls the base class constructor
  • It calls the derived class constructor
  • It invokes the parent class method
  • It's used to create a new instance of the class
The super() function is used to invoke the method of the parent class. It's commonly used in method overriding to call the parent class's method while adding or modifying functionality in the derived class method.

The file _______ allows you to organize related modules into a single directory hierarchy.

  • __init__.py
  • __main__.py
  • __module__.py
  • __package__.py
The file __init__.py is a special Python file used to create packages. It allows you to organize related modules into a single directory hierarchy, and it is executed when the package is imported. It's essential for defining package-level variables, functions, or initialization code.

Consider a scenario where you need to check if neither 'a' nor 'b' are true in Python. Which of the following operators would you primarily use?

  • a and b
  • a or b
  • not (a or b)
  • not a and not b
To check if neither 'a' nor 'b' are true, you would use the 'not' operator before both 'a' and 'b'. The expression 'not a and not b' ensures that both 'a' and 'b' are not true.

How does Python enforce the private access modifier for class members?

  • By defining a 'private' keyword
  • By enclosing members in curly braces {}
  • By prefixing with an underscore (_)
  • By using the 'private' keyword
In Python, private access to class members is conventionally enforced by prefixing their names with an underscore (_). While this doesn't make them entirely inaccessible, it signals to other developers that these members should be treated as private and not accessed directly. It's a convention, not a strict enforcement, and it's part of Python's "we are all consenting adults here" philosophy.

How do you execute a block of code only if multiple conditions are all true in Python?

  • Using the 'and' operator
  • Using the 'if' operator
  • Using the 'not' operator
  • Using the 'or' operator
To execute a block of code only if multiple conditions are all true in Python, you use the 'and' operator. It checks if all the specified conditions are true before executing the associated code block.