So you want to land your dream job as a C# programmer? You will need to prove that you know your stuff before any organization will hire you. Here are the most common interview questions that interviewers will use to test your technical knowledge and problem solving skills. Can you answer them all?
01. What are namespaces, and how they are used?
Namespaces are used to organize classes within the .NET Framework. They dictate the logical structure of the code. They are analogous to Java packages, with the key difference being Java packages define the physical layout of source files (directory structure) while .NET namespaces do not. However, many developers follow this approach and organize their C# source files in directories that correlate with namespaces. The .NET Framework has namespaces defined for its many classes, such as System.Xml–these are utilized via the using statement. Namespaces are assigned to classes via the namespace keyword.
02. What is a constructor?
A constructor is a class member executed when an instance of the class is created. The constructor has the same name as the class, and it can be overloaded via different signatures. Constructors are used for initialization chores.
03. What is a destructor?
A destructor deletes an object of the class from memory. It is called when the object is explicitly deleted by the code you write, or when the object passes out of scope, which can happen when the program exits a function. The destructor has the same name as the class, but with a tilde prefix.
04. How are methods overloaded?
Methods are overloaded via different signatures (number of parameters and types). Thus, you can overload a method by having different data types, different number of parameters, or a different order of parameters.
05. How do you prevent a class from being inherited?
The sealed keyword prohibits a class from being inherited.
06. Why use encapsulation?
Encapsulation – combining function definitions and data together into a class – is used to separate parts of the code from the rest of the program. This allows the private data of an object to be hidden from the rest of the program, keep code clean and easy to understand, and allows classes to be reused in other programs.
07. What is the difference between a class and a struct?
Whereas classes are passed by reference, structs are passed by value. Classes can be inherited, but structs cannot. Structs generally give better performance as they are stored on the stack rather than the heap.
08. What is boxing?
Boxing is the process of explicitly converting a value type into a corresponding reference type. Basically, this involves creating a new object on the heap and placing the value there. Reversing the process is just as easy with unboxing, which converts the value in an object reference on the heap into a corresponding value type on the stack. The unboxing process begins by verifying that the recipient value type is equivalent to the boxed type. If the operation is permitted, the value is copied to the stack.
09. What types of error can occur in a C# program?
The three possible types of C# error are as follows:
- Syntax error. This type of error, which is identified during compilation, occurs because the programmer has used syntax incorrectly or included a typo in the code.
- Logic error. This type of error causes the program to do something other than what the programmer intended. The program will output an unexpected result in response to tests.
- Runtime error. This type of error causes the program to crash or terminate incorrectly.
10. What is an object?
An object is an instance of a class through which we access the methods of that class. “New” keyword is used to create an object. A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.
11. What is Jagged Arrays?
The array which has elements of type array is called jagged array. The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.
12. What is the difference between ref & out parameters?
An argument passed as ref must be initialized before passing to the method, whereas out parameter needs not to be initialized before passing to a method.
13. What is serialization?
When we want to transport an object through network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be serializable, it should inherit ISerialize Interface.
De-serialization is the reverse process of creating an object from a stream of bytes.
14. What is difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.
15. What is an interface class?
Interface is an abstract class which has only public abstract methods and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.