Python Programming - Tricky Questions

Exercise : Tricky Questions - General Questions
  • Tricky Questions - General Questions
11.
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)
[1, 2, 3]
[5, 2, 3]
This code will result in an error.
[1, 2, 3, 5]
Answer: Option
Explanation:
The modify_list function modifies a copy of the list, not the original list.

12.
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)
9
7
8
1
Answer: Option
Explanation:
The multiplication has higher precedence than addition, so the result is 1 + (2 * 3) = 7.

13.
What is the output of the following Python code?
x = 5
y = x if x > 10 else x/2
print(y)
5
2.5
10
This code will result in an error.
Answer: Option
Explanation:
The ternary conditional expression sets y to x if x > 10, otherwise it sets y to x/2.

14.
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?
((1, 2), {'a': 3, 'b': 4})
([1, 2], {'a': 3, 'b': 4})
((1, 2), ('a': 3, 'b': 4))
([1, 2], ('a': 3, 'b': 4))
Answer: Option
Explanation:
The function collects positional arguments in a tuple (args) and keyword arguments in a dictionary {kwargs}.

15.
What is the output of the following Python code?
x = [1, 2, 3]
y = x + [4, 5]
z = x.extend([4, 5])
print(x, y, z)
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5] None
[1, 2, 3] [1, 2, 3, 4, 5] None
[1, 2, 3, 4, 5] [1, 2, 3] None
[1, 2, 3] [1, 2, 3] None
Answer: Option
Explanation:
x is a list with values [1, 2, 3]. y is the result of concatenating x with [4, 5]. z is the result of extending x with [4, 5], but it returns None. The values of x, y, and z are printed. Thus, the output is [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] None.