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.

What happens when a function doesn't have a return statement?

  • It raises an error
  • It returns 0
  • It returns None
  • It returns a blank string
When a function lacks a return statement, it automatically returns None. This indicates that the function doesn't return a specific value.

Which of the following tuple methods returns the number of times a specified value appears in the tuple?

  • count()
  • find()
  • index()
  • length()
The count() method is used to determine how many times a specified value appears in a tuple. It's a useful tool for counting occurrences of elements in tuples.

To create a module in Python, you need to write your functions, classes, and variables in a _______ file.

  • .module
  • .pack
  • .py
  • .txt
To create a module in Python, you need to write your functions, classes, and variables in a '.py' file, which is a Python source code file.

Which of the following is a modulus operator in Python?

  • %
  • &
  • *
  • ~
The '%' symbol in Python is the modulus operator. It returns the remainder when the first operand is divided by the second operand. For example, 10 % 3 returns 1 because 10 divided by 3 leaves a remainder of 1.

Python's reference implementation is known as _______.

  • CPython
  • PyInterpreter
  • PyRef
  • Pythonic
Python's reference implementation is known as CPython. It is the default and most widely used implementation of Python. Understanding this helps differentiate it from other implementations like Jython or IronPython.

Which of the following is not typically a use case for the with statement in Python?

  • Creating a context manager
  • Managing database connections
  • Opening and closing files
  • Performing arithmetic calculations inside a loop
The with statement is typically used for resource management, such as file handling or database connections. It is not meant for performing arithmetic calculations.

What's the primary benefit of using tuples as keys in dictionaries over lists?

  • Tuples are faster to access.
  • Tuples are immutable.
  • Tuples are more memory-efficient.
  • Tuples guarantee order.
The primary benefit of using tuples as keys in dictionaries is that tuples are immutable, while lists are not. This immutability ensures that the keys won't change, making tuples suitable for use as dictionary keys. Lists can't be used as keys because they can change.

_______ is a built-in Python function that dynamically loads a module into the current namespace.

  • import
  • include
  • load
  • require
The import statement is used to dynamically load a module into the current Python namespace. It allows you to access functions, classes, and variables defined in the imported module. This dynamic loading is crucial for code modularity and reusability, enabling you to use code from different modules in your Python programs.