You are on page 1of 6

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.

10. What#s the difference between the (ystem"Array"Copy&o+, and

(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.

13. What class is underneath the (orted0ist class?


$ sorted HashTable.

14. Will the finally bloc/ 'et e)ecuted if an e)ception has not occurred?
#es.

15. What#s the C# synta) to catch any possible e)ception?

$ 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.

and defined as separate entities from classes.

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.

10. What#s the difference between an interface and abstract class?


In an interface class, all methods are abstract there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. $n abstract class may have accessibility modifiers.

11. What is the difference between a (truct and a Class?


%tructs are value type variables and are thus saved on the stack, additional overhead but faster retrieval. $nother difference is that structs cannot inherit.

3ethod and 4roperty Questions


1.
What#s the implicit name of the parameter that 'ets passed into the set method5property of a class? :alue. The data type of the value parameter is defined by whatever data type the property is declared as. What does the /eyword virtual! declare for a method or property? The method or property can be overridden. -ow is method overridin' different from method overloadin'? 7hen overriding a method, you change the behavior of the method for the derived class. &verloading a method simply involves having another method with the same name within the class. Can you declare an override method to be static if the ori'inal method is not static? No. The signature of the virtual method must remain the same. !Note( &nly the keyword virtual is changed to keyword override" What are the different ways a method can be overloaded? 2ifferent parameter data types, different number of parameters, different order of parameters. If a base class has a number of overloaded constructors1 and an inheritin' class has a number of overloaded constructors6 can you enforce a call from an inherited constructor to a specific base constructor? #es, 'ust place a colon, and then keyword base !parameter list to invoke the

2. 3.

4.

5.

6.

appropriate constructor" in the overloaded constructor definition inside the inherited class.

%vents and Dele'ates


1. 2.
What#s a dele'ate? $ delegate ob'ect encapsulates a reference to a method. What#s a multicast dele'ate? $ delegate that has multiple handlers assigned to it. -ach assigned handler !method" is called.

730 Documentation Questions


1. 2.
Is 730 case-sensitive? #es. What#s the difference between 55 comments1 58 85 comments and 555 comments? %ingle line comments, multi line comments, and ;<= documentation comments. -ow do you 'enerate documentation from the C# file commented properly with a command-line compiler? *ompile it with the >doc switch.

3.

Debu''in' and &estin' Questions


1.
What debu''in' tools come with the "$%& (D9? 6. *or2)? @ command line debugger. To use *or2bg, you must compile the original *A file using the >debug switch. 8. 2bg*=, @ graphic debugger. :isual %tudio .N-T uses the 2bg*=,. What does assert+, method do? In debug compilation, assert takes in a )oolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true. What#s the difference between the Debu' class and &race class? 2ocumentation looks the same. 1se 2ebug class for debug builds, use Trace class for both debug and release builds. Why are there five tracin' levels in (ystem"Dia'nostics"&race(witcher? The tracing dumps can be quite verbose. Bor applications that are constantly running you run the risk of overloading the machine and the hard drive. Bive levels range from None to :erbose, allowing you to fine tune the tracing activities. Where is the output of &e)tWriter&race0istener redirected? To the *onsole or a text file depending on the parameter passed to the constructor.

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.

AD:"$%& and Database Questions


1.
What is the role of the Data;eader class in AD:"$%& connections? It returns a read only, forward only rowset from the data source. $ 2ata,eader provides fast access when a forward only sequential read is needed. What are advanta'es and disadvanta'es of 3icrosoft-provided data provider classes in AD:"$%&? %E=%erver.N-T data provider is high speed and robust, but requires %E= %erver license purchased from <icrosoft. &=- 2).N-T is universal for accessing other sources, like &racle, 2)8, <icrosoft $ccess and Informix. &=- 2).N-T is a .N-T layer on top of the &=- layer, so it9s not as fastest and efficient as %ql%erver.N-T. What is the wildcard character in (Q0? =et9s say you want to query database with =IF- for all employees whose name starts with =a. The wildcard character is G, the proper query with =IF- would involve H=aG9. %)plain ACID rule of thumb for transactions" $ transaction must be( 6. $tomic it is one unit of work and does not dependent on previous and following transactions. 8. *onsistent data is either committed or roll back, no 3in between4 case where something has been updated and something hasn9t. D. Isolated no transaction sees the intermediate results of the current transaction". I. 2urable the values persist if the data had been committed even if the system crashes right after. What connections does 3icrosoft (Q0 (erver support? 7indows $uthentication !via $ctive 2irectory" and %E= %erver authentication !via <icrosoft %E= %erver username and password". *etween Windows Authentication and (Q0 (erver Authentication1 which one is trusted and which one is untrusted? 7indows $uthentication is trusted because the username and password are checked with the $ctive 2irectory, the %E= %erver authentication is untrusted, since %E= %erver is the only verifier participating in the transaction. What does the Initial Catalo' parameter define in the connection strin'? The database name to connect to. What does the Dispose method do with the connection ob<ect? 2eletes it from the memory.

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.

You might also like