Which keyword is specifically used to terminate a loop prematurely?

  • break
  • exit
  • halt
  • terminate
The break keyword is used to prematurely terminate a loop in Python. When encountered, it exits the loop and continues with the next statement.

When using nested loops, the break statement will exit the _______ loop.

  • Current
  • Inner
  • Outer
  • Parent
The 'break' statement in Python is used to exit the innermost loop when used in nested loops. It terminates the loop in which it is placed, hence exiting the 'Inner' loop.

Which of the following statements is true about private attributes in a Python class?

  • Private attributes are accessible and modifiable outside the class, just like public attributes.
  • Private attributes are denoted by a double underscore prefix, such as "__private". They can be accessed and modified outside the class.
  • Private attributes are denoted by a single underscore prefix, such as "_private". They are intended for internal use within the class.
  • Private attributes are inaccessible from outside the class, making them more secure.
Private attributes in Python are typically denoted by a single underscore prefix (e.g., "_private"). While they can still be accessed from outside the class, the convention is to treat them as non-public and for internal use.

The _______ file can control which modules are imported when 'from package import *' is invoked.

  • import_rules.txt
  • init.py
  • main.py
  • package_config.ini
The init.py file in a Python package can control which modules are imported when 'from package import *' is used. It allows you to define the package's public interface.

Which keyword is used in Python to create a derived class?

  • baseclass
  • class
  • extends
  • superclass
In Python, the 'class' keyword is used to create both base (parent) classes and derived (child) classes. The derived class is created by inheriting from a base class.

The process of converting source code into bytecode, which is then executed by the Python interpreter, is called _______.

  • Compilation
  • Compilation
  • Execution
  • Interpretation
Python uses interpretation, not compilation, to execute code. The process is called interpretation, not compilation.

You are designing a Vector class to represent 2D vectors, and you want to add two vectors using the + operator. How would you implement this?

  • Create a new class, VectorAdder, to handle vector addition.
  • Implement a separate function, add_vectors(), for adding vectors.
  • Overload the + operator in the Vector class, so it combines the x and y components of the vectors.
  • Overload the += operator for the Vector class to support in-place addition of vectors.
The correct approach is to overload the + operator in the Vector class, allowing for a natural syntax for vector addition. This is a common operator overloading scenario.

Consider a loop that processes each item in a list. If you want to skip processing for specific items and continue with the next one, which statement would you use?

  • break
  • continue
  • pass
  • skip
To skip processing for specific items in a loop and continue with the next one, you would use the 'continue' statement. It allows you to bypass the current iteration and move on to the next item in the loop.

Which method would you use to get a list of all the keys in a dictionary?

  • d.items()
  • d.keylist()
  • d.keys()
  • d.values()
To get a list of all the keys in a dictionary 'd', you would use the d.keys() method. It returns a list of all the keys present in the dictionary.

The variables that are defined inside a function are referred to as _______ variables.

  • Global
  • Instance
  • Local
  • Non-local
Variables defined inside a function are known as 'local' variables. They are only accessible within that function's scope.