Bird
0
0

Find the error in this code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Classes and Objects
Find the error in this code:
class Animal {
  public string type;
  public Animal(string t) { type = t; }
}

Animal a = new Animal();
Console.WriteLine(a.type);
AClass Animal has no constructor.
BVariable a is not initialized.
CCannot print a.type directly.
DConstructor requires a string argument but none provided.
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters

    The constructor requires a string argument, but new Animal() provides none.
  2. Step 2: Identify the error cause

    This causes a compilation error because no matching constructor exists without parameters.
  3. Final Answer:

    Constructor requires a string argument but none provided. -> Option D
  4. Quick Check:

    Constructor parameters must match call [OK]
Quick Trick: Match constructor arguments exactly [OK]
Common Mistakes:
MISTAKES
  • Calling constructor without required arguments
  • Assuming default constructor exists
  • Thinking printing field causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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