OOP – DOT NET Chapter Wise Interview Questions

OOP – DOT NET Chapter Wise Interview Questions

Question 1:
What is Object-Oriented Programming?
OOP is software designing technique where we think in terms of real world objects.

Question 2:
What is a class and object?
Class is a blueprint/template. Objects are instances of classes, in other words they bring life in class. To use a class we need to create an object.

Question 3:
What are different properties provided by object-oriented systems?
Following are characteristics of object-oriented System:

  • Abstraction: Abstraction means show only what is necessary. Example color is abstracted to RGB (Red, Green Blue). By just making the combination of these three colors we can achieve any color in world. So rather than remembering each and every color we just use RGB.
  • Encapsulation: It is a process of hiding all the complex processing from the outside world and make your objects simple.
  • Inheritance: This concept helps to define parent-child relationship between classes.
  • Polymorphism: It is a property of object to act differently under different conditions. For instance a simple user object depending on conditions can act like a admin or like data entry object.Remember the word APIE (Abstraction, Polymorphism, Inheritance and Encapsulation).

Question 4:
How can we implement encapsulation in .NET?
Encapsulation can be achieved by using the below five access modifiers,

  • Private: Only members of class have access to the variables.
  • Protected: All members in current class and in derived classes can access the variables,
  • Friend (internal in C#): Only members in current project have access to the elements.
  • Protected friend (protected internal in C#): All members in current project and all members in derived class can access the variables.
  • Public: All members have access in all classes and projects.

Note: This question can also be tweaked by asking what are the different access modfiers in .NET

Question 5:
What’s the difference between abstraction and encapsulation?
Note: This question is a bit confusing question. Abstraction says show only what is necessary and encapsulation says hide complexity. Does it look like talking the same thing’s with different faces?

Abstraction and encapsulation complement each other. Encapsulation implements abstraction. Abstraction is design process while encapsulation happens during coding phase which is achieved by using access modifiers. Abstraction is done in design phase while encapsulation is implemented in execution phase using access modifiers.

Question 6:
How is inheritance implemented in .NET?
Inheritance is implemented by using the “:” symbol.
Below is a simple code snippet.where we have “Customer” class which is the parent class. We have then created a child class called as “CustomerDiscount” which inherits all the properties and adds a “Discount” property.

Question 7:
What are the two different types of polymorphism?
There are two kinds of polymorphism static and dynamic. Many people also call them as runtime or compile-time polymorphism.

Question 8:
How can we implement static polymorphism?
Static polymorphism is implemented by using method overloading. In compile-time itself we come to know if there are mismatches. Below code snippet shows how method overloading is implemented. The add method can take either 2 inputs or 3 inputs.
Depending on the number of inputs the addition logic is executed.

Question 9:
How can we implement dynamic polymorphism?
Dynamic polymorphism is implemented by using override and virtual keyword.
Below is a simple code snippet which has three classes, Customer class is the parent class. CustomerDiscountlOPercent and CustomerDiscount20Percent are child classes.
Customer parent class has a discount function which returns zero discounts. This function is defined as virtual and then overridden by both the child classes with 10% and 20% discount.

Now on the client-side on the fly your parent object can point to any child classes and invoke the child implementation accordingly. This is called as dynamic polymorphism; the parent object can point to any of the child objects and invoke the child function dynamically.

Question 10:
What is downcasting and upcasting?
“Upcasting” means moving subclass object to the parent class object. “Downcasting” is opposite to “Upcasting” moving the parent object to the child object.
“Upcasting” is perfectly valid but “Downcasting” is not allowed in .NET as shown in Figure 3.1. For instance below is a simple “Customer” parent class which is further inherited by a child class “GoldCustomer”.

Below is an “upcasting” code where the child parent class GoldCustomer is pushed to the Customer class.

Below is a sample of “downcasting” code where parent class object is tried to move to a child class object, this is not allowed in .NET.

Question 11:
What is the difference overriding and overloading?
Note: I am not sure why this question is asked frequently. It’s like comparing apples with mangoes. Probably it’s just the common word “over” which confuses developers.
Overloading is a concept where we can have same method names with different input signature.
In overriding we have a parent class with virtual functions which are overridden in the child classes.

Question 12:
What is operator overloading?
Operator overloading is a concept of polymorphism where you can redefine operators like +,-,* etc., with additional functionalities.
For instance we can redefine the + functionalities to add objects like obj l + obj 2. Below is simple code snippet which redefines + operator.

You can now use the + operator to add objects of type someclass as shown in the below code snipet.

Question 13:
What are abstract classes?
Abstract class is a half-defined parent class. The full implementation of abstract class is defined by the child classes.
For example below code snippet shows a simple abstract class/half-defined class called “DatabaseCommon” and later the concrete classes, i.e., “SQLServer” and “Oracle” inherit and define a complete implementation for the same.
To define an abstract class we need to use the abstract keyword.

Question 14:
What are abstract methods?
Abstract classes can have abstract methods. Abstract methods when defined in a parent class have to be implemented in the child classes. If abstract methods are not implemented it will throw an error.

Question 15:
What is an Interface?
Interface is a contract that defines the signature of the functionality. It looks like a class but has no implementation. It has only empty definition of methods, functions, events, and indexer.
Interfaces provide forced implementation. For instance in the below code snippet we have created a simple interface called as “IDbCompuisory”. The below classes who implement interface “IDbCompuisory” has to provide implementation for “ExecSql”.

Question 16:
Do interface has accessibility modifier?
All elements in Interface should be public. So no accessibility modifier is required.

Question 17:
Can we create an object of abstract class or an interface?

Question 18:
What is difference between abstract classes and interfaces?

Abstract class Interface
Implementation Some methods in abstract classes can have implementation. All methods, function, properties in interfaces have to empty compulsorily.
Scenario Abstract classes are used when we want to share common functionality in parent-child relationship. Interfaces are used to define contract, enforce standardization, decoupling and dynamic polymorphism.
We can declare variables. In interface we cannot declare variables.
Inheritance vs
Abstract classes are inherited. Interfaces are implemented.

Question 19:
An abstract with only abstract method, how is it different from interfaces?
If you define all methods, functions, properties as abstract in abstract class it inhibits the same behavior as an interface.

Question 20:
If we want to update interface with new methods, what is the best practice?
The biggest use of interface is to ensure that strict contract is followed between clients and components. So that when changes happen on the components clients do not have to change too much. In real world contracts between two parties do not change which implies that interfaces should also not change.
So if you want to add new methods or change methods in interfaces the best practice is to create new interfaces by inheriting. With this approach your older client who are using the interface stay happy and the new clients who want those new or changed methods get the benefits of the new functionality.
Let’s consider you have a simple interface called as ” Iview” which helps you to view data. Below is the code for the same. Let’s consider that this interface is consumed by many clients.

Over a period of time some users demand “Edit” functionality as well but the re$t of the users are happy with the ‘View” functionality and they do not want them to get affected by these changes. Now if you go and change this interface (“IView”) as shown in Figure 3.2 you will be disturbing everyone.
So the best option would be to add a new interface, i.e., “(Edit” which inherits from “IView”. So the “lEdit” will have the functionality for “Edit” as well as “View” because it’s also inheriting from “IView”. And the clients who are consuming “IView” do not need to update as “IView’ is not changed at all.

So putting this whole story in one single sentence for the interviewer.
Interface once fixed should not be changed. If we ever have to add new functions, new interfaces should be created so that we do not break compatibility with old clients.

Question 21:
What is a delegate?
Delegate is an abstract pointer to a function or method. In other words you can create a pointer which points to a method or function and then pass that pointer wherever you wish and invoke the function/ method.

Question 22:
How can we create a delegate?
Creating a delegate is four-step process:

  • Declare a delegate
  • Create an object reference
  • Point the reference to the method
  • Invoke the method via the delegate

Below is the code snippet for the same.

Question 23:
What is a multicast delegate?
Normally when you create a delegate, your delegate points to only one function or method. In case you want to point multiple functions and invoke them sequentially, you need to use the multicast delegate.
To point to multiple function using delegate pointer we need to use “+=” sign as shown in the below code snippet.

Question 24:
What are Events?
Events are higher level of encapsulation over delegates. Events use delegates internally. Delegates are naked and when passed to any other code, the client code can invoke the delegate. Event provides a publisher/subscriber mechanism model.
So subscribers subscribe to the event and publisher then push messages to all the subscribers. Below is a simple code snippet for the same:
Create a delegate and declare the event for the same.

Raise the event.

Attached client methods to the event are fired / notified.

Question 25:
What is the difference between delegate and events?
They(l) What is the difference between delegate and events? can not be compared because one derives from the other.

  • Actually, events use delegates in bottom. But they add an extra layer of security on the delegates, thus forming the publisher and subscriber model.
  • As delegates are function to pointers, they can move across any clients. So any of the clients can add or remove events, which can be confusing. But events give the extra protection / encapsulation by adding the layer and making it a publisher and subscriber model.

Just imagine one of your clients doing this

This will reset all your delegates to nothing and you have to keep searching where the error is.

Question 26:
Do events have return type?
No, events do not have return type.

Question 27:
Can events have access modifiers?

Question 28:
Can we have shared events?
Yes, you can have shared events, do note only shared methods can raise shared events.

Question 29:
Explain Action, Func, Anonymous methods and Lambda expressions.
Left to the readers.

Question 30:
What is shadowing?
Shadowing replaces the complete element of the parent class. For instance you can see in the below sample code where the clsParent has a variable int “i”, which is replaced by the child class clsChild by a method “i”.
In other words when you refer the parent object “i” it is a variable and when you refer the child object “i” it is a method.

Question 31:
What is the difference between shadowing and overriding?
Overriding redefines only the implementation while shadowing redefines the whole element.

Question 32:
If we inherit a class do the private variables also get inherited?
Yes, the variables are inherited.

Question 33:
How can we stop the class from further inheriting?
We can stop the class from further inheriting by using the “sealed” keyword. For instance below is a sample code where we have a class called as “Human” which is further inherited to create a “Male” or “Female” class.
Now the below code is great but we do not anyone to further inherit from “Male” or “Female” class. In simple words “Male” and “Female” are the last legs in this inheritance hierarchy. This can be done by using the “sealed” keyword.

If anyone tries to inherit the sealed classes an error appears”cannot derive from sealed type”.

Question 34:
What is the use of “Must Inherit” keyword in VB.NET?
If you want to create an abstract class in VB.NET it is done by using “Must Inherit” keyword. You cannot create an object of a class, which is marked as “Must Inherit”. When you define “Must Inherit” keyword for class, you can only use the class by inheriting.

Question 35:
What are similarities between class and structure?
Following are the similarities between classes and structures:

  • Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
  • Structures and classes can implement interface.
  • Both of them can have constructors with and without parameter.
  • Both can have delegates and events.

Question 36:
What is the difference between class and structure?

Following are the key differences between them:

  • Structures are value types and classes are reference types. So structures use stack and classes use heap.
  • Structures members cannot be declared as protected, but class members can be. You cannot do inheritance in structures.
  • Structures do not require constructors while classes require.
  • Objects created from classes are terminated using Garbage Collector (GC). Structures are not destroyed using GC.

Question 37:
When to use structures and when to use classes?
You will use structures when:
Point 1: If you want to represent a custom value type. This custom value type is derived from primitive data types (int, double). Some of the examples of custom types are co-ordinates (which have X, Y), complex numbers (which have real and imaginary components). You can also term these things as value objects or technical objects. Technical objects do not represent real world objects like customer, supplier, invoice, etc.
Point 2: If you want to have low memory footprint. For instance let’s say you want to plot a graph. To plot a graph you need to have 100’s of objects created so that you can represent co-ordinates. Now if you create a class and generate those 100’s of objects you end up putting lot of load on your garbage collector. If you create a “struct”, it is a value type as shown in Figure 3.3. So they get created and destroyed immediately. Thus putting less load on memory.
For all other scenarios use a class.
Question 38:
What does virtual keyword mean?
The signify that method and property can be overridden by the child classes.

Question 39:
What are shared (VB.NET)/Static(C#) variables?
When you define a variable as static or shared only one instance of the object or variable is created.

Question 40:
What is enum and what are the benefits of using it?
enum helps to define, manage and assign constants in effective way. Now the below sample code is good but the level values are not readable.

Now by declaring a simple enum called as Quality as shown below.

Our code would look more readable as shown below. The other big benefit is if we change the numeric values of quality we do not have to change throughout the project. So we can go a change the Low quality to 1 and no change in code is required.

So summarizing enum has two big benefits:

  • Code becomes more readable.
  • Easy to change constants without affecting throughout the project. Easy maintenance.

Question 41:
What is the use of Flags in enum?
“Flags” is an enum attribute. If you want to set multiple values to an enum we need to use Flags.

In the below code we are setting “MyColors” enum to “Blue” and “Green” valuess.

Question 42:
How can we loop through Enum values?
We can use “Getvalues ()” method of the “Enum” class which returns an array of Enum values.

Question 43:
What is the use of “Enum.parse” method?
Interviewers can also put this question differently how to convert string to a Enum value.
Many times we would like to convert string into Enum objects. For example let’s say you have a color Enum and you would like to pass string “Red” and get Enum object “color. Red”.
“Enum.parse” method parses string to an Enum value. Like in the below code we pass “Red” string value and it gets typecasted to enum which has green Enum as a constant.

Question 44:
What are nested classes?
Nested classes are classes within classes.

Question 45:
If you create the child class object which constructor will fire first?

Parent class constructor will fire first.

Question 46:
In what instances you will declare a constructor to be private?
When we create a private constructor, we cannot create object of the class. Private constructors are used when we want only a single instance of the class to be created and externally no one can use the ‘ new’ keyword to create the object.

Question 47:
Can we have different access modifiers on get/set methods of a property?
Yes, we can have different access modifiers. The below code will compile perfectly well.

Question 48:
How can you define a property read only for external world and writable in the same assembly?
This question is a variation to the previous question. So its possible that interviewer can ask the previous question in this format.
Let’s us first try to understand this question. Let’s say if you have a class called as “Customer” with a property “CustomerCode”. Now you want that anyone can read from the “CustomerCode” property but this property can only be set from within the assembly.
In other words any one can run the below code.

But setting of the value can be only done from within assembly. The below code will run only if it is within assembly and it will throw an error if it is external to the assembly.

This can be achieved by have different access modifiers for “set” and “get” properties on the “CustomerCode” property.

So for the “get” we will have public access modifiers and for “set” we will apply internal access modifiers. Now because “get” is public this property can be read anywhere and because the “set” is internal it can only be accessed from within assembly. Below is the code for the same.

Question 49:
Will the finally run in this code?
In the below code in the try statement we have a return statement, will the finally block still run.

Yes, the finally code will run even though there is a return statement in the try. The finally block will run irrespective what happens in try block. That’s the reason why finally block is a good place to put clean up code.

Question 50:
What is an Indexer?
Many times classes have contained (aggregated or composed) collections. For example in the below code you can see we have a customer class which has an Address collection.

Now let’s say we would like to fetch the addresses collection by “Pincode” and “PhoneNumber”. So the logical step would be that you would go and create two overloaded functions one which fetches by using “PhoneNumber” and the other by “PinCode”. You can see in the below code we have two functions defined.

Now on the client-side your code would look something as shown below.

Now the above code is great but how about simplifying things further. How about something more simple as shown in the below code. In other words the class itself is to be indexed.

You can see in the Figure 3.5 where we can have two overloaded indexers “Pincode” and “PhoneNumber”.

This can be achieved by using an indexer. There are two things we need to remember about indexer:

  • “Indexer” is defined by using “this”
  • “Indexer” is a property so we need to define set and get for the same.

Below is the code implementation for indexer. You can see we have used “this” keyword with two overloaded functions one which will fetch us address by using the “PhoneNumber” and the other by using “PinCode”.

Once you put the above “indexer” code you should be able to access the address collection from the Customer object using an indexer” [ ]” as shown in Figure 3.5.
Summarizing in one sentence: Indexers helps to access contained collection within a class using a simplified interface. It is a syntactic sugar.

Question 51:
Can we have static indexer in C#?

About the author


Leave a Comment