C# Polymorphism MCQ

1. What is polymorphism in C#?

a) A way to process different data types with a single interface
b) The ability of different classes to provide unique implementations of the same method
c) A feature that allows a class to have multiple constructors
d) The ability to change a variable's type at runtime

Answer:

b) The ability of different classes to provide unique implementations of the same method

Explanation:

Polymorphism in C# allows methods with the same name to behave differently in different classes, usually through method overriding.

2. Which keyword is used for method overriding in C#?

a) virtual
b) override
c) new
d) base

Answer:

b) override

Explanation:

The 'override' keyword is used in a derived class to override a method defined in the base class.

3. What is method overloading in C#?

a) Creating methods in a derived class with the same name as the base class
b) Defining multiple methods with the same name but different parameters within the same class
c) Changing the return type of a method
d) Calling methods from the base class

Answer:

b) Defining multiple methods with the same name but different parameters within the same class

Explanation:

Method overloading involves creating multiple methods in the same class with the same name but different parameter lists.

4. What is required to make a method overridable in a base class in C#?

a) The method must be static
b) The method must be private
c) The method must be declared with the 'virtual' keyword
d) The method must be sealed

Answer:

c) The method must be declared with the 'virtual' keyword

Explanation:

To allow a method to be overridden in derived classes, it must be declared with the 'virtual' keyword in the base class.

5. How can you hide a method in a base class without overriding it in C#?

a) Using the 'new' keyword
b) Using the 'hide' keyword
c) By making the method private
d) By making the method static

Answer:

a) Using the 'new' keyword

Explanation:

The 'new' keyword can be used in a derived class to hide a non-virtual method of the base class.

6. What is dynamic polymorphism in C#?

a) Polymorphism determined at compile time
b) Polymorphism that occurs when code is executed
c) Polymorphism achieved through method overloading
d) Polymorphism achieved with dynamic types

Answer:

b) Polymorphism that occurs when code is executed

Explanation:

Dynamic polymorphism is determined at runtime, typically through method overriding, allowing objects to be treated as instances of their base type.

7. What happens when a virtual method is called on a reference whose compile-time type is different from its runtime type?

a) The method of the compile-time type is executed
b) The method of the runtime type is executed
c) A runtime error occurs
d) The method call is ignored

Answer:

b) The method of the runtime type is executed

Explanation:

When a virtual method is called, the version of the method that corresponds to the runtime type of the object is executed.

8. Can interfaces be used to achieve polymorphism in C#?

a) No, interfaces cannot be used for polymorphism
b) Yes, by allowing different classes to implement the same interface differently
c) Only if the interface methods are static
d) Only within the same assembly

Answer:

b) Yes, by allowing different classes to implement the same interface differently

Explanation:

Interfaces provide a way to achieve polymorphism in C# by defining a contract that can be implemented differently by multiple classes.

9. What is the difference between static and dynamic polymorphism?

a) Static polymorphism is compile-time, while dynamic polymorphism is runtime
b) Static polymorphism uses virtual methods, while dynamic polymorphism uses new methods
c) There is no difference
d) Static polymorphism uses inheritance, while dynamic polymorphism uses interfaces

Answer:

a) Static polymorphism is compile-time, while dynamic polymorphism is runtime

Explanation:

Static polymorphism (method overloading) is resolved at compile time, while dynamic polymorphism (method overriding) is resolved at runtime.

10. In C#, how do you prevent a method from being overridden in a derived class?

a) By declaring the method as private
b) By using the 'final' keyword
c) By using the 'sealed' keyword on the method
d) By omitting the 'virtual' keyword

Answer:

c) By using the 'sealed' keyword on the method

Explanation:

The 'sealed' keyword is used on a method in a derived class to prevent further overriding of that method in any subsequent derived classes.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top