Bird
0
0

What is the output of this code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What is the output of this code?
object obj = 42;
if (obj is int n)
{
    Console.WriteLine(n + 10);
}
else
{
    Console.WriteLine("Not an int");
}
ANot an int
B52
C42
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Check the type of obj

    obj holds the integer 42, so obj is int n is true and assigns 42 to n.
  2. Step 2: Calculate the output inside the if block

    It prints n + 10 which is 42 + 10 = 52.
  3. Final Answer:

    52 -> Option B
  4. Quick Check:

    Type check passes, output is 42 + 10 = 52 [OK]
Quick Trick: If type matches, variable holds value for use [OK]
Common Mistakes:
MISTAKES
  • Printing the original obj instead of n + 10
  • Choosing else output wrongly
  • Thinking it causes a compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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