Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Collections
What will be the output of this code?
var dict = new Dictionary<string, int>();
dict.Add("x", 10);
dict["y"] = 20;
Console.WriteLine(dict["x"] + dict["y"]);
A10 20
B30
Cx y
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary additions

    First, dict.Add("x", 10) adds key "x" with value 10. Then dict["y"] = 20 adds key "y" with value 20.
  2. Step 2: Calculate the sum printed

    dict["x"] is 10 and dict["y"] is 20, so their sum is 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    10 + 20 = 30 [OK]
Quick Trick: Sum values accessed by keys with dict[key] syntax [OK]
Common Mistakes:
MISTAKES
  • Expecting output as separate values instead of sum
  • Confusing keys and values in output
  • Thinking dict["y"] is invalid without Add

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes