Interview Questions
By: D.K. Gautam
1. What is C#?
• What we can do using C#
2. What is CLR ?
Applications which run under the CLR are provided with the following features.
CLR internally contains the following things in it.
https://youtu.be/vExAd3Bb9Ck
3. What is an object?
4. What is the difference between ref
& out parameters?
5. What is Jagged Arrays?
• int[][] jagArray = new int[5][];
• jagArray[0] = new int[4];
• jagArray[1] = new int[5];
• jagArray[2] = new int[3];
• jagArray[3] = new int[8];
• jagArray[4] = new int[11];
• jagArray[0] = new int[] { 3, 5, 7, 9 };
6. What is enum?
• An enumeration is a set of named integer constants. An
enumerated type is declared using the enum keyword.
• C# enumerations are value data type. In other words,
enumeration contains its own values and cannot inherit or
cannot pass inheritance.
• Exp:
• enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
7. What is Boxing and Unboxing?
Boxing is a process of converting a value type into reference
type.
int x=15;
Object obj=x; // Boxing
Unboxing is a process of converting reference type that is
created through the process boxing into a value type. but Un-
Boxing required an explicit conversion.
int y=Convert.ToInt32(obj); // Un-Boxing
8. What is difference between
constants and read-only?
• Constant variables are declared and initialized at compile time.
• The value can’t be changed afterwards.
• Read only is used only when we want to assign the value at run
time.
9. What is Static Classes?
• A static class is basically the same as a non-static class, but
there is one difference: a static class cannot be instantiated.
• In other words, you cannot use the new keyword to create a
variable of the class type. Because there is no instance variable,
you access the members of a static class by using the class name
itself.
10. 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.
• Syntax: sealed class <className>
• Exp. sealed class sealedDemo
11. 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.
Advantages
• Encapsulating the method's call from caller
• Effective use of delegate improves the performance of application
• Used to call a method asynchronously
Exp. public delegate int mydelegate(int num1, int num2);
12. What’s the difference between an
interface and abstract class?
• Interfaces have all the methods having only declaration but no
definition.
• In an abstract class, we can have some concrete methods.
• In an interface class, all the methods are public.
• An abstract class may have private methods.
• https://youtu.be/4scvQ4l5cRQ
13. What is the difference between
method overriding and method
overloading?
• In method overriding, we change the method definition in the
derived class that changes the method behavior.
• Method overloading is creating a method with the same name
within the same class having different signatures.
• https://youtu.be/kDI6qy9goa8
14. What is the difference between a
Struct and a Class?
• Structs are value-type variables and classes are reference types.
• Structs stored on the stack, causes additional overhead but
faster retrieval.
• Structs cannot be inherited.
• While defining Struct we use “struct “ keyword, while defining
class we use “class” keyword.
• https://youtu.be/JNLluFb7VQo
15. What is difference between is and
as operators in c#?
• “is” operator is used to check the compatibility of an object with
a given type and it returns the result as Boolean.
• “as” operator is used for casting of object to a type or a class.
16. Which are Access Modifiers
available in C#?
• public : There are no restrictions on accessing public members.
• private : Access is limited to within the class definition. This is
the default access modifier type if none is formally specified.
• protected : Access is limited to within the class definition and
any class that inherits from the class.
• internal : Access is limited exclusively to classes defined within
the current project assembly.
• protected internal : Access is limited to the current assembly
and types derived from the containing class. All members in
current project and all members in derived class can access the
variables.
17. What is Constructors? explain
• A is special method of the class that will be automatically
invoked when an instance of the class is created is called as
constructor.
• Constructors are mainly used to initialize private fields of the
class while creating an instance for the class.
• When you are not creating a constructor in the class, then
compiler will automatically create a default constructor in the
class that initializes all numeric fields in the class to zero and all
string and object fields to null.
• Syntax.
[Access Modifier] ClassName([Parameters])
{
}
18. Types of Constructors ?
Basically constructors are 5 types those are
Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private Constructor
19. What is Encapsulation?
• It is all about hiding of the data by wrapping the data under a
special container known as a class. Which provides the security
to members that are defined inside in it.
• Exp. class
20. What is abstraction ?
• This is all about hiding of the complexity and then provide the
set of interfaces to consuming the functionality.
• Method is the good example of abstraction because when we
call a method we never knows the logic behind them. We are
aware only how to called them but not how they are defined.
• Exp. Send a message, to call someone
21. Explain Polymorphism?
Polymorphism means one name many forms. Polymorphism
means one object behaving as multiple forms. One function
behaves in different forms.
In other words, "Many forms of a single object is called
Polymorphism.“
It can be achieved by two way
1. Method overloading and
2. Method overriding
22. What is inheritance in C#?
• Inheritance is the ability to create a class from another class, the
"parent" class, extending the functionality and state of the
parent in the derived, or "child" class. It allows derived classes to
overload methods from their parent class.
• In simple way we can say, establishing parent child relationship
between the classes is called as inheritance.
23. Differences Between Hashtable
and Dictionary
1. Hashtable is threadsafe and while Dictionary is not.
2. Dictionary is types means that the values need not to boxing while
Hashtable values need to be boxed or unboxed because it stored
the values and keys as objects.
3. When you try to get the value of key which does not exists in the
collection, the dictionary throws an exception of
'KeyNotFoundException' while hashtable returns null value.
4. When using large collection of key value pairs hashtable would be
considered more efficient than dictionary.
5. When we retrieve the record in collection the hashtable does not
maintain the order of entries while dictionary maintains the order of
entries by which entries were added.
6. Dictionary relies on chaining whereas Hashtable relies on rehashing.
24. String and String Builder
• String is immutable, Immutable means if you create string object
then you cannot modify it and It always create new object of string
type in memory.
• Example
string str = "Hello";
// create a new string instance instead of changing the old one
str += "How Are";
str += "You ??";
• StringBuilder is mutable, means if create string builder object then
you can perform any operation like insert, replace or append without
creating new instance for every time.it will update string at one place
in memory doesnt create new space in memory.
• Example
StringBuilder strb = new StringBuilder("");
strb.Append("Hello");
strb.Append("How Are You ??");
string strNew = strb.ToString();
25. 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.