Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Classes and Objects
Identify the error in this code snippet:
class Car {
  public string model;
  public Car(string m) { model = m; }
}

Car c = new Car;
AClass Car has no constructor defined.
BMissing parentheses after Car in object creation.
Cmodel field is not initialized.
DCannot assign new Car to variable c.
Step-by-Step Solution
Solution:
  1. Step 1: Check object instantiation syntax

    In C#, when creating a new object, parentheses must follow the class name even if no arguments are passed.
  2. Step 2: Analyze the code snippet

    The code uses new Car; without parentheses, which causes a syntax error.
  3. Final Answer:

    Missing parentheses after Car in object creation. -> Option B
  4. Quick Check:

    new requires parentheses () [OK]
Quick Trick: Always add () after new ClassName [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses after new keyword
  • Assuming default constructor exists without parentheses
  • Ignoring compiler error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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