Bird
0
0

What will be printed by the following code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What will be printed by the following code?
object data = "world";
string text = data as string;
Console.WriteLine(text != null ? text.ToUpper() : "empty");
Aworld
BWORLD
Cempty
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand the as operator

    The as operator attempts to cast data to string. Since data is a string, text will hold "world".
  2. Step 2: Evaluate the conditional expression

    The condition text != null is true, so text.ToUpper() is executed, printing "WORLD".
  3. Final Answer:

    WORLD -> Option B
  4. Quick Check:

    Successful cast with 'as' returns original string [OK]
Quick Trick: If 'as' succeeds, result is non-null [OK]
Common Mistakes:
MISTAKES
  • Assuming 'as' returns null even if cast succeeds
  • Not calling ToUpper() on the string
  • Confusing output with original casing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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