C# Inheritance MCQ

1. What is inheritance in C#?

a) The process of creating new classes from existing ones
b) Copying the same method into multiple classes
c) Using the same variables in different classes
d) Connecting classes through interfaces

Answer:

a) The process of creating new classes from existing ones

Explanation:

Inheritance in C# is the process of deriving new classes from existing ones, allowing the new classes to adopt and possibly extend the properties and methods of the base class.

2. What keyword is used to inherit from a base class in C#?

a) extends
b) super
c) base
d) :

Answer:

d) :

Explanation:

In C#, the colon (:) is used in the class declaration to inherit from a base class.

3. Which class in C# is the base class for all other classes?

a) Object
b) Base
c) Root
d) Main

Answer:

a) Object

Explanation:

In C#, the 'Object' class is the ultimate base class from which all other classes implicitly inherit.

4. Can a derived class override a non-virtual method of the base class in C#?

a) Yes, always
b) No, only virtual methods can be overridden
c) Yes, but only if the method is static
d) No, unless the method is abstract

Answer:

b) No, only virtual methods can be overridden

Explanation:

In C#, a derived class can only override methods in the base class that are marked as virtual, abstract, or override.

5. What is the correct way to call a constructor of the base class in C#?

a) super()
b) base()
c) :base()
d) parent()

Answer:

c) :base()

Explanation:

In C#, :base() is used within a derived class constructor to call a specific constructor of the base class.

6. What is the default access modifier for base class inheritance in C#?

a) public
b) private
c) protected
d) internal

Answer:

a) public

Explanation:

If no access modifier is specified for inheritance, the default is 'public' in C#.

7. Can structures (structs) in C# inherit from other structures or classes?

a) Yes, from both structures and classes
b) No, structures cannot inherit from any type
c) Yes, but only from other structures
d) Yes, but only from classes

Answer:

b) No, structures cannot inherit from any type

Explanation:

In C#, structures (structs) cannot inherit from other structures or classes, but they can implement interfaces.

8. What is the purpose of the 'sealed' keyword in relation to inheritance in C#?

a) To prevent a class from inheriting from other classes
b) To allow a class to be inheritable
c) To prevent a class from being a base class
d) To seal methods so they cannot be overridden

Answer:

c) To prevent a class from being a base class

Explanation:

The 'sealed' keyword in C# is used to prevent a class from being used as a base class, effectively stopping inheritance from that class.

9. What is multiple inheritance in the context of C#?

a) Inheriting from multiple base classes
b) Inheriting both methods and properties
c) A class inheriting from another class and multiple interfaces
d) A concept not supported in C#

Answer:

d) A concept not supported in C#

Explanation:

Multiple inheritance, or inheriting from more than one base class, is not supported in C#. However, a class can implement multiple interfaces.

10. Can a derived class in C# access private members of its base class?

a) Yes, always
b) No, private members are accessible only within the class they are declared
c) Yes, but only through public or protected methods
d) No, unless the class is sealed

Answer:

b) No, private members are accessible only within the class they are declared

Explanation:

Private members of a base class are not accessible to derived classes. Derived classes can only access public, protected, and internal members of the base class.

Leave a Comment

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

Scroll to Top