Python · Question 8 · Medium

What will be the output of the following Python code?

d = {k: len(k) for k in ["ram", "sita", "om"]}
print(sum(d.values()))
  1. 3
  2. 9
  3. 11
  4. {'ram': 3, 'sita': 4, 'om': 2}
Answer and explanation

Answer: B. 9

Explanation: The dictionary comprehension maps each name to its length: ram → 3, sita → 4, om → 2. The sum of the values is 9.

Previous: What will be the output of the following Python code? · Next: What will be the output of the following Python code?

Practise python

Try a fresh set of python questions with an explanation after every answer, free and without signing up: Python practice. Or sit a full placement mock test.

More python questions

More technical & programming practice

Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge