Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Classes and Objects
What will be the output of this C# code?
class Fruit {
  public string Name = "Apple";
}

Fruit f = new Fruit();
Console.WriteLine(f.Name);
AApple
BFruit
CName
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the class

    The class Fruit has a public string field Name initialized to "Apple".
  2. Step 2: Understand object instantiation

    Creating an object f of Fruit sets f.Name to "Apple".
  3. Step 3: Output statement

    Console.WriteLine(f.Name) prints the value of Name, which is "Apple".
  4. Final Answer:

    Apple -> Option A
  5. Quick Check:

    Object field value printed correctly [OK]
Quick Trick: Object fields print their assigned values [OK]
Common Mistakes:
MISTAKES
  • Expecting class name to print
  • Confusing field name with its value
  • Assuming code causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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