What will be the output of the following Python code? A = [10, 20, 30] B = "U"…
2026
What will be the output of the following Python code?
A = [10, 20, 30]
B = "U"
D = dict.fromkeys(A, B)
print(D)
- A.
{10: 'U', 20: 'U', 30: 'U'}
- B.
{10: 'U', 20: None, 30: None}
- C.
{10: None, 20: None, 30: None}
- D.
[(10, 'U'), (20, 'U'), (30, 'U')]
Attempted by 142 students.
Show answer & explanation
Correct answer: A
The code initializes a list A with integers [10, 20, 30] and a string B as 'U'. The function dict.fromkeys(A, B) creates a dictionary where keys are elements from A and the value for each key is set to B. Since 'U' is an immutable string, it remains constant across all keys. The resulting dictionary D maps 10, 20, and 30 to 'U'. The print statement outputs {10: 'U', 20: 'U', 30: 'U'}.