Polymorphism Questions with Answers

9 practice questions on Polymorphism (C#) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following are necessary for Run-time Polymorphism?
I. The overridden base method must be virtual, abstract or override.
II. Both the override method and the virtual method must have the same access level modifier.
III. An override declaration can change the accessibility of the virtual method.
IV. An abstract inherited property cannot be overridden in a derived class.
V. An abstract method is implicitly a virtual method.

  1. 1, 3
  2. 1, 2, 5
  3. 2, 3, 4
  4. 4 only

Answer: B. 1, 2, 5

Explanation

Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. A derived class can override a base class member only if the base class member is declared as virtual orabstract. The derived member must use the override keyword to explicitly indicate that the method is intended to participate in virtual invocation.

Step-by-step solution

  1. I is true: the overridden base method must be virtual, abstract or override.
  2. II is true: override and virtual methods must have the same access level.
  3. III is false: an override cannot change the accessibility of the virtual method. IV is false: abstract members must be overridden.
  4. V is true: an abstract method is implicitly virtual. So I, II and V are correct, which is option B.

Why the other options are wrong

  • A. It leaves out II and V and includes III, which is false.
  • C. It includes III and IV, which are both false.
  • D. IV is false: an abstract member must be overridden in a derived class.

Tip: Override rules: the base must be virtual, abstract or override, the access level must match, and abstract methods are virtual.

Question 2Medium

Which of the following keyword is used to overload user-defined types by defining static member functions?

  1. Op
  2. Opoverload
  3. Operator
  4. Operatoroverload

Answer: C. Operator

Explanation

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. Not all operators can be overloaded, however, and others have restrictions.

Step-by-step solution

  1. Operator overloading is done using the 'operator' keyword in C#.
  2. 'Operator' is used to define static member functions for user-defined types.
  3. Static member functions are required for operator overloading in C#.
  4. The correct keyword is 'Operator' as per C# syntax.
  5. Final answer: Operator.

Why the other options are wrong

  • A. Op is not a valid keyword for operator overloading in C#.
  • B. Opoverload is not a recognized keyword in C# syntax.
  • D. Operatoroverload is not the correct keyword for defining overloaded operators.

Tip: Remember, 'operator' is the only valid keyword for overloading in C#.

Question 3Medium

Which of the followings is the correct way to overload + operator?

  1. public sample operator + ( sample a, sample b )
  2. public abstract operator + ( sample a, sample b)
  3. public abstract sample operator + (sample a, sample b )
  4. public static sample operator + ( sample a, sample b )

Answer: D. public static sample operator + ( sample a, sample b )

Explanation

The correct way to overload the + operator is with 'public static sample operator + (sample a, sample b)'. This makes the method accessible and ensures it can be called without creating an instance of the class.

Step-by-step solution

  1. Operator overloading requires the method to be public for access.
  2. The operator must be static as it operates on types, not instances.
  3. The return type should match the type being operated on, which is 'sample' here.
  4. Parameters are of type 'sample', so they are passed correctly.
  5. Combining all elements gives 'public static sample operator + (sample a, sample b)'.

Why the other options are wrong

  • A. Missing 'static' keyword makes it instance method, not overloadable.
  • B. Abstract methods cannot be overloaded directly with operators.
  • C. Abstract keyword is unnecessary for operator overloading.

Tip: Use 'public static' for operator overloading to ensure it's accessible and can be called without an instance.

Question 4Medium

A derived class can stop virtual inheritance by declaring an override as

  1. Inherits
  2. Extends
  3. Inheritable
  4. sealed

Answer: D. sealed

Explanation

Sealed keyword always come before override keyword and because override keyword is implicitly virtual and to make that method should not be overridden by inherited class we use sealed keyword.
So that the class methods can't be inherited by any derived class.

Step-by-step solution

  1. Sealed stops a method from being overridden in derived classes.
  2. It is used with override to prevent further inheritance of virtual methods.
  3. Sealed ensures the method remains fixed in its current class.
  4. This makes the method non-virtual and stops virtual inheritance.
  5. Thus, sealed is the correct keyword for stopping virtual inheritance.

Why the other options are wrong

  • A. Inherits does not stop virtual inheritance.
  • B. Extends does not prevent method overriding.
  • C. Inheritable allows methods to be inherited.

Tip: Use sealed with override to stop virtual inheritance.

Question 5Medium

Which of the following statements is correct?

  1. Static methods can be a virtual method.
  2. Abstract methods can be a virtual method.
  3. It is necessary to override a virtual method.
  4. When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overriden.

Answer: D. When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overriden.

Explanation

Abstract methods can be virtual. The Following code is An abstract method that is virtual. Also [D] is correct

abstract class Journal
{
abstract virtual public void Issue()
{
// Implementation code
}
}

Step-by-step solution

  1. An override must match the virtual method's name and signature.
  2. Static methods belong to the class, so they cannot be virtual.
  3. Overriding a virtual method is optional.
  4. So only option D is correct.

Why the other options are wrong

  • A. Static methods cannot be virtual.
  • B. Abstract methods are not declared with virtual in C#.
  • C. A virtual method may be overridden but need not be.

Tip: Override must match the base method's name, return type and parameters.

Question 6Medium

Which of the following unary operators can be overloaded?
I. True.
II. False
III.+
IV. New
V. Is.

  1. I,II,III.
  2. III,IV,V.
  3. III only
  4. V only

Answer: A. I,II,III.

Explanation

true and false are overloadable unary operators, and so is the unary + operator, so I, II and III are correct. new and is are keywords, not operators, so they cannot be overloaded.

Step-by-step solution

  1. The C# overloadable unary operators include true, false and +.
  2. New and is are keywords, not operators, so they cannot be overloaded.
  3. So I, II and III can be overloaded, and IV and V cannot.
  4. The answer is option A, I, II, III.

Why the other options are wrong

  • B. IV (new) and V (is) are keywords, not operators, so they cannot be overloaded.
  • C. I and II (true and false) can be overloaded too, so III only leaves out correct items.
  • D. V (is) is a keyword, and I and II (true and false) can be overloaded.

Tip: Overloadable unary operators are + - ! ~ ++ -- true and false; keywords such as new and is are not operators.

Question 7Medium

Can you access a hidden base class method in the derived class?

  1. Yes.
  2. No.

Answer: A. Yes.

Explanation

Yes. A derived class can hide a base method with new, but the base method can still be called by casting the derived object to the base class, as in ((BaseClass)DC).Method();. This prints I am a base class method.

Step-by-step solution

  1. The derived class hides Method with the new keyword.
  2. Casting the derived object to BaseClass lets you call the base version of the method.
  3. The example prints 'I am a base class method.'
  4. So the correct answer, Yes, is correct.

Why the other options are wrong

  • B. A hidden base method can be reached by casting the object to the base type.

Tip: Cast to the base type to call a method that the derived type hides.

Question 8Medium

The various techniques provided by C# to implement static polymorphism is/are

- i) Function overloading
- ii) Static overloading
- iii) Operator overloading

  1. i and ii only
  2. i and iii only
  3. ii and iii only
  4. All i, ii, iii

Answer: B. i and iii only

Explanation

The mechanism of linking a function withan object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism.
These are:Function overloading, Operator overloading.

Step-by-step solution

  1. Static polymorphism in C# is done with function overloading (i) and operator overloading (iii).
  2. Static overloading (ii) is not a separate technique.
  3. So the correct answer, i and iii only, is correct.

Why the other options are wrong

  • A. Static overloading (ii) is not a technique, so i and ii is wrong.
  • C. Static overloading (ii) is not a technique, so ii and iii is wrong.
  • D. Static overloading (ii) is not a technique, so all three is wrong.

Tip: Static (compile-time) polymorphism means overloading functions and operators.

Question 9Medium

What is printed?

class A { public virtual void Show() { Console.Write("A"); } }
class B : A { public override void Show() { Console.Write("B"); } }

A a = new B();
a.Show();
  1. A
  2. AB
  3. Compile-time error
  4. B

Answer: D. B

Explanation

A virtual method is dispatched on the actual object type, which is B, so the override runs.

Step-by-step solution

  1. Reference type is A.
  2. Object type is B.
  3. Virtual call picks B.Show.

Why the other options are wrong

  • A. The virtual call uses the runtime type, not the declared type.
  • B. Only the override runs; the base version is not called automatically.
  • C. Assigning a derived object to a base reference is allowed.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics