Tuesday, 14 July 2020

Top 12 C# Interview Questions & Answers

Dinesh Wadhwani

Top 12 C# Interview Questions & Answers

Q1. CAN YOU STORE DIFFERENT DATA TYPES DATA IN AN ARRAY IN C# ?
ANS: Yes we can create an object array, we can add any types to the object array including non primitive data types data like customer, employee etc. But we have to override ToString method if we want meaningful output. And we can do that in two ways first by creating the the normal array of particular size and second is we can create the arraylist with the help of collections. 

ArrayList - Learning C# by Developing Games with Unity 5.x ...

Q2. WHAT IS JAGGED ARRAY IN C# ?
ANS: A jagged array is an array of arrays. For example there are 3 employees and each employee has different numbers of qualifications and we have to print them then at that condition we will use jagged array. 


Q3. WHY AND WHEN WE USE ABSTRACT CLASSES IN C# . GIVE AN EXAMPLE WHERE WE COULD USE AN ABSTRACT CLASS IN C# ?
ANS: Let's understand this by an example :- suppose we have two separate classes first is FullTimeEmployee and second is ContractEmployee. We have many common details in this two classes which we have to maintain in both classes hence we have to maintain many duplicate code in both the classes which is not the good programming. To avoid this we can simply move the common code of both the classes into the newly created base class. But the question is this new class will be the concrete (non abstract class) OR abstract class. This question comes in mind because if we make the base class as a concrete class then nothing stopping us to create the object of base class because according to this example in our organization we only create the object of contract and full time employee not base employee class. So that's why we have to create that base class as a abstract class because we don't want to create the object of base class.


Q4. WHAT IS THE ADVANTAGE OF USING INTERFACE ?
ANS: 
1. Interface allow us to develop loosely coupled system (system OR code which is not depends on each other)
2. Interface are very useful for dependency injection
3. Interfaces make unit testing and mocking very easier
4. Interface helps us to achieve abstraction and that abstract class will be implemented in another class and another advantage is like by seeing the interface class we can get to know how much functions we have to implement

Q5. CAN WE STORE DIFFERENT LIST TYPES DATA IN A SINGLE LIST ?
ANS: Yes by creating a list of list of objects.


Q6. CAN AN ABSTRACT CLASS HAVE A CONSTRUCTOR ?
ANS: 
1. Yes an abstract class have a constructor. An abstract class constructors is used to initialize fields of the abstract class. 
2. You would provide a constructor for an abstract class if you want to initialize certain fields of the abstract class before the instantiation of a child class takes place.
3. An abstract class constructor can also be used to execute code that is relevant for every child class. This prevents duplicate code.
4. You cannot create an instance of an abstract class, we can create the instance of the classes that are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called.

Q7. CAN AN ABSTRACT METHOD CAN BE CALLED FROM ABSTRACT CLASS CONSTRUCTOR ?
ANS: Yes we can do that. For example we have created a abstract base class and in that we have created and abstract method and an concrete constructor and called the abstract method under that constructor. We have created child class in that we have inherited the base class and and overridden the base class abstract method and just by creating the object of that child class in the main method we can indirectly call the base class abstract method because whenever the object of child class be created then base class parameters and functions will be inherited in child class so at that time base class constructor will call and call the abstract class method (for which definition is provided in child class). In this way we can call the abstract method from abstract class constructor.

Q8. DIFFERENCE BETWEEN INT AND INT32 ?
ANS: 
1. Both int and int32 allow us to create a 32 bit integer. 
2. Whether we use int OR int32 the behavior will be identical
3. Int32 is not allowed when creating an enum
4. To use INT32 we need to use (using System) declaration but when we are using int we need not have to use that.

Q9. REVERSE EACH WORD IN A STRING USING C# ?
ANS: 
1. Split the input string using a input space as a separator
2. Split method returns a string array that contains each word of the input string
3. Select method constructs a new string array by reversing each character in each word
4. Join method join each reverse character word in a string array




Q10. C# ABSTRACT CLASS VIRTUAL METHOD. AN ABSTRACT CLASS HAS A DEFAULT IMPLEMENTATION OF A METHOD. THE METHOD DEFAULT IMPLEMENTATION IS NEEDED IN SOME CLASS BUT NOT NEEDED IN SOME CLASS. HOW CAN YOU ACHIEVE?
ANS: 
1. If you need default implementation of a abstract base class method than do not override the abstract base class default implementation method in child class. Just make the abstract base class method as virtual method 
2. If you do not need default implementation of abstract base class method then override this default implementation method in child class to get the new implementation and just make the abstract base class method as virtual method

Q11. DIFFERENCE BETWEEN IS AND AS KEYWORD IN C# ?
ANS: 
1. The is operator is used to check if the run-time type of an object is compatible with the given type. If it is compatible then it will return true else false. The is operator returns boolean type
2. The as operator is used to perform conversion between compatible reference types. as operator is not of boolean type. It will return null if conversion between compatible reference types is not matched.
3. The is operator returns true if the given object is of the same type else it will return false whereas as operator returns the object when they are compatible with the given type. Else it will return null

Q12. WHAT IS SEALED KEYWORD IN C# ?
ANS: 
1. Sealed classes are used to restrict the inheritancefeature of object-oriented programming.
2. Reason why you would want to sealed class is that it improves performance a little bit. If you know that you are not going to be using inheritance on them.

Sunday, 28 June 2020

Meaning Examples and Definitions of the SOLID principles in Software and Object Oriented Design

Dinesh Wadhwani

S.O.L.I.D: The First 5 Principles of Object Oriented Design

WHAT IS SOLID PRINCIPLES :-

1. SOLID principles is a coding standard through which we have a clear concept for developing software in a proper way to avoid bad design

2. It was introduced by Robert C martin

3. When we apply SOLID principles then it makes our code more easier to read

4. It helps us to achieve better testability of code by creating test classes of the classes.

5. This principle is an acronym of the five principles which is given below :-




A. SINGLE RESPONSIBILITY PRINCIPAL

1. One class should only serve one purpose. All the methods and properties should all work towards the same goal

2. So whatever the responsibility of the class it should handle that responsibility only

3. For example : If a class is creating to retrieve the data from the table so it should do the retrieve operation only. It should not perform the other operations in that class. If they want to perform other operations then they have to create other class for that


B. OPEN CLOSED PRINCIPAL

1. Software entities such as classes, functions etc should be open for extension but closed for modification 

2. Any new functionality should be implemented by adding new classes and methods instead of changing the current ones OR existing ones

3. For example : If existing functionality is already written to select the data from a particular table and we have to add one more functionality to delete the data from the table then instead of modifying that in existing class we will create new class to add that new functionality OR just we with the help of inheritance we will inherit the existing Functionality and write our own Functionality in derived class

4. Or we can understand like this like one function is already there in order to add a new functionality we can extend that function but we cannot modify that that is why it said like this open for extension but closed for modification.


C. LISKOV SUBSTITUTION PRINCIPLE

1. Derived types must be completely substitute for their base types

2. This principal is extension of the open close principal

3. For example : two classes are there first is animal and second is dog so we can create the object like below

4. Animal obj = new dog(); - Because dog should have all the fields for animal class

5. But we cannot create object like below

6. Dog obj = new animal();

7. Similarly like another example is there : Two classes are there first is employee and second is temporary employee so we can create the object like this Employee obj = new Temporary employee(); but not like this Temporary employee obj = new Employee();


D. INTERFACE SEGREGATION PRINCIPAL

1. A client should not be forced to implement an interface that it doesn't use

2. This rule means that we should break our interfaces in many smaller ones so they better satisfy the exact needs of our clients

3. For example : one interface class is created in which two abstract functions are declared one is to select the data from database and another is to delete the data from table

4. One class is created in which we have implemented the above interface two methods which is fine

5. But I have created the other class in which I need only to select the data from DB but if I will not implement the second function then it will give error to me because that function is abstract function

6. So the solution is for second class we will create the seperate interface in which I will create abstract function and implement in that class then that will be fine


E. DEPENDENCY INVERSION PRINCIPLE

1. High level modules should not depends on low level modules.Both should depend on abstraction

2. The interaction between high level and low level modules should be thought as an abstract interaction between them

3. For example : Two classes are there first one is car and second is Scorpio, Scorpio class has one function drive which car class is using but suppose that if sometime Scorpio function drive become change then it will create the problem for class car. So the solution for this is to create a interface between them and use that interface in child class so whenever Scorpio function be change then it should change in interface also