Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - Collections
What will be the output of this code?
var dict = new Dictionary<string, int>() { {"a", 1}, {"b", 2} };
Console.WriteLine(dict["b"]);
A2
B1
CKeyNotFoundException
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary initialization

    The dictionary has keys "a" with value 1 and "b" with value 2.
  2. Step 2: Access value by key "b"

    dict["b"] returns the value 2 associated with key "b".
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Accessing dict["b"] = 2 [OK]
Quick Trick: Access dictionary by key to get its value [OK]
Common Mistakes:
MISTAKES
  • Confusing keys and values
  • Expecting exception when key exists
  • Thinking null is returned for existing keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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