PCEP 30 02 PCEP Certified Entry Level Python Programmer · Free Practice Question Easy
Question 8
What is the expected output of the following code ?
dict = {"Dalserver":12,"Ausserver":14,"Londonserver":16}
for i in dict.items():
print(i[0],end="")
-
A
DAL
-
B
DalserverAusserverLondonserver
-
C
12
-
D
121416
Reveal correct answer
Correct answer: B
Explanation
The for loop iterates through the items of the dictionary, and each i is a tuple containing a key-value pair.
i[0] accesses the first element of the tuple, which is the key (server name).
print(i[0], end="") prints each key without a newline character.
The output of the code will be the concatenation of the server names:
DalserverAusserverLondonserver
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
