C# Interview Questions
C# Interview Questions
This is a list of questions I have gathered from other sources and created myself over a
period of time from my experience, many of which I felt where incomplete or simply
wrong. I have finally taken the time to go through each question and correct them to the
best of my ability. However, please feel free to post feedback to challenge, improve, or
suggest new questions. I want to thank those of you that have contributed quality
questions and corrections thus far.
There are some question in this list that I do not consider to be good questions for an
interview. However, they do exist on other lists available on the Internet so I felt
compelled to keep them easy access.
General Questions
1.
2.
3.
4.
5.
6.
7.
8.
9.
Array.CopyTo does not make a deep copy. This can be easily verified by running the
following code.
public class MyClass
{
public static void Main()
{
Container[] containerArray = new Container[10];
for( int i=0; i < containerArray.Length; i++ ){
containerArray[i] = new Container( i );
}
Container[] containerArrayCopy = new Container[10];
containerArray.CopyTo( containerArrayCopy, 0 );
containerArrayCopy[2].V = 3;
for( int i=0; i < containerArray.Length; i++ ){
Console.Write( "{0}, ", containerArray[i].V );
}
Console.WriteLine();
}
}
public class Container{
public int V;
public Container(int i ){
V = i;
}
}
11. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
12. Whats the .NET collection class that allows an element to be accessed
using a unique key?
HashTable, Dictionary, NameValueCollection.
13. What class is underneath the SortedList class?
A sorted HashTable.
14. Will the finally block get executed if an exception has not occurred?
Yes. Finally block always get executed
15. Whats the C# syntax to catch any possible exception?
A catch block that catches the exception of type System.Exception. You can also
omit the parameter data type in this case and just write catch {}.
16. Can multiple catch blocks be executed for a single try statement?
No. Once the proper catch block processed, control is transferred to the finally
block (if there are any).
17. Explain the three services model commonly know as a three-tier
application.
Presentation (UI), Business (logic and underlying code) and Data (from storage or
other sources).
If A.equals(B) is true then A.getHashcode & B.getHashCode must always return same
hash code.
Answer :
The answer is False because it is given that A.equals(B) returns true i.e. objects are
equal and now its hashCode is asked which is always independent of the fact that
whether objects are equal or not. So, GetHashCode for both of the objects returns
different value.
18.
Class Questions
1.
2.
Can you prevent your class from being inherited by another class?
Yes. The keyword sealed will prevent the class from being inherited.
3.
Can you allow a class to be inherited, but prevent the method from being
over-ridden?
Yes. Just leave the class public and make the method sealed.
4.
5.
6.
7.
Why cant you specify the accessibility modifier for methods inside the
interface?
They all must be public, and are therefore public by default.
8.
9.
What happens if you inherit multiple interfaces and they have conflicting
method names?
Its up to you to implement the method inside your own class, so implementation
is left entirely up to you. This might cause a problem on a higher-level scale if
similarly named methods from different interfaces expect different data, but as far
as compiler cares youre okay.
To Do: Investigate
Structs are value-type variables and are thus saved on the stack, additional
overhead but faster retrieval. Another difference is that structs cannot inherit.
Whats the implicit name of the parameter that gets passed into the set
method/property of a class?
Value. The data type of the value parameter is defined by whatever data type the
property is declared as.
2.
3.
4.
5.
6.
Whats a delegate?
A delegate object encapsulates a reference to a method.
2.
Is XML case-sensitive?
Yes.
2.
3.
2.
3.
Whats the difference between the Debug class and Trace class?
Documentation looks the same. Use Debug class for debug builds, use Trace class
for both debug and release builds.
4.
5.
6.
7.
What
1.
2.
3.
8.
2.
3.
involve La%.
4.
5.
6.
7.
What does the Initial Catalog parameter define in the connection string?
The database name to connect to.
8.
9.
Assembly Questions
1.
2.
3.
4.
5.
6.
7.
8.