August 4, 2020

Top 25 C# Interview Question and Answer

1.   What is C#?
C# is an object oriented programming language developed by Microsoft. It is a safe and managed language that is compiled by .NET framework to generate Microsoft intermediate language (machine code). Suitable for developing web-based applications.


2.   What is class in c#?
Class is a group of similar objects. It is a template from which object are created. It ca have field, method, constructors, etc.

3.   What is an object?
Object is instance of class. All the member of the class can be accessed through the object. In c# object is a real world entity. For example chair, car , pen, mobile.

4.   What is namespace In C#?
Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace. We can say namespace is a collection of class

5.   What is function?
Function is a block of code that has a signature. Function is used to execute statements specified in the code block.
Function name: It is a unique name that is used to make Function call.
Return type: It is used to specify the data type of function return value.
Body: It is a block that contains executable statements.
Access specifier: It is used to specify function accessibility in the application.
Parameters: It is a list of arguments that we can pass to the function during call.


6.   What are constructors in C#?
A constructor is a member function in the class and has the same name as its class. Whenever the object class is created, the constructor is automatically invoked. It constructs the value of data members while initializing the class.

7.   What are the different types of constructors in C#?
Basically, there are five types of constructors:
1.    Static constructor
2.    Private constructor
3.    Copy constructor
4.    Default constructor
5.    Parameterized constructor

8.   What is static constructor?
Static constructor is used to initialize static data members as soon as the class is referenced first time.

9.   What is Destructor?
A destructor works opposite to constructor, It destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically.
destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.

10. What is static?
Static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. Static can be field, method, constructor, class, properties, operator and event.
Indexers and destructors cannot be static.

11. What is method overloading in C#?
Method overloading is mechanism to create multiple methods with the same name and unique signature in the same class. When you go for compilation, the compiler uses overload resolution to determine the specific method to be invoked.

12. What is Method Overriding?
If derived class defines same method as defined in its base class, it is known as method overriding in C#. It is used to achieve runtime polymorphism.

13. What is an interface?
Answer 1. Interface is an abstract class that has only public abstract method. These methods only have declaration not the definition. These abstract methods must be implemented in the inherited classes.
Answer2. Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated. It is used to achieve fully abstraction because it cannot have method body.
If a class or struct inherit an interface .it  must provide the implementation of all the methods declared inside the interface.
Interface methods are public and abstract by default. You cannot explicitly use public and abstract keywords for an interface method.

14. What is Abstract class?
Abstract class is a class which is declared abstract. It can have abstract and non-abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. derived class is forced to provide the implementation of all the abstract methods.
An abstract method in C# is internally a virtual method so it can be overridden by the derived class.

15. What is delegates?
A delegate is a type safe function pointer. That is it holds a reference (Point) to a function. The signature of the delegate must match the signature of the function, the delegate pointer to, otherwise you get compiler error. This is reason delegate are called type safe function pointer. You can create an instance of it.

16. What is List<T>?
List<T> class is used to store and fetch elements. It can have duplicate elements. It is found in System.Collections.Generic namespace.

17. What is HashSet<T>?
HashSet class can be used to store, remove or view elements. It does not store duplicate elements. It is suggested to use HashSet class if you have to store only unique elements. It is found in System.Collections.Generic namespace.

18. What is SortedSet<T>
SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order. It is found in System.Collections.Generic namespace.

19. What is Stack<T>?
Stack<T> class is used to push and pop elements. It uses the concept of Stack that arranges elements in LIFO (Last In First Out) order. It can have duplicate elements. It is found in System.Collections.Generic namespace.

20. What is LinkedList<T>
LinkedList<T> class uses the concept of linked list. It allows us to insert and delete elements fastly. It can have duplicate elements. It is found in System.Collections.Generic namespace.
21. What is encapsulation?
Answer:  Packaging an object’s variables within its method is called encapsulation.

22. What is abstraction?
Answer: Abstraction is of the process of hiding unwanted details from the user.

23. What is function overloading and operator overloading?
Function Overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several &nbsp;functions of the same name that perform similar tasks but on different data types.
Operator overloading: It allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).=]=4ds

24. What is Do-While Loop?
The C# do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
The C# do-while loop is executed at least once because condition is checked after loop body.

25. What is while loop?
In C#, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop than for loop.

26. What is For Loop?
The C# for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops.

27. What is Enum?
Enum in C# is also known as enumeration. It is used to store a set of named constants such as season, days, month, size etc. The enum constants are also known as enumerators. Enum in C# can be declared within or outside class and structs.
Enum constants has default values which starts from 0 and incremented to one by one. But we can change the default value.

28. What is Structs?
In C#, classes and structs are blueprints that are used to create instance of a class. Structs are used for lightweight objects such as Color, Rectangle, Point etc.
Unlike class, structs in C# are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct.

No comments:

Post a Comment