Tuesday, 14 July 2020

Top 12 C# Interview Questions & Answers

Top 12 C# Interview Questions & Answers


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.

Dinesh Wadhwani

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

1 comments:

  1. Top 12 C Interview Questions And Answers >>>>> Download Now

    >>>>> Download Full

    Top 12 C Interview Questions And Answers >>>>> Download LINK

    >>>>> Download Now

    Top 12 C Interview Questions And Answers >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete