MCQs on Advanced Object-Oriented Programming | C#

Chapter 8 dives deep into advanced object-oriented programming (OOP) concepts in C#. It covers inheritance, method overriding, abstract classes, interfaces, and the differences between abstract classes and interfaces in C#. These concepts are vital for building robust applications.


Multiple Choice Questions (MCQs)

Section 1: Inheritance and Base/Derived Classes

  1. What is the correct syntax to declare a derived class in C#?
    a) class Derived : Base {}
    b) class Derived inherits Base {}
    c) class Derived(Base) {}
    d) class Derived : BaseClass {}
  2. Which keyword is used to inherit from a base class in C#?
    a) extends
    b) super
    c) inherits
    d) :
  3. In C#, can a derived class access the private members of a base class?
    a) Yes
    b) No
    c) Only static members
    d) Only public members
  4. What is the default access modifier for members in a base class if no access modifier is specified?
    a) public
    b) private
    c) protected
    d) internal
  5. How can you call a method from a base class in a derived class in C#?
    a) base.MethodName();
    b) this.MethodName();
    c) super.MethodName();
    d) base.ClassName.MethodName();
  6. Which of the following is true about inheritance in C#?
    a) A class can inherit from multiple base classes.
    b) C# does not support inheritance.
    c) A derived class can inherit from one base class.
    d) All classes in C# are derived from a single base class.

Section 2: Method Overriding and Polymorphism

  1. What is method overriding in C#?
    a) Defining a new method in the derived class
    b) Re-defining a method in the base class
    c) Changing the signature of a method
    d) Providing a new implementation for an inherited method in the derived class
  2. Which keyword is used to override a method in C#?
    a) virtual
    b) override
    c) new
    d) base
  3. What does polymorphism mean in C#?
    a) A derived class can inherit multiple base classes
    b) A class can have multiple methods with the same name
    c) Objects of different classes can be treated as objects of a common base class
    d) Objects of the same class can have different data types
  4. What is the result of method overriding in C#?
    a) A new method is created in the derived class.
    b) The base class method is hidden.
    c) The base class method is called instead of the derived class method.
    d) The derived class method is called instead of the base class method at runtime.
  5. Which of the following is a requirement for method overriding in C#?
    a) The method in the base class must be marked as static.
    b) The method in the derived class must have the same return type.
    c) The method in the base class must be marked as private.
    d) The method in the base class must be marked as virtual or abstract.
  6. What will happen if a derived class does not override a virtual method?
    a) The program will throw an error.
    b) The base class implementation will be used.
    c) The method will be unavailable to the derived class.
    d) The derived class will implement its own version automatically.

Section 3: Abstract Classes and Interfaces

  1. What is the main purpose of an abstract class in C#?
    a) To prevent any method from being inherited
    b) To define a class that cannot be instantiated directly
    c) To allow for only concrete methods
    d) To implement interfaces
  2. Which of the following cannot be defined in an abstract class in C#?
    a) Abstract methods
    b) Properties
    c) Static methods
    d) Private methods
  3. What is an interface in C#?
    a) A class with no methods
    b) A type that defines method signatures without implementation
    c) A class that cannot be inherited
    d) A class with only abstract methods
  4. Can a class implement multiple interfaces in C#?
    a) Yes
    b) No
    c) Only if the interfaces are of the same type
    d) Yes, but only one interface can have a default implementation
  5. Which of the following is true about abstract classes and interfaces in C#?
    a) An abstract class can implement multiple interfaces.
    b) An interface can inherit from an abstract class.
    c) A class can implement multiple abstract classes.
    d) Both abstract classes and interfaces cannot contain any methods.
  6. What will happen if a class does not implement all methods from an interface?
    a) The class will be abstract.
    b) The compiler will throw an error.
    c) The interface methods will not be accessible.
    d) The class will implement the methods automatically.
  7. Can an abstract class have constructors in C#?
    a) Yes
    b) No
    c) Only if it implements an interface
    d) Only in derived classes
  8. What is the correct syntax for defining an interface method in C#?
    a) public void MethodName();
    b) public abstract void MethodName();
    c) void MethodName();
    d) public virtual void MethodName();

Section 4: Interfaces vs Abstract Classes

  1. What is the primary difference between an abstract class and an interface in C#?
    a) Abstract classes cannot have fields, but interfaces can.
    b) An interface can contain method implementations, but an abstract class cannot.
    c) A class can implement multiple interfaces, but can only inherit from one abstract class.
    d) Abstract classes are faster than interfaces.
  2. Which of the following can be declared in an interface?
    a) Properties
    b) Static members
    c) Fields
    d) Methods with implementation
  3. Which keyword is used to implement an interface in C#?
    a) abstract
    b) interface
    c) implements
    d) base
  4. Can an interface in C# contain method bodies?
    a) Yes, starting from C# 8.0
    b) No
    c) Yes, but only for virtual methods
    d) Yes, only for static methods
  5. Can an abstract class implement an interface in C#?
    a) Yes
    b) No
    c) Only in specific circumstances
    d) Yes, but only if the class is sealed
  6. When should you use an abstract class over an interface in C#?
    a) When you need to provide method implementations
    b) When the class is supposed to be instantiated
    c) When you want to allow multiple inheritance
    d) When the class is meant to be used as a data type

Section 5: Virtual and Override Methods

  1. What is the difference between a virtual method and an override method in C#?
    a) virtual is used to override a method, while override defines a method.
    b) virtual is used to define a method in a base class that can be overridden, while override is used to modify it in a derived class.
    c) override can only be used in interfaces, while virtual is used in abstract classes.
    d) virtual defines a method that cannot be overridden, while override is used to provide a new implementation.
  2. Which of the following is a valid use of the virtual keyword in C#?
    a) To create a method in a base class that can be overridden in a derived class
    b) To create a method that will always be overridden
    c) To declare a method that cannot be overridden
    d) To implement an interface method
  3. Can a sealed class override a virtual method in C#?
    a) Yes, as long as it is the first time the method is overridden.
    b) No, because sealed classes cannot be inherited.
    c) Yes, because sealed methods cannot be further overridden.
    d) No, because sealed classes prevent any method overriding.
  4. What will happen if you attempt to override a method that is not marked as virtual or abstract in C#?
    a) A compiler error will occur.
    b) The program will throw a runtime exception.
    c) The method will be overridden implicitly.
    d) The base method will be executed.

Answer Key

QnoAnswer
1a) class Derived : Base {}
2d) :
3b) No
4b) private
5a) base.MethodName();
6c) A derived class can inherit from one base class.
7d) Providing a new implementation for an inherited method in the derived class
8b) override
9c) Objects of different classes can be treated as objects of a common base class
10d) The derived class method is called instead of the base class method at runtime
11d) The method in the base class must be marked as virtual or abstract
12b) The base class method is used.
13b) To define a class that cannot be instantiated directly
14d) Private methods
15b) A type that defines method signatures without implementation
16a) Yes
17a) An abstract class can implement multiple interfaces.
18b) The compiler will throw an error.
19a) Yes
20c) void MethodName();
21c) A class can implement multiple interfaces, but can only inherit from one abstract class.
22a) Properties
23c) implements
24a) Yes, starting from C# 8.0
25a) Yes
26a) When you need to provide method implementations
27b) virtual is used to define a method in a base class that can be overridden, while override is used to modify it in a derived class.
28a) To create a method in a base class that can be overridden in a derived class
29b) No, because sealed classes cannot be inherited.
30a) A compiler error will occur.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top