What is method overloading in Python?

  • Automatically resolves ambiguity
  • Defining multiple methods
  • Python doesn't support it
  • Using the '@overload' decorator
Method overloading, as seen in some other languages, is not directly supported in Python. Python resolves method calls based on the arguments passed.

You have a function that takes three parameters, but sometimes you need to pass more arguments. Which technique allows for this?

  • Using default arguments
  • Using keyword arguments (kwargs)
  • Using optional parameters
  • Using variable-length argument lists (args)
To pass more arguments than expected, you can use variable-length argument lists (args), allowing for a flexible number of additional arguments.

In a Python codebase, you see that a child class is trying to modify an attribute with two leading underscores from a parent class but encounters an error. What's the likely cause?

  • Python does not allow modifying attributes from child classes.
  • The attribute is marked as a protected attribute and can only be accessed within the parent class.
  • The attribute with double underscores is treated as a name mangling name, and it's not directly accessible from child classes.
  • The child class does not have proper inheritance from the parent class.
In Python, an attribute with double underscores (e.g., __attribute) is treated as a name-mangling name. It's not directly accessible from child classes to prevent accidental overwriting.

_______ is the built-in Python exception for an error in the program logic.

  • RuntimeError
  • SyntaxError
  • TypeError
  • ValueError
SyntaxError is the built-in Python exception that occurs when there is an error in the program's syntax or structure.

In which file mode can you read from and write to a file simultaneously?

  • append-update mode
  • read-append mode
  • read-write mode
  • read-write-append mode
You can read from and write to a file simultaneously in 'read-write-append mode' ('r+'). It allows both reading and writing operations on the file.

The while loop continues execution as long as the test expression remains _______.

  • FALSE
  • Non-zero
  • TRUE
  • True or False
The while loop continues execution as long as the test expression remains True. It repeatedly executes a block of code as long as the specified condition evaluates to True.

Which of the following is used in Python to denote a block of code?

  • Curly braces {}
  • Indentation
  • Parentheses ()
  • Square brackets []
In Python, code blocks are denoted by indentation, typically using four spaces. It's a fundamental aspect of Python's syntax, known as the "off-side rule."

How can you reverse the order of elements in a list named my_list?

  • my_list.reverse()
  • my_list.sort(reverse=True)
  • my_list[::-1]
  • reversed(my_list)
To reverse the order of elements in a list, you can use my_list[::-1]. This slicing technique creates a new list with elements in reverse order, leaving the original list unchanged.

If you want to type hint a variable that can be either an integer or a float, you would use Union[int, _______].

  • Any
  • float
  • number
  • real
To type hint a variable that can be either an integer or a float, you would use 'Union[int, float]'. The 'Union' type hint allows specifying multiple acceptable types.

When reading a file in text mode in Python, what does the file method readline() return?

  • A boolean indicating EOF
  • A line of text
  • A list of lines in the file
  • The entire file contents
In Python, when you use the readline() method in text mode, it returns a line of text from the file, up to and including the newline character 'n'.