Thursday, July 17, 2008

Interview Questions C#

General Questions


1. Does C# support multiple-inheritance?
No, use interfaces instead.

2. Describe the accessibility modifier “protected internal”.
It is available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

3. What’s the top .NET class that everything is derived from? System.Object.

4. Can you store multiple data types in System. Array? No.

5. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

6. 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 {}.

7. Can multiple catch blocks be executed?
No. Once the proper catch code fires off, the control is transferred to the finally block.

8. What is 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.

Class Questions:

0. How do you inherit from a class in C#?
Place a colon and then the name of the base class.

1. Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

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

3. What’s an abstract class?
A class that cannot be instantiated. Abstract class is an inherited class that have the methods overridden.

4. When do you absolutely have to declare a class as abstract?

When at least one of the methods in the class is abstract.

5. What’s an interface class?
It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.

6. Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.

7. Can you inherit multiple interfaces?Yes.

8. What’s the difference between an interface and abstract class?
In an interface no accessibility modifiers are allowed, which is ok in an abstract class.

9. What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack -> additional overhead but faster retrieval. Another difference is that structs CANNOT inherit. (Questions courtesy of Eyal)

Method and Property Questions

10. What’s 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.

11. What does the keyword “virtual” declare for a method or property?
The method or property can be overridden.

12. How is method overriding different from method overloading?
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

13. Can you override private virtual methods?
No. Private methods are not accessible outside the class.

Original answer: No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.

14. What are the different ways a method can be overloaded?
Different parameter data types, different number of parameters, different order of parameters.

15. What’s a delegate?
A delegate object encapsulates a reference to a method.

16. What’s a multicast delegate?
It’s a delegate that points to and eventually fires off several methods.

17. What is 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.

18. What namespaces are necessary to create a localized application?
System.Globalization and System.Resources.

19. What is the difference between and XML documentation tag?
Single line code example and multiple-line code example.

20. Is XML case-sensitive? Yes.

Debugging and Testing Questions

21. What debugging tools come with the .NET SDK?

1. CorDBG – command-line debugger.

2. DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR.

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() method 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. How do you debug an ASP.NET Web application?
Attach the aspnet_wp.exe process to the DbgClr debugger.

26. What are three test cases you should go through in unit testing?

1. Positive test cases (correct data, correct output).

2. Negative test cases (broken or missing data, proper handling).

3. Exception test cases (exceptions are thrown and caught properly).

27. Can you change the value of a variable while debugging a C# application? Yes.

ADO.NET and Database Questions

1. What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?
SQLServer.NET data provider is high-speed and robust. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access. OLE-DB.NET is not as fastest and efficient as SqlServer.NET.

2. Explain ACID rule of thumb for transactions.
A transaction must be:

1. Atomic - It is one unit of work and does not dependent on previous and following transactions.

2. Consistent - Data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.

3. Isolated - No transaction sees the intermediate results of the current transaction).

4. Durable - The values persist if the data committed even if the system crashes right after.

3. What connections does Microsoft SQL Server support?
Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and password).

4. What does the Initial Catalog parameter define in the connection string?
The database name to connect to.

5. What is the data provider name to connect to an Access database?
Microsoft.Access.

6. What does the Dispose method do with the connection object?
Deletes it from the memory.

7. 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. The connection string must be identical.

Some More C# Questions

1. When you inherit a protected class-level variable, who is it available to? Classes in the same namespace.

2. Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.

3. Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

4. How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.

5. What does the keyword virtual mean in the method definition? The method can be over-ridden.

6. Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

7. How can you overload a method? By using parameters.

8. What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the when lot of manipulations have to be done to the text.

9. How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.

10. What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable.

11. What’s class SortedList underneath? A sorted HashTable.

12. Will finally block get executed if the exception had not occurred? Yes.

13. 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 {}.

14. What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

15. What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods.

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

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

18. What namespaces are necessary to create a localized application? System.Globalization, System.Resources.

19. What’s the difference between and XML documentation tag? Single line code example and multiple-line code example.

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

21. How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.

22. Can you change the value of a variable while debugging a C# application? Yes.

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

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

25. Why would you use untrusted verificaion? Web Services might use it, as well as non-Windows applications.

26. What’s the data provider name to connect to Access database? Microsoft.Access.

27. What does dispose method do with the connection object? Deletes it from the memory.

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

No comments: