Bird
0
0

Which of the following code snippets correctly demonstrates constructor overloading in C#?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Classes and Objects
Which of the following code snippets correctly demonstrates constructor overloading in C#?
Aclass Car { public Car() {} public Car(string model) {} }
Bclass Car { public void Car() {} public void Car(string model) {} }
Cclass Car { public Car() {} public Car() {} }
Dclass Car { public Car(int year) {} public Car(int year) {} }
Step-by-Step Solution
Solution:
  1. Step 1: Identify valid constructor syntax

    Constructors must have no return type and share the class name.
  2. Step 2: Check for overloading

    class Car { public Car() {} public Car(string model) {} } has two constructors with different parameter lists, which is valid overloading.
  3. Step 3: Identify errors in other options

    class Car { public void Car() {} public void Car(string model) {} } uses 'void' return type, invalid for constructors. Options C and D have duplicate parameter lists, causing errors.
  4. Final Answer:

    class Car { public Car() {} public Car(string model) {} } is the correct constructor overloading syntax.
  5. Quick Check:

    Constructor names match class name, no return type, different parameters [OK]
Quick Trick: Constructors have no return type and differ by parameters [OK]
Common Mistakes:
MISTAKES
  • Adding a return type to constructors
  • Defining multiple constructors with identical parameters
  • Using different method names instead of class name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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