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.
If a derived class does not have its constructor, which constructor gets called when an object of the derived class is instantiated?
- Base class constructor
- Derived class constructor
- Python automatically creates a default constructor for the derived class.
- There is an error and no constructor is called.
If the derived class does not have its constructor, the base class constructor is called when an object of the derived class is instantiated. It's important to remember that if the base class constructor has parameters, you must explicitly call it in the derived class's constructor using super().init() to ensure proper initialization.
You are debugging a program that should print numbers from 1 to 10, but it's getting stuck and printing the same number indefinitely. What could be a possible reason?
- Incorrect Print Statement
- Infinite Loop
- Memory Overflow
- Syntax Error
The program printing the same number indefinitely suggests an infinite loop. Infinite loops can occur when the loop condition is not being updated correctly, leading to the program getting stuck on the same step. This can be due to a logical error in the loop's condition or the loop variable not being updated properly.
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.
A context manager's _______ method should return a Boolean value to control whether or not exceptions should be suppressed.
- __close__
- __enter__
- __exit__
- __manage__
A context manager's __exit__ method should return a Boolean value indicating whether exceptions should be suppressed (True) or propagated (False). It's responsible for handling exceptions within the context.