Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

301.

Are All Methods Virtual In C#?

Answer»

No. Like C++, METHODS are non-virtual by DEFAULT, but can be MARKED as virtual.

No. Like C++, methods are non-virtual by default, but can be marked as virtual.

302.

If C# Destructors Are So Different To C++ Destructors, Why Did Ms Use The Same Syntax?

Answer»

Presumably they wanted C++ programmers to FEEL at HOME. I THINK they MADE a mistake.

Presumably they wanted C++ programmers to feel at home. I think they made a mistake.

303.

Are C# Destructors The Same As C++ Destructors?

Answer»

No. They look the same but they are very different. The C# destructor syntax (with the familiar ~ character) is just syntactic sugar for an override of the System.Object Finalize method. This Finalize method is called by the garbage collector when it determines that an object is no longer referenced, before it frees the memory associated with the object. So far this sounds like a C++ destructor. The difference is that the garbage collector makes no guarantees about when this procedure happens. Indeed, the algorithm employed by the CLR garbage collector means that it may be a long time after the application has finished with the object. This lack of certainty is often termed 'non-deterministic finalization', and it means that C# destructors are not suitable for releasing scarce resources such as database connections, file HANDLES etc.

To achieve deterministic destruction, a CLASS must offer a method to be used for the purpose. The standard approach is for the class to implement the IDisposable INTERFACE. The USER of the object must call the Dispose() method when it has finished with the object. C# offers the 'using' construct to make this easier.

No. They look the same but they are very different. The C# destructor syntax (with the familiar ~ character) is just syntactic sugar for an override of the System.Object Finalize method. This Finalize method is called by the garbage collector when it determines that an object is no longer referenced, before it frees the memory associated with the object. So far this sounds like a C++ destructor. The difference is that the garbage collector makes no guarantees about when this procedure happens. Indeed, the algorithm employed by the CLR garbage collector means that it may be a long time after the application has finished with the object. This lack of certainty is often termed 'non-deterministic finalization', and it means that C# destructors are not suitable for releasing scarce resources such as database connections, file handles etc.

To achieve deterministic destruction, a class must offer a method to be used for the purpose. The standard approach is for the class to implement the IDisposable interface. The user of the object must call the Dispose() method when it has finished with the object. C# offers the 'using' construct to make this easier.

304.

Are C# Constructors The Same As C++ Constructors?

Answer»

Very similar, but there are some significant DIFFERENCES. First, C# SUPPORTS constructor chaining. This means one constructor can call another:
class Person
{
public Person( string name, int age ) { ... }
public Person( string name ) : this( name, 0 ) {}
public Person() : this( "", 0 ) {}
}

Another difference is that virtual method calls within a constructor are routed to the most derived implementation - SEE Can I Call a virtual method from a constructor.

Error handling is also somewhat different. If an exception occurs during construction of a C# object, the destuctor (finalizer) will still be called. This is unlike C++ where the destructor is not called if construction is not completed. (Thanks to Jon Jagger for pointing this out.)

Finally, C# has STATIC CONSTRUCTORS. The static constructor for a class runs before the first instance of the class is created.

Also note that (like C++) some C# developers prefer the factory method pattern over constructors. See Brad Wilson's article.

Very similar, but there are some significant differences. First, C# supports constructor chaining. This means one constructor can call another:
class Person
{
public Person( string name, int age ) { ... }
public Person( string name ) : this( name, 0 ) {}
public Person() : this( "", 0 ) {}
}

Another difference is that virtual method calls within a constructor are routed to the most derived implementation - see Can I Call a virtual method from a constructor.

Error handling is also somewhat different. If an exception occurs during construction of a C# object, the destuctor (finalizer) will still be called. This is unlike C++ where the destructor is not called if construction is not completed. (Thanks to Jon Jagger for pointing this out.)

Finally, C# has static constructors. The static constructor for a class runs before the first instance of the class is created.

Also note that (like C++) some C# developers prefer the factory method pattern over constructors. See Brad Wilson's article.

305.

Is A C# Interface The Same As A C++ Abstract Class?

Answer»

No, not QUITE. An abstract CLASS in C++ cannot be instantiated, but it can (and often does) contain
implementation code and/or data MEMBERS. A C# INTERFACE cannot contain any implementation code or data members - it is simply a group of method names & signatures. A C# interface is more like a COM interface than a C++ abstract class.

No, not quite. An abstract class in C++ cannot be instantiated, but it can (and often does) contain
implementation code and/or data members. A C# interface cannot contain any implementation code or data members - it is simply a group of method names & signatures. A C# interface is more like a COM interface than a C++ abstract class.

306.

Does C# Support Multiple Inheritance (mi)?

Answer»

No, though it does SUPPORT IMPLEMENTATION of multiple INTERFACES on a SINGLE class or struct.

No, though it does support implementation of multiple interfaces on a single class or struct.

307.

Structs Are Largely Redundant In C++. Why Does C# Have Them?

Answer»

In C++, a struct and a CLASS are pretty much the same thing. The only difference is the default visibility level (PUBLIC for structs, private for CLASSES). However, in C# structs and classes are very DIFFERENT. In C#, structs are value types (INSTANCES stored directly on the stack, or inline within heap-based objects), whereas classes are reference types (instances stored on the heap, accessed indirectly via a reference). Also structs cannot inherit from structs or classes, though they can implement interfaces. Structs cannot have destructors. A C# struct is much more like a C struct than a C++ struct.

In C++, a struct and a class are pretty much the same thing. The only difference is the default visibility level (public for structs, private for classes). However, in C# structs and classes are very different. In C#, structs are value types (instances stored directly on the stack, or inline within heap-based objects), whereas classes are reference types (instances stored on the heap, accessed indirectly via a reference). Also structs cannot inherit from structs or classes, though they can implement interfaces. Structs cannot have destructors. A C# struct is much more like a C struct than a C++ struct.

308.

Are C# References The Same As C++ References?

Answer»

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 );
}

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 );
}

309.

Okay, So An Int Is A Value Type, And A Class Is A Reference Type. How Can Int Be Derived From Object?

Answer»

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

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

310.

What Are The Fundamental Differences Between Value Types And Reference Types?

Answer»

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!

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!

311.

Is It True That All C# Types Derive From A Common Base Class?

Answer»

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.

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.

312.

What Standard Types Does C# Use?

Answer»

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

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

313.

Does C# Have Its Own Class Library?

Answer»

Not EXACTLY. The .NET Framework has a COMPREHENSIVE CLASS library, which C# can make use of. C# does not 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.

314.

Does C# Replace C++?

Answer»

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.

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.

315.

How Do I Develop C# Apps?

Answer»

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.

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.

316.

What Is C#?

Answer»

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++."

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++."

317.

What's The Data Provider Name To Connect To Access Database?

Answer»

Microsoft.Access.

Microsoft.Access.

318.

What Does The Parameter Initial Catalog Define Inside Connection String?

Answer»

The DATABASE NAME to CONNECT to.

The database name to connect to.

319.

Which One Is Trusted And Which One Is Untrusted?

Answer»

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.

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.

320.

What's The Role Of The Datareader Class In Ado.net Connections?

Answer»

It RETURNS a read-only DATASET from the data SOURCE when the command is EXECUTED.

It returns a read-only dataset from the data source when the command is executed.

321.

Explain The Three Services Model (three-tier Application).

Answer»

PRESENTATION (UI), business (LOGIC and underlying code) and DATA (from storage or other SOURCES).

Presentation (UI), business (logic and underlying code) and data (from storage or other sources).

322.

Why Are There Five Tracing Levels In System.diagnostics.traceswitcher?

Answer»

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.

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.

323.

What's The Difference Between The Debug Class And Trace Class? Documentation Looks The Same.

Answer»

USE DEBUG CLASS for debug builds, use Trace class for both debug and RELEASE builds.

Use Debug class for debug builds, use Trace class for both debug and release builds.

324.

What Does Assert() Do?

Answer»

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.

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.

325.

Is Xml Case-sensitive?

Answer»

YES, so &LT;Student> and <student> are DIFFERENT ELEMENTS.

Yes, so <Student> and <student> are different elements.

326.

What's The Difference Between &lt;c&gt; And &lt;code&gt; Xml Documentation Tag?

Answer»

SINGLE LINE CODE EXAMPLE and multiple-line code example.

Single line code example and multiple-line code example.

327.

What's The Difference Between // Comments, /* */ Comments And /// Comments?

Answer»

Single-line, multi-line and XML DOCUMENTATION COMMENTS.

Single-line, multi-line and XML documentation comments.

328.

What Namespaces Are Necessary To Create A Localized Application?

Answer»

System.Globalization, System.Resources.

System.Globalization, System.Resources.

329.

What's A Satellite Assembly?

Answer»

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.

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.

330.

How's The Dll Hell Problem Solved In .net?

Answer»

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.

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.

331.

What's A Multicast Delegate?

Answer»

It’s a DELEGATE that points to and eventually fires off SEVERAL METHODS.

It’s a delegate that points to and eventually fires off several methods.

332.

What's A Delegate?

Answer»

A DELEGATE OBJECT encapsulates a reference to a method. In C++ they were referred to as FUNCTION POINTERS.

A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

333.

What's The C# Equivalent Of C++ Catch (....), Which Was A Catch-all Statement For Any Possible Exception?

Answer»

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

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

334.

Will Finally Block Get Executed If The Exception Had Not Occurred?

Answer»

Yes.

Yes.

335.

What's Class Sortedlist Underneath?

Answer»

A SORTED HASHTABLE.

A sorted HashTable.

336.

What's The .net Datatype That Allows The Retrieval Of Data By A Unique Key?

Answer»

HashTable.

HashTable.

337.

How Can You Sort The Elements Of The Array In Descending Order?

Answer»

By CALLING SORT() and then REVERSE() METHODS.

By calling Sort() and then Reverse() methods.

338.

What's The Difference Between The System.array.copyto() And System.array.clone()?

Answer»

The FIRST ONE performs a deep COPY of the array, the second one is SHALLOW.

The first one performs a deep copy of the array, the second one is shallow.

339.

What's The Advantage Of Using System.text.stringbuilder Over System.string?

Answer»

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.

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.