C# Interview questions

I already faced the following questions in my previous interview:
  • Asked question on Partial Class
  • Asked question on Queue 
  • Asked question on Liked List
  • Asked question on OOPS -Tell me different types of OOPs. || Inheritance || Method Hidding || Run time Polymorphism ||  Explain and differentiate between method overloading and method overriding. ||
Important Questions and Answers




*Class: A class is a blueprint/template from which you can create an individual object. 
Class doesn't consume any space.

*Object: Any entity that has a state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
-->It is an instance of a class
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

*Access Modifiers  :

*Abstraction: Hiding internal details and showing functionality is known as abstraction. 
For example phone call, we don't know the internal processing.
we use abstract class and interface to achieve abstraction.

*Polymorphism: If one task is performed in different ways, it is known as polymorphism.
we use method overloading and method overriding to achieve polymorphism.
Technically we can say that when a method/fuction shows different behaviors when we passed different types and number values, then it is called polymorphism

The polymorphism in C# can be implemented using the following three ways.

  1. Overloading
  2. Overriding
  3. Hiding
*Inheritance: When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Inheritance is classified into 5 types. They are as follows.

  1. Single Inheritance
  2. Hierarchical Inheritance
  3. Multilevel Inheritance
  4. Multiple Inheritance -- Not Supported. Only can achieve by Interface.

Why multiple inheritance is not supported in .NET

To reduce the complexity and simplify the language, multiple inheritance is not supported in .net.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.
Encapsulation: Encapsulation is a process of binding code and data together into a single unit.
-->We can create a fully encapsulated class by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
-->It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
-->It is a way to achieve data hiding in Java because other classes will not be able to access the data through the private data members.

In C# encapsulation is implemented

  1. By declaring the variables as private (to restrict its direct access from outside the class)
  2. By defining one pair of public setter and getter methods or properties to access private variables.
Interface:
The interface is a fully un-implemented class used for declaring a set of operations of an object. So, we can define an interface as a pure abstract class which allows us to define only abstract methods. 
The abstract method means a method without body or implementation.

No comments:

Post a Comment

Today Tasks