Wednesday, 7 December 2016

2 C# Interview Questions

   Top C# Interview Questions and Answers

1) What is C-Sharp (C#)?
C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).
2) Explain the features of C#?
Below are some of the features supported in C# -
  • Constructors and Destructors
  • Properties
  • Passing Parameters
  • Arrays
  • Main
  • XML Documentation and
  • Indexers

3) List some of the advantages of C#?
Below are the advantages of C# -
  • Easy to learn
  • Object oriented
  • Component oriented
  • Part of .NET framework
4) What are IDE’s provided by Microsoft for C# development?
Below are the IDE’s used for C# development –
  • Visual Studio Express (VCE)
  • Visual Studio (VS)
  • Visual Web Developer
5) Explain the types of comments in C#?
Below are the types of comments in C# -
  • Single Line Comment Eg : //
  • Multiline Comments Eg: /* */
  • XML Comments Eg : ///
6) Explain sealed class in C#?
Sealed class is used to prevent the class from being inherited from other classes. So “sealed” modifier also can be used with methods to avoid the methods to override in the child classes.
7) Give an example of using sealed class in C#?
Below is the sample code of sealed class in C# -
class X {} 
sealed class Y : X {}

Sealed methods –

class A
{
 protected virtual void First() { }
 protected virtual void Second() { }
}
class B : A
{
 sealed protected override void First() {}
 protected override void Second() { }
}
If any class inherits from class “B” then method – “First” will not be overridable as this method is sealed in class B.
8) List out the differences between Array and ArrayList in C#?
  • Array stores the values or elements of same data type but arraylist stores values of different datatypes.
  • Arrays will use the fixed length but arraylist does not uses fixed length like array.
9) Why to use “using” in C#?
“Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.
10) Explain namespaces 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.
11) Why to use keyword “const” in C#? Give an example.
“Const” keyword is used for making an entity constant. We can’t reassign the value to constant.
Eg: const string _name = "Test";
12) What is the difference between “constant” and “readonly” variables in C#?
  • “Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables.
  • “readonly” variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.
13) Explain “static” keyword in C#?
“Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.
14) What is the difference between “dispose” and “finalize” variables in C#?
  • Dispose - This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.
  • Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.
15) How the exception handling is done in C#?
In C# there is a “try… catch” block to handle the error.
16) Can we execute multiple catch blocks in C#?
No. Once any exception is occurred it executes specific exception catch block and the control comes out.
17) Why to use “finally” block in C#?
“Finally” block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last “finally” block will be executed. So closing connection to database / releasing the file handlers can be kept in “finally” block.
18) What is the difference between “finalize” and “finally” methods in C#?
  • Finalize – This method is used for garbage collection. So before destroying an object this method is called as part of clean up activity.
  • Finally – This method is used for executing the code irrespective of exception occurred or not.
19) What is the difference between “throw ex” and “throw” methods in C#?
  • “throw ex” will replace the stack trace of the exception with stack trace info of re throw point.
  • “throw” will preserve the original stack trace info.
20) Can we have only “try” block without “catch” block in C#?
Yes we can have only try block without catch block.
21) List out two different types of errors in C#?
Below are the types of errors in C# -
  • Compile Time Error
  • Run Time Error
22) Do we get error while executing “finally” block in C#?
Yes. We may get error in finally block.
23) Mention the assembly name where System namespace lies in C#?
Assembly Name – mscorlib.dll
24) What are the differences between static, public and void in C#?
  • Static classes/methods/variables are accessible throughout the application without creating instance. Compiler will store the method address as an entry point. 
  • Public methods or variables are accessible throughout the application. 
  • Void is used for the methods to indicate it will not return any value.
25) What is the difference between “out” and “ref” parameters in C#?
“out” parameter can be passed to a method and it need not be initialized where as “ref” parameter has to be initialized before it is used.
26) Explain Jagged Arrays in C#?
If the elements of an array is an array then it’s called as jagged array. The elements can be of different sizes and dimensions.
27) Can we use “this” inside a static method in C#?
No. We can’t use “this” in static method.
28) What are value types in C#?
Below are the list of value types in C# -
  • decimal
  • int
  • byte
  • enum
  • double
  • long
  • float
29) What are reference types in C#?
Below are the list of reference types in C# -
  • class
  • string
  • interface
  • object
30) Can we override private virtual method in C#?
No. We can’t override private virtual methods as it is not accessible outside the class.
31) Explain access modifier – “protected internal” in C#?
“protected internal” can be accessed in the same assembly and the child classes can also access these methods.
32) In try block if we add return statement whether finally block is executed in C#?
Yes. Finally block will still be executed in presence of return statement in try block.

33) What you mean by inner exception in C#?
Inner exception is a property of exception class which will give you a brief insight of the exception i.e, parent exception and child exception details.
34) Explain String Builder class in C#?
This will represent the mutable string of characters and this class cannot be inherited. It allows us to Insert, Remove, Append and Replace the characters. “ToString()” method can be used for the final string obtained from StringBuilder. For example,
StringBuilder TestBuilder = new StringBuilder("Hello");
TestBuilder.Remove(2, 3); // result - "He"
TestBuilder.Insert(2, "lp"); // result - "Help"
TestBuilder.Replace('l', 'a'); // result - "Heap"
35) What is the difference between “StringBuilder” and “String” in C#?
  • StringBuilder is mutable, which means once object for stringbuilder is created, it later be modified either using Append, Remove or Replace.
  • String is immutable and it means we cannot modify the string object and will always create new object in memory of string type.
36) What is the difference between methods – “System.Array.Clone()” and “System.Array.CopyTo()” in C#?
  • “CopyTo()” method can be used to copy the elements of one array to other. 
  • “Clone()” method is used to create a new array to contain all the elements which are in the original array.
37) How we can sort the array elements in descending order in C#?
“Sort()” method is used with “Reverse()” to sort the array in descending order.
38) Explain circular reference in C#?
This is a situation where in, multiple resources are dependent on each other and this causes a lock condition and this makes the resource to be unused.
39) List out some of the exceptions in C#?
Below are some of the exceptions in C# -
  • NullReferenceException
  • ArgumentNullException
  • DivideByZeroException
  • IndexOutOfRangeException
  • InvalidOperationException
  • StackOverflowException etc.
40) Explain Generics in C#?
Generics in c# is used to make the code reusable and which intern decreases the code redundancy and increases the performance and type safety. 
Namespace – “System.Collections.Generic” is available in C# and this should be used over “System.Collections” types.
41) Explain object pool in C#?
Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.
42) What you mean by delegate in C#?
Delegates are type safe pointers unlike function pointers as in C++. Delegate is used to represent the reference of the methods of some return type and parameters.
43) What are the types of delegates in C#?
Below are the uses of delegates in C# -
  • Single Delegate
  • Multicast Delegate
  • Generic Delegate
44) What are the three types of Generic delegates in C#?
Below are the three types of generic delegates in C# -
  • Func
  • Action
  • Predicate
45) What are the differences between events and delegates in C#?
Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in subscriber/destination model.
46) Can we use delegates for asynchronous method calls in C#?
Yes. We can use delegates for asynchronous method calls.
47) What are the uses of delegates in C#?
Below are the list of uses of delegates in C# -
  • Callback Mechanism
  • Asynchronous Processing
  • Abstract and Encapsulate method
  • Multicasting
48) What is Nullable Types in C#?
Variable types does not hold null values so to hold the null values we have to use nullable types. So nullable types can have values either null or other values as well.
Eg: Int? mynullablevar = null;
49) Why to use “Nullable Coalescing Operator” (??) in C#?
Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable. For example,
double? myFirstno = null;
double mySecno;
mySecno = myFirstno ?? 10.11;

No comments:

Post a Comment