Bird
0
0

Which of the following is the correct way to override a method named Calculate in a derived class in C#?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Which of the following is the correct way to override a method named Calculate in a derived class in C#?
Apublic int Calculate() { return 0; }
Bpublic new int Calculate() { return 0; }
Cpublic virtual int Calculate() { return 0; }
Dpublic override int Calculate() { return 0; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify override syntax

    To override a base class virtual method, the derived class method must use the override keyword.
  2. Step 2: Analyze options

    public override int Calculate() { return 0; } correctly uses override. public new int Calculate() { return 0; } uses new which hides the base method but does not override. public virtual int Calculate() { return 0; } declares a new virtual method, not an override. public int Calculate() { return 0; } is a normal method without override.
  3. Final Answer:

    public override int Calculate() { return 0; } -> Option D
  4. Quick Check:

    Override requires 'override' keyword [OK]
Quick Trick: Use 'override' keyword to override methods [OK]
Common Mistakes:
MISTAKES
  • Using 'new' instead of 'override'
  • Omitting 'override' keyword
  • Declaring method as virtual instead of override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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