Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Identify the error in this code snippet:
object obj = "hello";
if (obj is int number)
{
    Console.WriteLine(number);
}
ANo error, code runs and prints number
BRuntime exception thrown
CSyntax error in the if statement
DThe variable 'number' is not assigned because obj is not int
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the type check

    obj is a string, so obj is int number is false and number is not assigned.
  2. Step 2: Understand the effect on code execution

    The if block is skipped, so nothing prints. No error or exception occurs.
  3. Final Answer:

    The variable 'number' is not assigned because obj is not int -> Option D
  4. Quick Check:

    Type check false means variable not assigned [OK]
Quick Trick: If type check fails, variable is not assigned [OK]
Common Mistakes:
MISTAKES
  • Thinking it causes syntax error
  • Expecting runtime exception
  • Assuming variable is assigned anyway

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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