1000 C Sharp Dot NET (C#.NET) 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 im mutable, 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?
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 im mutable, 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.
These question and answers are taken from the book
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 user name 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.
42. What Is C#?
C# is a programming language designed by Microsoft. It is loosely based on C/C++, and bears a striking similarity to Java. Microsoft describe C# as follows:
"C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. C# (pronounced 'C sharp') is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++."
These question and answers are taken from the book
43. How Do I Develop C# Apps?
The (free) .NET SDK contains the C# command-line compiler (csc.exe). Visual Studio has fully integrated support for C# development. On Linux you can use Mono.
44. Does C# Replace C++?
There are three options open to the Windows developer from a C++ background:
•Stick with standard C++. Don't use .NET at all.
•Use C++ with .NET. Microsoft supply a .NET C++ compiler that produces IL rather than machine code. However to make full use of the .NET environment (e.g. garbage collection), a set of extensions are required to standard C++. In .NET 1.x this extended language is called Managed Extensions for C++. In .NET 2.0 ME C++ has been completely redesigned under the stewardship of Stan Lippman, and renamed C++/CLI.
•Forget C++ and use C#.
Each of these options has merits, depending on the developer and the application. For my own part, I intend to use C# where possible, falling back to C++ only where necessary. ME C++ (soon to be C++/CLI) is very useful for interop between new .NET code and old C++ code - simply write a managed wrapper class using ME C++, then use the managed class from C#. From experience, this works well.
45. Does C# Have Its Own Class Library?
Not exactly. The .NET Framework has a comprehensive class library, which C# can make use of. C# does not have its own class library.
46. What Standard Types Does C# Use?
C# supports a very similar range of basic types to C++, including int, long, float, double, char, string, arrays, structs and classes. However, don't assume too much. The names may be familiar, but many of the details are different. For example, a long is 64 bits in C#, whereas in C++ the size of a long depends on the platform (typically 32 bits on a 32-bit platform, 64 bits on a 64-bit platform). Also classes and structs are almost the same in C++ - this is not true for C#. Finally, chars and strings in .NET are 16-bit (Unicode/UTF-16), not 8-bit like C++.
47. Is It True That All C# Types Derive From A Common Base Class?
Yes and no. All types can be treated as if they derive from object (System.Object), but in order to treat an instance of a value type (e.g. int, float) as object-derived, the instance must be converted to a reference type using a process called 'boxing'. In theory a developer can forget about this and let the run-time worry about when the conversion is necessary, but in reality this implicit conversion can have side-effects that may trip up the unwary.
48. What Are The Fundamental Differences Between Value Types And Reference Types?
C# divides types into two categories - value types and reference types. Most of the intrinsic types (e.g. int, char) are value types. Structs are also value types. Reference types include classes, arrays and strings. The basic idea is straightforward - an instance of a value type represents the actual data, whereas an instance of a reference type represents a pointer or reference to the data.
The most confusing aspect of this for C++ developers is that C# has predetermined which types are represented as values, and which are represented as references. A C++ developer expects to take responsibility for this decision.
For example, in C++ we can do this:
int x1 = 3; // x1 is a value on the stack
int *x2 = new int(3) // x2 is a pointer to a value on the heap
but in C# there is no control:
int x1 = 3; // x1 is a value on the stack
int x2 = new int();
x2 = 3; // x2 is also a value on the stack!
49. Okay, So An Int Is A Value Type, And A Class Is A Reference Type. How Can Int Be Derived From Object?
It isn't, really. When an int is being used as an int, it is a value. However, when it is being used as an object, it is a reference to an integer value (on the managed heap). In other words, when you treat an int as an object, the runtime automatically converts the int value to an object reference. This process is called boxing. The conversion involves copying the int to the heap, and creating an object instance which refers to it. Unboxing is the reverse process - the object is converted back to a value.
int x = 3; // new int value 3 on the stack
object objx = x; // new int on heap, set to value 3 - still have x=3 on stack
int y = (int)objx; // new value 3 on stack, still got x=3 on stack and objx=3 on heap
50. Are C# References The Same As C++ References?
Not quite. The basic idea is the same, but one significant difference is that C# references can be null . So you cannot rely on a C# reference pointing to a valid object. In that respect a C# reference is more like a C++ pointer than a C++ reference. If you try to use a null reference, a NullReferenceException is thrown.
For example, look at the following method:
void displayStringLength( string s )
{
Console.WriteLine( "String is length {0}", s.Length );
}
The problem with this method is that it will throw a NullReferenceException if called like this:
string s = null;
displayStringLength( s );
Of course for some situations you may deem a NullReferenceException to be a perfectly acceptable outcome, but in this case it might be better to re-write the method like this:
void displayStringLength( string s )
{
if( s == null )
Console.WriteLine( "String is null" );
else
Console.WriteLine( "String is length {0}", s.Length );
}
Comments
Post a Comment