The correct way to check if a key is present in a dictionary is to use the in keyword directly on the dictionary.
Discuss About this Question.
What will be the output of the following Python code?
x = 5
y = 2
result = x // y
A:
2.5
B:
2
C:
3
D:
2.0
Answer:B
The // operator performs integer division in Python, discarding any remainder. Therefore, 5 // 2 results in 2.
Discuss About this Question.
What is the value of result after executing the following Python code?
my_list = [1, 2, 3, 4, 5]
result = my_list[::2]
A:
[1, 3, 5]
B:
[2, 4]
C:
[5, 3, 1]
D:
[1, 2, 3, 4, 5]
Answer:A
The slicing notation my_list[::2] extracts elements with a step of 2, starting from index 0. Therefore, it includes elements at indices 0, 2, and 4, resulting in the list [1, 3, 5].
Discuss About this Question.
What is the output of the following Python code?
def mystery_function(x):
return x if x > 0 else mystery_function(-x)
result = mystery_function(-5)
print(result)
A:
-5
B:
5
C:
0
D:
This code will result in an infinite loop.
Answer:B
The mystery_function recursively calls itself with the absolute value of the input until the input is greater than 0. Therefore, mystery_function(-5) returns mystery_function(5), resulting in the output 5.
Discuss About this Question.
Ad Slot (Above Pagination)
Install ExamAdept
Fast access — add this app to your device.
To install on iPhone/iPad: tap Share → Add to Home Screen.
Discuss About this Question.