.NET Interview Questions - OOPS

1. What is Class?

A class is a way to create your own custom types by grouping together variables of other types, methods and events. It is a data type which represents a blue print of objects. We can say it is a template of object. The variable remains in memory until all references to it go out of scope.

2. What is Object?

An object can be a variable, a data structure, or a function. In the OOPS which is class driven, object refers to a particular instance of a class where the object can be a combination of variables, functions etc. Everything that the software object knows (state/ variables) and can do (behavior/ methods) is expressed by the variables and methods within that object

3. What are feature of OOPS? 

  1. Objects: Objects are the basic unit of OOP. They are instances of class, which have data members and uses various member functions to perform tasks.
  2. Classes: It is similar to Structures in C. It is a user defined data type but it can contain functions. As explained above, class is blueprint of an object. 
  3. Abstraction: Abstration is refered as OOPS feature which allows us to show only required features of the application and hinding rest of the details. It allows real complex world to be represented in simplified way.
  4. Encapsulation: This is nothing but similar to data binding. But encapsulation is the process of binding variables and function into the class.
  5. Inheritance: Inheritance was invented to increase the resuability of code. With inheritance, we can use once written code again and again. The class which is inherited is called base calls & the class which inherits is called derived class. So when, a derived class inherits a base class, the derived class can use all the functions which are defined in base class depending on the type and access modifier of class, hence making code reusable. Hierarchy is used to define more specialized classes with relation. For example, Vehicle class can be inherited by Car class. Car may add new attributes and may use some of existing properties of Vehicle class.
  6. Polymorphism: It makes the code more readable. It defines operator overloading. It is feature of OOPS which allows us to create functions with same name but with different arguments or return type. So, the function remains same but signature is different and hence acts differently.
  7. Message passing: When application wants to achieve certain tasks which can only be done using combination of objects. For example, if we want to make order processing application. We will use customer, product, order and payment object. And these objects should communicate between each other.
  8. Dynamic binding: Dynamic binding is concept of OOPS which usefull to make decesions at run time. It means that the code to be executed for a specific purpose is not known until run-time. Dynamic binding is also known as late binding or run-time binding.

  4. What is abstract class?

Abstract class is a class which can not be instantiated. It allows to have incomplete functions in this which must be implemented in child class.

  • Object of abstract class can not be created.
  • It is designed to act as a base class (to be inherited by other class).
  • Abstract class is somewhat similar to Interface.
  • In VB abstract class is created using 'MustInherit' and in C# we have 'Abstract' keyword.

5. What is Interface?

Interface is a contract which defines only signature of the functionality. So if a class implements an interface, it shows to outer world that it has the specific behaviour and funcationality which is defined in interface but that class implements at its own. For example, if a class implements IDisposible interface that means it has the functionality to dispose unmanaged and unused resources but that method has to be implemented. If a class implements an interface then it is compulsion to provide implementation of its all methods.

NOTE: One class can implement multiple interfaces but can inherit only one class.

6. What is delegate?

Delegate is a class which has the ability to hold reference of a function. Delegates are known as pointer-to-function.

7. Do events have return type?

No. Events can only have access modifiers as per our choice but generally this is also public. Events can be shared between multiple controls or methods.

8. What is shadowing?

When two elements in a program have same name, one of them hides or shadows the other one. In such case the object which is currently available in scope is referenced.

9. What is the difference between shadowing and overriding?

Overriding redefines only the implementation while shadowing redefines the whole element.

In overriding derived class can refer the parent class element by using 'ME in VB' and 'this in C#' but in shadowing we can access it with 'MyBase'.

10. What is the difference between delegate and event?

Delegates are just function pointers, That is, they hold references to functions. A Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.

An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic.

11. If we inherit a class do the private variables also get inherited?

Yes, the variables are inherited but can not be accessed directly from the class interface.

12. Define the access modifiers in .NET? 

  1. private : Access is limited to the containing type.
  2. protected : Access is limited to the containing class or types derived from the containing class.
  3. public : Access is not restricted.
  4. Internal : Access is limited to the current assembly.
  5. protected internal: Access is limited to the current assembly or types derived from the containing class.

 13. How can we prevent a class to be overriden?

By making a class sealed. In c# by using 'Sealed' keyword and in VB by using 'NotInheritable' keyword.

14. What are the similarities and differences in Class and Structure?

Similarities: 

  1. Both can have constructors, methods, properties, fields, constants, enumurations, events and event handlers.
  2. Both can implement interface.
  3. Both can have consturctors with/without parameters.

 Differences:

  1. Structures are value type and Classes are reference type. So structures use stack and classes use heap.
  2. Structure's members can not be declared as protected.
  3. We can not do inheritance in structures.
  4. Class object can be deleted by garbage collector but object of structure can not be deleted.

 15. What is 'Virtual'?

Virtual keyword is used when we need to override the property and methods.

Add comment

Loading