Discussion
What is the expected output of the following Python code?
print("\nDictionary Iteration")
d = dict()
d['lmn'] = 123
d['abc'] = 345
for i in d :
print("%s %d" %(i, d[i]))
Answer: A
The output is a blank line, then 'Dictionary Iteration', then 'lmn 123' and 'abc 345'. The string begins with \n, which prints an empty line first. The dictionary d keeps the keys in the order inserted (in current Python versions), so the loop for i in d goes over 'lmn' and then 'abc', and each pass prints the key with %s and its value with %d. So the lines are 'lmn 123' and 'abc 345'. The other options swap the key and the value or print wrong values, and they also have a stray quote mark in the title line.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development