C# Interview Questions
C# Interview Questions
1. What’s the implicit name of the parameter that gets passed into
the class’ set method? Value and its data type depend on whatever variable
we’re changing.
2. How do you inherit from a class in C#? Place a colon and then the
name of the base class. Notice that it’s double colon in C++.
5. 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.
9. What does the keyword virtual mean in the method definition? The
method can be over-ridden.
10. 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.
11. Can you override private virtual methods? No, moreover, you cannot
access private methods in inherited classes, have to be protected in the base
class to allow any sort of access.
12. Can you prevent your class from being inherited and becoming a
base class for some other classes? Yes, that’s what keyword sealed in the
class definition is for. The developer trying to derive from your class will get a
message: cannot inherit from Sealed class WhateverBaseClassName. It’s the
same concept as final class in Java.
13. Can you allow class to be inherited, but prevent the method from
being over-ridden? Yes, just leave the class public and make the method
sealed.
16. What’s an interface class? It’s an abstract class with public abstract
methods all of which must be implemented in the inherited classes.
17. 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 is public by default.
21. How can you overload a method? Different parameter data types,
different number of parameters, different order of parameters.
27. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
28. What’s the .NET datatype that allows the retrieval of data by a
unique key? HashTable.
30. Will finally block get executed if the exception had not occurred?
Yes.
31. 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 {}.
32. 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.
36. 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.
37. What are the ways to deploy an assembly? An MSI installer, a CAB
archive, and XCOPY command.
45. 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.
46. 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.
47. 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.
48. 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.
57. 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
%’.
58. 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.
61. Why would you use untrusted verificaion? Web Services might use it,
as well as non-Windows applications.
62. What does the parameter Initial Catalog define inside Connection
String? The database name to connect to.
64. What does Dispose method do with the connection object? Deletes
it from the memory.
69. What’s the top .NET class that everything is derived from?
System.Object.
75. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
Class Questions:-
What is the syntax to inherit from a class in C#?
Place a colon and then the name of the base class.
Example: class MyNewClass : MyBaseClass
Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.
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.
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?
Yes. .NET does support multiple interfaces.
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.
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).
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.
Assembly Questions
How is 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.
WHAT IS SERIALIZATION?
Serialization is the process of converting an object or a connected graph of
objects into a contiguous stream of bytes. Deserialization is the process of
converting a contiguous stream of bytes back into its graph of connected
objects. The ability to convert objects to and from a byte stream is an
incredibly useful mechanism. Here are some examples:
• An application's state (object graph) can easily be saved in a disk file or
database and then restored the next time the application is run. ASP.NET
saves and restores session state by way of serialization and deserialization.
• A set of objects can easily be copied to the system's clipboard and then
pasted into the same or another application. In fact, Windows® Forms uses
this procedure.
• A set of objects can be cloned and set aside as a backup while a user
manipulates the main set of objects.
• A set of objects can easily be sent over the network to a process running on
another machine. The Microsoft® .NET Framework remoting architecture
serializes and deserializes objects that are marshaled by value.
Why would you want to use serialization? The two most important
reasons are
• to persist the state of an object to a storage medium so an exact copy can
be recreated at a later stage, and
• to send the object by value from one application domain to another.
DOM Approach is useful for small documents in which the program needs to
process a large portion of the document.
SAX approach is useful for large documents in which the program only needs
to process a small portion of the document.
SAX parser generally requires more code than the DOM interface.
Unless we build a DOM style tree from our application's internal representation
for the data, we can't as easily write the XML file back to disk.
The DOM tree is not constructed, so there is potentially less memory
allocation.
If we convert the data in the DOM tree to another format, the SAX API may
help to remove the intermediate step.
If we do not need all the XML data in memory, the SAX API allows us to
process the data as it is parsed.