Exercise: Tricky Questions

Questions for: Tricky Questions

Consider the following Python code:
def some_function(*args, **kwargs):
    return args, kwargs

result = some_function(1, 2, a=3, b=4)
print(result)
What will be the value of result?
A:
((1, 2), {'a': 3, 'b': 4})
B:
([1, 2], {'a': 3, 'b': 4})
C:
((1, 2), ('a': 3, 'b': 4))
D:
([1, 2], ('a': 3, 'b': 4))
Answer: A
The function collects positional arguments in a tuple (args) and keyword arguments in a dictionary {kwargs}.
What is the output of the following Python code?
x = 5
y = x if x > 10 else x/2
print(y)
A:
5
B:
2.5
C:
10
D:
This code will result in an error.
Answer: B
The ternary conditional expression sets y to x if x > 10, otherwise it sets y to x/2.
What will be the output of the following Python code?
def func(x, y, z):
    return x + y * z

result = func(1, 2, 3)
print(result)
A:
9
B:
7
C:
8
D:
1
Answer: B
The multiplication has higher precedence than addition, so the result is 1 + (2 * 3) = 7.
What is the output of the following Python code?
def modify_list(my_list):
    my_list[0] = 5

original_list = [1, 2, 3]
modify_list(original_list.copy())
print(original_list)
A:
[1, 2, 3]
B:
[5, 2, 3]
C:
This code will result in an error.
D:
[1, 2, 3, 5]
Answer: A
The modify_list function modifies a copy of the list, not the original list.
Consider the following Python code:
class CustomError(Exception):
    def __init__(self, message):
        super().__init__(message)

raise CustomError("An example custom error.")
What will happen when this code is executed?
A:
The code will run without any errors.
B:
The code will result in a TypeError.
C:
The code will result in a NameError.
D:
The code will raise a custom error of type CustomError.
Answer: D
The code raises a custom error of type CustomError with the specified message.
Ad Slot (Above Pagination)
Quiz