C# Inheritance, Interfaces, and Polymorphism Fundamentals Quiz Quiz

Challenge your understanding of inheritance, interfaces, and polymorphism in C#. This quiz covers basic principles, keyword usage, and practical scenarios to enhance foundational knowledge for C# developers.

  1. Basic Inheritance Syntax

    Which keyword is used in C# to indicate that a class derives from another class?

    1. extends
    2. :
    3. implements
    4. inherits

    Explanation: In C#, the colon (:) is used after the class name to indicate inheritance from a base class. 'Extends' and 'inherits' are keywords from other languages, not C#. 'Implements' is used for interfaces, not for classes. Therefore, only the colon is correct for class inheritance.

  2. Interface Implementation

    How does a class in C# declare that it implements an interface named IDrawable?

    1. class Shape implements IDrawable
    2. class Shape inherits IDrawable
    3. class Shape extends IDrawable
    4. class Shape : IDrawable

    Explanation: A class implements an interface in C# by using the colon syntax followed by the interface name, just as in 'class Shape : IDrawable'. 'Implements', 'inherits', and 'extends' are not valid C# keywords or syntax for this context, making only the first option correct.

  3. Virtual Methods

    What is the correct way to allow a method to be overridden in a C# derived class?

    1. Mark it as virtual
    2. Mark it as override
    3. Mark it as static
    4. Mark it as abstract

    Explanation: To permit a method to be overridden in C#, the base class method must be marked as 'virtual'. 'Override' is used when overriding the method in a derived class. 'Abstract' is used for methods without implementation, and 'static' methods cannot be overridden, so those options are incorrect.

  4. Interface Members

    Which statement about members inside a C# interface is true?

    1. They are always public and abstract by default.
    2. They can be private fields.
    3. They must be implemented immediately.
    4. They can have access modifiers.

    Explanation: Interface members in C# are implicitly public and abstract, and do not include any implementation. Access modifiers are not allowed within interfaces, implementation is provided by classes, and private fields are not supported in interfaces, so only the correct answer fits.

  5. Multiple Inheritance

    How can a C# class inherit features from multiple sources?

    1. By using the 'multiple' keyword
    2. By implementing multiple interfaces
    3. By using inherited static methods
    4. By inheriting from several base classes

    Explanation: C# does not support multiple inheritance with classes, but does allow a class to implement multiple interfaces. There is no 'multiple' keyword, and static methods are not related to inheritance for multiple sources. Only implementing multiple interfaces is valid.

  6. Polymorphism Scenario

    Given 'Animal animal = new Dog();', what concept is illustrated by assigning a Dog to an Animal variable?

    1. Encapsulation
    2. Polymorphism
    3. Method overloading
    4. Structure inheritance

    Explanation: Assigning an object of a derived type (Dog) to a base class reference (Animal) is an example of polymorphism. Method overloading involves methods with the same name and different parameters, while encapsulation refers to hiding details, and structure inheritance is not a C# feature.

  7. Abstract Classes

    Which C# keyword is necessary to define a class that cannot be instantiated and must be inherited?

    1. override
    2. public
    3. virtual
    4. abstract

    Explanation: The 'abstract' keyword is used to declare a class that cannot be instantiated directly and must be inherited. 'Public' only affects accessibility, 'override' is used for methods, and 'virtual' allows for overriding but does not make a class abstract, so those are incorrect here.

  8. Interface vs. Abstract Class

    Which feature can abstract classes have in C# that interfaces cannot?

    1. Event members
    2. Private fields
    3. Multiple inheritance
    4. Static constructors

    Explanation: Abstract classes can contain private fields, whereas interfaces cannot have any fields. C# does not support multiple class inheritance. Both abstract classes and interfaces can define events, and static constructors are only allowed in classes, not interfaces.

  9. Default Interface Implementation

    As of recent C# versions, what is a new feature of interfaces related to member implementation?

    1. Interfaces can inherit from classes.
    2. Interfaces can define constructors.
    3. Interfaces can provide default implementations for methods.
    4. Interfaces can have static fields.

    Explanation: Recent C# versions allow interfaces to include default implementations for methods, providing more flexibility. Interfaces still cannot have constructors or static fields, and they cannot inherit from classes, making the other options invalid.

  10. Sealed Keyword

    What is the effect of marking a C# class as 'sealed'?

    1. It cannot have fields.
    2. It cannot implement interfaces.
    3. It cannot be inherited by other classes.
    4. It can only inherit from abstract classes.

    Explanation: Marking a class as 'sealed' prevents it from being a base class, so no other class can inherit from it. Sealed classes can still implement interfaces, have fields, and inherit from any class (including abstract ones), thus the other options are incorrect.