Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Classes and Objects
Identify the error in this C# code snippet:
class Car {
  public string Model;
  public Car() {
    Model = "Tesla";
  }
  public Car(string model) {
    Model = model;
  }
  public void Car(string model) {
    Model = model;
  }
}
AThe class cannot have more than one constructor with parameters.
BConstructors cannot be overloaded in C#.
CThe first constructor is missing a return type.
DThe method named Car has a return type void, so it is not a constructor.
Step-by-Step Solution
Solution:
  1. Step 1: Check method named Car with void return

    Constructors have no return type; this is a method, not a constructor.
  2. Step 2: Understand difference between constructor and method

    Method named like class but with return type is not a constructor, may confuse users.
  3. Final Answer:

    The method named Car has a return type void, so it is not a constructor. -> Option D
  4. Quick Check:

    Constructor vs method return type = C [OK]
Quick Trick: Constructor has no return type; method with void is not constructor. [OK]
Common Mistakes:
MISTAKES
  • Confusing methods named like class with constructors
  • Thinking constructors can have return types
  • Believing constructors cannot be overloaded

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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