C# interview questions and answers

  1. What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.
  2. Can you store multiple data types in System.Array? No.
  3. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()? The first one performs a deep copy of the array, the second one is shallow.
  4. How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.
  5. What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable.
  6. What’s class SortedList underneath? A sorted HashTable.
  7. Will finally block get executed if the exception had not occurred? Yes.
  8. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for 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 {}.
  9. Can multiple catch blocks be executed? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block.
  10. Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.
  11. What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.
  12. What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods.
  13. How’s the DLL Hell problem solved in .NET? Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.
  14. What are the ways to deploy an assembly? An MSI installer, a CAB archive, and XCOPY command.
  15. What’s a satellite assembly? When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
  16. What namespaces are necessary to create a localized application? System.Globalization, System.Resources.
  17. What’s the difference between // comments, /* */ comments and /// comments? Single-line, multi-line and XML documentation comments.
  18. How do you generate documentation from the C# file commented properly with a command-line compiler? Compile it with a /doc switch.
  19. What’s the difference between <c> and <code> XML documentation tag? Single line code example and multiple-line code example.
  20. Is XML case-sensitive? Yes, so <Student> and <student> are different elements.
  21. What debugging tools come with the .NET SDK? CorDBG – command-line debugger, and DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.
  22. What does the This window show in the debugger? It points to the object that’s pointed to by this reference. Object’s instance data is shown.
  23. What does assert() do? In debug compilation, assert takes in a Boolean 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.
  24. What’s 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.
  25. Why are there five tracing levels in System.Diagnostics.TraceSwitcher? The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from None to Verbose, allowing to fine-tune the tracing activities.
  26. Where is the output of TextWriterTraceListener redirected? To the Console or a text file depending on the parameter passed to the constructor.
  27. How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.
  28. What are three test cases you should go through in unit testing? Positive test cases (correct data, correct output), negative test cases (broken or missing data, proper handling), exception test cases (exceptions are thrown and caught properly).
  29. Can you change the value of a variable while debugging a C# application? Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.
  30. Explain the three services model (three-tier application). Presentation (UI), business (logic and underlying code) and data (from storage or other sources).
  31. What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET? SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix, but it’s a .NET layer on top of OLE layer, so not the fastest thing in the world. ODBC.NET is a deprecated layer provided for backward compatibility to ODBC engines.
  32. What’s the role of the DataReader class in ADO.NET connections? It returns a read-only dataset from the data source when the command is executed.
  33. What is the wildcard character in SQL? Let’s say you want to query database with LIKE for all employees whose name starts with La. The wildcard character is %, the proper query with LIKE would involve ‘La%’.
  34. Explain ACID rule of thumb for transactions. Transaction must be Atomic (it is one unit of work and does not dependent on previous and following transactions), Consistent (data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t), Isolated (no transaction sees the intermediate results of the current transaction), Durable (the values persist if the data had been committed even if the system crashes right after).
  35. What connections does Microsoft SQL Server support? Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and passwords).
  36. Which one is trusted and which one is untrusted? Windows Authentication is trusted because the username and password are checked with the Active Directory, the SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.
  37. Why would you use untrusted verificaion? Web Services might use it, as well as non-Windows applications.
  38. What does the parameter Initial Catalog define inside Connection String? The database name to connect to.
  39. What’s the data provider name to connect to Access database? Microsoft.Access.
  40. What does Dispose method do with the connection object? Deletes it from the memory.
  41. What is a pre-requisite for connection pooling? Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.
This entry was posted in .NET. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

20 Comments on C# interview questions and answers

  1. KK
    Posted 2/2/2004 at 7:27 am | Permalink

    Q.29. Correction.. You cannot change the value of a variable while debugging. If you do you have to restart the program.

    Good Interview Questions. Helped me a lot.

  2. Andrew O
    Posted 2/17/2004 at 7:40 am | Permalink

    In reply to KK’s comment: Actually you can change the value of variables by using the command window. Type IMMED in the command window prompt to switch to immediate mode which will get rid of the “>”. TO manipulate a variable in the command window, type strCustomerName.Value = “TechInterview INC” or txtComments.innerText = “New String value” and it will override the old value in strCustomerName with the new value.

  3. arun
    Posted 2/20/2004 at 6:18 am | Permalink

    how can we use sessionstate using sqlserver method?

  4. Andrew O
    Posted 2/24/2004 at 4:21 am | Permalink

    Arun >> I understand your question to be “how can session information/data be stored using a SQL server?”

    At work we use an Oracle 9i db to store our session info by removing the standard session provided by .NET and forcing our own session implementation in the web.config file.

    So to answer your question, in order to store ASP.NET session information inside SQL Server you must:

    1) Have SQL Server version 7 or 2000 installed.

    2) Execute the following script using SQL Server’s Query Analyzer, which creates the database that ASP.NET uses to store Session:


    3. Modify the web.config file appropriately (where the connectionString attribute denotes the connection string that should be used to open the database):
    <sessionState mode="SQLServer" sqlConnectionString=
    "data source=;user sa;password=" />

    Storing Session inside SQL Server is the most durable option, as information persists even if the machine crashes. This is the slowest option of the three, however, because session data must be accessed from disk rather than directly from memory, a process thousands of times slower. Note that as with the Windows Service option, you can point ASP.NET to a Session store on a different machine, which is also appropriate in Web farm settings.

  5. nayan
    Posted 2/26/2004 at 2:32 am | Permalink

    39)What’s the data provider name to connect to Access database? Ans : Microsoft Jet 3.51/4.0 OLEDB data provider

  6. Rajani
    Posted 3/13/2004 at 12:39 am | Permalink

    I am using java script to read the xml data and when i am using the statement var myxml=new ActveXObject(”MSxml2.DOMDocument.4.0″);
    i am getting a error as “Automation server cannot create objects.why??

  7. Michael
    Posted 4/6/2004 at 7:07 am | Permalink

    A couple of (probably picky) corrections:

    10 — you should throw your own exceptions when changing the type of an exception will give more information to the layer that you’ve decided should handle the exception, so you might wrap a FileNotFound exception in a custom “CantOpenConfigFileException” class for example. Rather than always attempting to handle errors in the method that threw the exception, you should define points in your architecture at which you’ve decided exceptions should be trapped and corrective code run.

    40 — it doesn’t delete it from memory, it closes any resources (e.g. returns the underlying database connection to the connection pool). The garbage collector will delete the object from memory at some undefined point in the future.

    41 — technically the decision on whether to use a pooled connection is made by a string comparison of the connection string being opened and that in the pool, so even though the parameters might be functionally the same, a difference in case or an extra space will cause the pooled connection not to be used.

  8. marimuthu
    Posted 6/13/2004 at 9:44 am | Permalink

    How can i edit the datagrid? please give one example?

  9. Bala
    Posted 11/21/2004 at 9:42 am | Permalink

    You should expand the answer to question 3 coz the answer here is sure to prompt another question - “whats the difference between shallow and deep copy?” and on some occasions might even annoy the interviewer :p

  10. Posted 11/6/2005 at 6:33 am | Permalink

    I think these questions are a bit .. shallow. Testing (asking) an interviewee purely trivial code questions ensures that we’re buying a code monkey. I think you need to focus on things like code structure, OOP, efficiency in coding, and architecture.

  11. Dom
    Posted 11/11/2005 at 4:26 am | Permalink

    37: Use untrusted (SQL) verification so everybody uses same connection string to access db. Only this way can connection pooling be used.

  12. mahesh
    Posted 2/6/2006 at 4:13 am | Permalink

    How can i edit the datagrid? please give one example?

  13. Posted 12/15/2006 at 12:08 am | Permalink

    The answer to 3 is wrong. They both perform shallow copies. Clone() returns a new object while CopyTo() shallow copies to an existing object.

    The result of them both is that both objects will contain pointers to the same data.

  14. chandra
    Posted 12/27/2006 at 12:20 pm | Permalink

    Q#3: The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identacle object.

    Copy to doesnt do “Deep copy” check this out

    public class MyClass
    public static void Main()
    Container[] containerArray = new Container[10];
    for( int i=0; i

  15. Gurjinder Singh Brar
    Posted 1/11/2007 at 9:33 am | Permalink

    Ans #40
    Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. Connection pool manager maintains a pool of open database connections. But be sure than your connections use the same connection string each time.

    Gurjinder Singh Brar

  16. xero
    Posted 3/6/2007 at 9:59 am | Permalink

    Q2: Yes and no. The array can only be of one type but it may have any derived types from that type in it. For instance, an object array.

    Q10: So does that mean the .NET framework and Windows have serious design flaws? If you never need to throw an exception then either you’re in a very small project or have a serious design issue.

    Q13: Assemblies are also automatically discovered without a system call or reboot.

    Q14: Via any means to distribute a file…

    Q16: You do not have to use the .NET Frame to perform localization.

    Q37: In case it’s too much overhead to maintain coupled ActiveDir and SQL Server perms or ever plan to switch away from ActiveDir.

    Q40: Dispose only inactivates the connection object. It will not be freed from memory until the GC wants it to be.

    And I agree, this are only good interview questions if you want a code monkey.

  17. Chaud
    Posted 3/28/2007 at 12:48 pm | Permalink

    > And I agree, this are only good interview questions
    > if you want a code monkey.

    Yes, but an architct should know the answers to basic .NET questions like these as a matter of course.

    Besides, the purpose of asking questions like this is not so much the answer as seeing how they handle a question they don’t know the answer to. Do they squirm and begin to bullshit, or do they just say, “I don’t know”?

    Me personally, I don’t handle these interview situations well. I get stressed and my brain freezes. Just recently in an interview, I was asked what IDisposable does and I froze. Jeez! After I walked out of the interview I could walk you through IDisposable, unmanaged resource handling and finalizers like a freakin’ instructor, but in the interview I looked like an ape.

    Not like “code monkey” ape, but more like a banana-eating, butt-scratching, poo-throwing ape.

  18. Rekha
    Posted 2/11/2008 at 4:12 am | Permalink

    is it possible to to define function inside structure?

  19. Posted 6/10/2008 at 9:06 pm | Permalink

    Regarding 10. I don’t see how it is BAD to throw your own exceptions (unless you catch them in the same scope of course). Exceptions are thrown so that your client code can handle them according to the context. No meaningful standalone component can handle all exceptions without throwing them othwerwise why THROW at all?

  20. MS
    Posted 10/16/2008 at 2:19 pm | Permalink

    Regarding 10, the answer to that is complete bullsh*t. If someone were interviewing me and said that, *I* would walk out. It shows a complete lack of understanding of what Exceptions in .NET are all about. On top of this, the answers for some of the others are vague and misleading at best.

    If you’re going to interview candidates, you really need to know what you’re talking about.

Post a Comment

Your email is never published nor shared. Required fields are marked *