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.
Does C# support multiple-inheritance? No. Who is a protected class-level variable available to? It is available to any sub class !a class inheriting this class". Are private class-level variables inherited? #es, but they are not accessible. $lthough they are not visible or accessible via the class interface, they are inherited. Describe the accessibility modifier protected internal!" It is available to classes that are within the same assembly and derived from the specified base class. What#s the top "$%& class that everythin' is derived from? %ystem.&b'ect. What does the term immutable mean? The data value may not be changed. Note( The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory. What#s the difference between (ystem"(trin' and (ystem"&e)t"(trin'*uilder classes? %ystem.%tring is immutable. %ystem.%tring)uilder was designed with the purpose of having a mutable string where a variety of operations can be performed. What#s the advanta'e of usin' (ystem"&e)t"(trin'*uilder over (ystem"(trin'? %tring)uilder is more efficient in cases where there is a large amount of string manipulation. %trings are immutable, so each time a string is changed, a new instance in memory is created. Can you store multiple data types in (ystem"Array? No.
4.
5. 6.
7.
8.
9.
(ystem"Array"Clone+,? The *lone!" method returns a new array !a shallow copy" ob'ect containing all the elements in the original array. The *opyTo!" method copies the elements into another existing array. )oth perform a shallow copy. $ shallow copy means the contents !each array element" contains references to the same ob'ect as the elements in the original array. $ deep copy !which neither of these methods performs" would create a
new instance of each element+s ob'ect, resulting in a different, yet identacle ob'ect.
11. -ow can you sort the elements of the array in descendin' order?
)y calling %ort!" and then ,everse!" methods.
12. What#s the "$%& collection class that allows an element to be accessed usin'
a uni.ue /ey? HashTable.
14. Will the finally bloc/ 'et e)ecuted if an e)ception has not occurred?
#es.
$ catch block that catches the exception of type %ystem.-xception. #ou can also omit the parameter data type in this case and 'ust write catch ./.
16. Can multiple catch bloc/s be e)ecuted for a sin'le try statement?
No. &nce the proper catch block processed, control is transferred to the finally block !if there are any".
17. %)plain the three services model commonly /now as a three-tier application"
0resentation !1I", )usiness !logic and underlying code" and 2ata !from storage or other sources".
Class Questions
1.
What is the synta) to inherit from a class in C#? 0lace a colon and then the name of the base class. -xample( class MyNewClass : MyBaseClass Can you prevent your class from bein' inherited by another class? #es. The keyword 3sealed4 will prevent the class from being inherited. Can you allow a class to be inherited1 but prevent the method from bein' over-ridden? #es. 5ust leave the class public and make the method sealed. What#s an abstract class? $ class that cannot be instantiated. $n abstract class is a class that must be inherited and have the methods overridden. $n abstract class is essentially a blueprint for a class without any implementation. When do you absolutely have to declare a class as abstract? 6. 7hen the class itself is inherited from an abstract class, but not all base abstract methods have been overridden. 8. 7hen at least one of the methods in the class is abstract. What is an interface class? Interfaces, like classes, define a set of properties, methods, and events. )ut unlike classes, interfaces do not provide implementation. They are implemented by classes,
2. 3.
4.
5.
6.
7.
Why can#t you specify the accessibility modifier for methods inside the interface? They all must be public, and are therefore public by default. Can you inherit multiple interfaces? #es. .N-T does support multiple interfaces. What happens if you inherit multiple interfaces and they have conflictin' method names? It9s 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 you9re okay. &o Do2 Investigate
8. 9.
2. 3.
4.
5.
6.
appropriate constructor" in the overloaded constructor definition inside the inherited class.
3.
2.
3.
4.
5.
6. 7.
-ow do you debu' an A(4"$%& Web application? $ttach the aspnetCwp.exe process to the 2bg*lr debugger. What 6. 8. D. are three test cases you should 'o throu'h in unit testin'? 0ositive test cases !correct data, correct output". Negative test cases !broken or missing data, proper handling". -xception test cases !exceptions are thrown and caught properly".
8.
Can you chan'e the value of a variable while debu''in' a C# application? #es. If you are debugging via :isual %tudio.N-T, 'ust go to Immediate window.
2.
3.
4.
5.
6.
7. 8.
&o Do2 answer better. The current answer is not entirely correct.
9.
What is a pre-re.uisite for connection poolin'? <ultiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings. The connection string must be identical.
Assembly Questions
1.
-ow is the D00 -ell problem solved in "$%&? $ssembly versioning allows the application to specify not only the library it needs to run !which was available under 7inD8", but also the version of the assembly. What are the ways to deploy an assembly? $n <%I installer, a *$) archive, and ;*&0# command. What is a satellite assembly? 7hen you write a multilingual or multi cultural application in .N-T, and want to distribute the core application separately from the localiJed modules, the localiJed assemblies that modify the core application are called satellite assemblies. What namespaces are necessary to create a locali=ed application? %ystem.?lobaliJation and %ystem.,esources. What is the smallest unit of e)ecution in "$%&? an $ssembly. When should you call the 'arba'e collector in "$%&? $s a good rule, you should not call the garbage collector. However, you could call the garbage collector when you are done using a large ob'ect !or set of ob'ects" to force the garbage collector to dispose of those very large ob'ects from memory. However, this is usually not a good practice. -ow do you convert a value-type to a reference-type? 1se )oxing. What happens in memory when you *o) and >nbo) a value-type? )oxing converts a value type to a reference type, thus storing the ob'ect on the heap. 1nboxing converts a reference type to a value type, thus storing the value on the stack.
2. 3.
4. 5. 6.
7. 8.