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.
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.
Tuples can be used as keys in dictionaries because they are _______.
- Hashable
- List-like
- Ordered
- Unchangeable
Tuples can be used as keys in dictionaries because they are hashable, which means their contents do not change, making them suitable for dictionary keys.