C# Fundamentals
-----------------------------------------------------------------------------------
C# Fundamentals
-----------------------------------------------------------------------------------
C# Interview Questions & Answers
-------------------------------------------------------------
1) What is C#?
C# is an object-oriented, type-safe, and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language.
2) 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.
3) Define Constructors
A constructor is a member function in a class that has the same name as its class. The constructor is automatically invoked whenever an object class is created. It constructs the values of data members while initializing the class.
4) What is the difference between public, static, and void?
Public declared variables or methods are accessible anywhere in the application. Static declared variables or methods are globally accessible without creating an instance of the class. Static member are by default not globally accessible it depends upon the type of access modified used. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And Void is a type modifier that states that the method or variable does not return any value.
5) What is namespace in C#?
A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.
6) What is the purpose of ‘using’ statement in C#?
The 'using' block is used to obtain a resource and process it and then automatically dispose of when the execution of the block completed.
7) What are value types in C#
Value type variables can be assigned a value directly. They are derived from the class System.ValueType.
The value types directly contain data. Some examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.
8) What are reference types in C#
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.
In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.
9) Compare Virtual methods and Abstract methods.
Any Virtual method must have a default implementation, and it can be overridden in the derived class using the override keyword. On the contrary, an Abstract method doesn’t have an implementation, and it resides in the abstract class. The derived class must implement the abstract method. Though not necessary, we can use an override keyword here.
10) Can multiple catch blocks be executed?
No, Multiple catch blocks can't be executed. Once the proper catch code executed, the control is transferred to the finally block, and then the code that follows the finally block gets executed.
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 an 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 a 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 implement ISerialize Interface. De-serialization is the reverse process of creating an object from a stream of bytes.
14) Can we use "this" command within a static method?
We can't use 'This' in a static method because we can only use static variables/methods in a static method.
15) What is boxing in C#?
When a value type is converted to object type, it is called boxing.
16) What is unboxing in C#?
When an object type is converted to a value type, it is called unboxing.
17) What is the difference between dynamic type variables and object type variables?
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.
18) What are pointer types in C#?
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.
19) What is the purpose of is operator in C#?
is operator determines whether an object is of a certain type.
20) What is the purpose of as operator in C#?
as operator casts without raising an exception if the cast fails.
21) What is encapsulation?
Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.
22) How encapsulation is implemented in C#?
Encapsulation is implemented by using access specifiers.
23) What is the difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can't be changed afterward. Read-only is used only when we want to assign the value at run time.
24) What is an interface class?
An Interface is an abstract class which has only public abstract methods, and the methods only have the declaration and not the definition.
25) What are sealed classes in C#?
We create sealed classes when we want to restrict the class to be inherited. Sealed modifier used to prevent derivation from a class. If we forcefully specify a sealed class as base class, then a compile-time error occurs.
26) What is method overloading?
Method overloading is creating multiple methods with the same name with unique signatures in the same class. When we compile, the compiler uses overload resolution to determine the specific method to be invoke.
27) What is the difference between Array and Arraylist?
In an array, we can have items of the same type only. The size of the array is fixed when compared. To an arraylist is similar to an array, but it doesn't have a fixed size.
28) Can a private virtual method be overridden?
No, because they are not accessible outside the class.
29) What is scope of a public member variable of a C# class?
Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
30) What is scope of a private member variable of a C# class?
Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
31) What is scope of a protected member variable of a C# class?
Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.
32) What is scope of a Internal member variable of a C# class?
Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.
33) What are nullable types in C#?
C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.
34) Can you create a function in C# which can accept varying number of arguments?
By using the params keyword, a method parameter can be specified which takes a variable number of arguments or even no argument.
35) How to sort an array in C#?
Using Array.sort(array) function. It sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
36) What is the difference between Finalize() and Dispose() methods?
Dispose() is called when we want for an object to release any unmanaged resources with them. On the other hand, Finalize() is used for the same purpose, but it doesn't assure the garbage collection of an object.
37) What are circular references?
Circular reference is situation in which two or more resources are interdependent on each other causes the lock condition and make the resources unusable.
38) What are generics in C#.NET?
Generics are used to make reusable code classes to decrease the code redundancy, increase type safety, and performance. Using generics, we can create collection classes. To create generic collection, System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace. Generics promotes the usage of parameterized types.
39) What is an object pool in .NET?
An object pool is a container having objects ready to be used. It tracks the object that is currently in use, total number of objects in the pool. This reduces the overhead of creating and re-creating objects.
40) What are delegates?
Delegates are same are function pointers in C++, but the only difference is that they are type safe, unlike function pointers. Delegates are required because they can be used to write much more generic type-safe functions.
------------------------------------------------------------------------------------------------------------------------------------------------