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.
Which keyword is used in C# to indicate that a class derives from another class?
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.
How does a class in C# declare that it implements an interface named 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.
What is the correct way to allow a method to be overridden in a C# derived class?
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.
Which statement about members inside a C# interface is true?
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.
How can a C# class inherit features from multiple sources?
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.
Given 'Animal animal = new Dog();', what concept is illustrated by assigning a Dog to an Animal variable?
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.
Which C# keyword is necessary to define a class that cannot be instantiated and must be inherited?
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.
Which feature can abstract classes have in C# that interfaces cannot?
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.
As of recent C# versions, what is a new feature of interfaces related to member implementation?
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.
What is the effect of marking a C# class as 'sealed'?
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.