Bird
0
0

What will be the output of this C# code?

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

class Program {
  static void Main() {
    Dog myDog = new Dog();
    Console.WriteLine(myDog.Name);
  }
}
ABuddy
BmyDog
CDog
DName
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and field access

    The code creates a new Dog object and accesses its Name field which is set to "Buddy".
  2. Step 2: Determine what is printed

    The Console.WriteLine prints the value of myDog.Name, which is "Buddy".
  3. Final Answer:

    Buddy -> Option A
  4. Quick Check:

    Object field value printed = Buddy [OK]
Quick Trick: Object.field prints the stored value [OK]
Common Mistakes:
MISTAKES
  • Printing the object name instead of field
  • Confusing class name with field value
  • Expecting variable name as output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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