Bird
0
0

What will be the output of the following code?

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

class Program {
  static void Main() {
    Dog d = new Dog();
    System.Console.WriteLine(d.Name);
  }
}
ABuddy
BName
CDog
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand class field initialization

    The class Dog has a public field Name initialized to "Buddy".
  2. Step 2: Trace the program output

    In Main, a Dog object is created and d.Name is printed, so output is "Buddy".
  3. Final Answer:

    Buddy -> Option A
  4. Quick Check:

    Field value printed = Buddy [OK]
Quick Trick: Prints field value assigned in class [OK]
Common Mistakes:
MISTAKES
  • Confusing class name with field value
  • Expecting method output instead of field
  • Assuming compilation error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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