Tech Point Fundamentals

Sunday, June 5, 2022

C# Interview Questions and Answers - Part 03

C# Interview Questions and Answers - Part 03

csharp-interview-questions-and-answers-part1

Are you preparing for the C# Interview? If yes, then you are at the right place. This is the C# Interview Questions and Answers article series. Here we will see the top 150+ C# Interview Questions with Answers. 

Please visit our YouTube Channel for Interviews and other videos by below link:


Please read the complete Design Patterns Interview Questions and Answers series here.



Introduction


This is the 3rd part of this C# Interview Questions and Answers article series. Each part contains 10 C# Interview Questions with Answers. Please read all the C# Interview Questions list here.

I will highly recommend to please read the previous parts over here before continuing the current part:



C# Interview Questions and Answers - Part 03


Q021. What is the Fragile Base Class Problem (FBCP) of OOPS? How can you avoid this problem? 

A fragile base class is the most common problem with inheritance in OOPS. The fragile base class problem is a fundamental architectural problem of object-oriented programming systems where base classes are considered "fragile" because seemingly safe modifications to a base class, when inherited by the derived classes, may cause the derived classes to malfunction. So a base class is called fragile when changes made to it break a derived class.




Please watch the Fragile Base Class Problem video here

The Fragile Base Class Problem can be avoided by avoiding inheritance and using composition. For preventing inheritance we must have to make the class sealed. Please read more about the sealed class and the sealed method here.

Please watch how the .Net Framework avoids the Fragile Base Class Problem here and why composition is immune to fragile class problems here.








Q022. What is an abstract class? Can an abstract class implement an interface?

A class declared with the "abstract" modifier is known as an abstract class. The abstract modifier indicates that the type being modified has a missing or incomplete implementation.
The purpose of an abstract class is to provide a skeletal structure or blueprint for other classes to derive from along with the common reusable methods, properties, events, or indexers.






An abstract class may or may not have any abstract method but if a class has an abstract member, then the class must be abstract otherwise the compiler generates a compile-time error.

An abstract class can never be instantiated. It can only be used as a base class, and the inheriting class must have to implement the abstract members of the base class unless the inheriting class is also an abstract class.

Please read the complete Abstract Class article here for more details. You can also watch the abstract class video here.



An abstract class can also implement interfaces. Not even this but, abstract classes can also inherit from other abstract classes or concrete classes. 

Please read the complete Abstract Class vs Interface article here for more details. Please watch the Abstract Class vs Interface video here as well.




Q023. When do you choose interface over an abstract class or vice versa?

In C# to achieve abstraction and security, we use abstract classes. Normally we use the abstract classes when we create frameworks for the project. If you see the .Net frameworks you can find a lot of abstract classes. 

Normally we must have to declare the class as abstract if:
  • The class contains any abstract members.
  • The class does not provide implementation to any of the methods of the inherited interface.
  • The class does not provide the implementation of the base class abstract methods.

On the other hand, interfaces are used when we are defining some contract or blueprint for the external users. And there is a chance that a user may implement multiple interfaces based on the requirement because multiple interface inheritance is possible.

Please read the abstract class vs interface here for more details. You can also watch the interface vs abstract class video here



Q024. Can you define constructors in the abstract class? What is the use of a constructor in an abstract class that can not be instantiated?

Yes, an abstract class can also have constructors.  An abstract class only works as a base class in C# but still, it can define constructors. Please read about the constructors here.

An abstract class can not be instantiated but we know that an abstract class can also have concrete members which require some default values as an input parameter. 

So it will be the responsibility of the inheriting class to provide the values for the base abstract class's constructor.




Please read more about the abstract class here for more details. You can also watch the abstract class video here.



Q025. Can you create the object of the abstract class? Is it mandatory to have abstract members in the abstract class?

No, we cannot create the object of an abstract class. An abstract class only works as a base class.

It is not mandatory to have abstract members in the abstract class. But there is no means of an abstract class without any abstract member, we can use a concrete class instead. 

But if a class has an abstract member, then the class must be abstract otherwise the compiler generates a compile-time error. Please read the complete abstract class article here for more details.



Q026. What is the difference between abstract class and interface?

This is one of the favorite questions of the interviewer. On the other hand, the answer to this question is more confusing as well after the introduction of C#8. 

Some core differences are as follows but I will highly recommend you to please read the complete Interface vs Abstract Class article here.








  1. For the real abstract class, it is necessary to have at least one abstract member while an interface may not.
  2. An abstract class cannot have multiple class inheritance, but an interface can have multiple interface inheritance.
  3. An abstract class can have a constructor or destructor while an interface cannot.
  4. An abstract class can contain instance fields and constants, but the interface cannot.
  5. An abstract keyword is used to declare an abstract class while an interface keyword is used to define an interface.
  6. An abstract class cannot be inherited by structures while an interface can be inherited by structures.
  7. An abstract class can inherit from both class and interfaces while an interface can only be inherited from an interface.
  8. The virtual interface members (concrete methods) are not inherited in the derived class but all the concrete members of an abstract class are inherited.
  9. The inheriting class must have to override all the abstract members of the base abstract class with an explicit override keyword, while interface virtual members can only be overridden explicitly without the override keyword.
  10. You can override any abstract member of the abstract class as sealed in the inheriting class while you cannot in the case of the interface in C# 8.

Please also watch the Abstract Class vs Interface video here for more details.



Q027. Does C# support multiple inheritances? Why?

The answer to this question is both yes and no. C# does not support multiple class inheritance due to the diamond problem. But C# supports multiple interface inheritance.

But the question of the diamond problem came once again for multiple interface inheritance in C#8 as well because C# 8 supports default methods. Well, the diamond problem is resolved in C# for multiple interface inheritance by the most specific override rule.




Please read the Multiple Inheritance article here for more details. Please watch the multiple inheritance and diamond problem here.



Q028. What is the Diamond Problem in OOPS?

The diamond problem is an ambiguity that can arise because of allowing multiple inheritances. It is a big problem for languages that allow multiple inheritances of states like C++. Multiple inheritances are not allowed for classes in C#, however, it is allowed for interfaces in a limited way so that it does not contain any state (instance field).

C# does not support multiple class inheritance because it adds too much complexity to C#, one of them is the diamond problem. It will create ambiguity for CLR which one has to call if there is any method with the same name in more than one base class.

Please read the Diamond Problem here for more details. 



Q029. What is the difference between class inheritance and interface inheritance in C#?

The main difference between class and interface inheritance is as follows:

  1. A class cannot inherit from multiple classes, but a class can inherit from multiple interfaces.
  2. A class can inherit from both class and interface at the same time but an interface can only inherit from other interfaces.
  3. A derived class can override the base class method but the interface requires explicit override. Please read more here.
  4. A class can have instance members for reuse in the derived class but an interface cannot define any instance member.
  5. In C#8, an interface can also have reusable default methods. A base class can have constructors, so the derived class can pass the required parameter for the base class, but we can not pass that for interface inheritance because they do not have a constructor.

Please read the complete article here for more details about interface inheritance.




Q030. What is the difference between multiple and multilevel inheritances?

In multiple inheritances, there are multiple base classes or interfaces of the derived classes. For example Interface A -> Class AB <- Interface B. 

Multiple class inheritance is not allowed in C#, but multiple interface inheritance is allowed. Please read more about the multiple inheritances here.

On the other hand in multilevel inheritance, there is a chain of inheritance. For example Class A -> Class B -> Class C.

So in the Multilevel inheritance, a derived class will inherit a base class and the derived class also act as the base class to other class. Multilevel inheritance is allowed in interfaces as well.


To Be Continued Part-04...


Recommended Articles






Thanks for visiting this page. Please follow and join us on LinkedIn, Facebook, Telegram, Quora, YouTube, Twitter, Pinterest, Tumbler, and VK  for regular updates.


No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.