Bird
0
0

What is the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What is the output of this code?
object obj = 3.14;
if (obj is int n)
{
    Console.WriteLine($"Integer: {n}");
}
else
{
    Console.WriteLine("Not an integer");
}
ANot an integer
BCompilation error
CInteger: 3.14
DInteger: 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the type of obj

    obj holds a double value 3.14, not an int.
  2. Step 2: Evaluate the if condition

    The condition obj is int n is false because obj is not an int. So, the else block runs.
  3. Final Answer:

    Not an integer -> Option A
  4. Quick Check:

    Type mismatch leads to else = D [OK]
Quick Trick: Type must match exactly for 'is' pattern to succeed [OK]
Common Mistakes:
MISTAKES
  • Assuming implicit conversion to int
  • Expecting double to match int pattern

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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