Bird
0
0

What will be the output of this code?

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

class Program {
  static void Main() {
    Animal a = new Animal();
    Console.WriteLine(a.Name);
  }
}
ACompilation error
BAnimal
CCat
DName
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class and member initialization

    The class Animal has a public string member Name initialized to "Cat".
  2. Step 2: Understand program output

    The Main method creates an Animal object and prints a.Name, which is "Cat".
  3. Final Answer:

    Cat -> Option C
  4. Quick Check:

    Printed member value = Cat [OK]
Quick Trick: Public members can be accessed directly outside class [OK]
Common Mistakes:
MISTAKES
  • Expecting class name to print
  • Thinking member is private by default
  • Assuming compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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