Bird
0
0

You want to declare an interface IVehicle with two methods: Start() and Stop(). Which of the following is the correct way to declare it and implement it in a class Car?

hard🚀 Application Q15 of 15
C Sharp (C#) - Interfaces
You want to declare an interface IVehicle with two methods: Start() and Stop(). Which of the following is the correct way to declare it and implement it in a class Car?
Ainterface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } }
Binterface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { }
Cinterface IVehicle { void Start(); void Stop(); } class Car : IVehicle { public void Start() { Console.WriteLine("Car started"); } public void Stop() { Console.WriteLine("Car stopped"); } }
Dinterface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } }
Step-by-Step Solution
Solution:
  1. Step 1: Declare interface with method signatures only

    IVehicle must declare Start() and Stop() without bodies.
  2. Step 2: Implement interface methods publicly in class

    Car must implement both methods with public access and provide method bodies.
  3. Step 3: Check other options for errors

    interface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { } has method bodies in interface (invalid). interface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } } does not implement interface. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } } implements methods but lacks public modifier, causing error.
  4. Final Answer:

    Correct interface and class implementation with public methods -> Option C
  5. Quick Check:

    Interface methods declared; class implements publicly [OK]
Quick Trick: Interface methods need public implementation in classes [OK]
Common Mistakes:
MISTAKES
  • Adding method bodies inside interface
  • Not implementing interface in class
  • Omitting public modifier in class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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