Bird
Raised Fist0

Find the issue in this C# code:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Collections
Find the issue in this C# code:
var dict = new Dictionary<string, int>();
int value = dict["missingKey"];
AReturns 0 for missing key
BReturns null for missing key
CThrows KeyNotFoundException at runtime
DCompiles but returns default string
Step-by-Step Solution
Solution:
  1. Step 1: Accessing dictionary with missing key

    Using indexer with a key not present causes exception.
  2. Step 2: Exception type

    KeyNotFoundException is thrown at runtime for missing keys.
  3. Final Answer:

    Throws KeyNotFoundException at runtime -> Option C
  4. Quick Check:

    Indexer access missing key throws exception = B [OK]
Quick Trick: Indexer throws if key missing, use ContainsKey first [OK]
Common Mistakes:
MISTAKES
  • Assuming default value returned
  • Expecting null for value type int
  • Ignoring exception possibility

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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