Latest C# Interview Question Answers Collection.
Collection of some of the common question expected in a .NET interview.
Is multiple inheritance supported in C#.NET?In C#.NET, multiple inheritance is not supported, however to accommodate it in a different way you can use interfaces.
Can you tell what is the implicit name of the parameter that gets passed into the class set method?
The implicit name of the parameter is Value, and its datatype depends on whatever variable we would be changing.
Explain in brief how can you inherit from a class in C#.NET?
You can place a colon and following it the name of the base class. It is double colon in C++ when you inherit.
To inherit a protected class-level variable in C#, where it is made available?
It is made available to Classes in the same namespace.
Can you tell me if private class-level variables be inherited in C#?
Technically, private class-level variables can be inherited, but they are not accessible, hence you can say that they are not inherited.
Explain in brief the accessibility modifier protected internal.
Protected internal is a accessibility modifier that is madeavailable to derived classes and classes within the same Assembly as also from the C# base class.
Explain in brief how method overriding different from method overloading?
When you do overriding, you change the method behavior for a derived class. Whereas when you do overloading, it simply involves having a method with the same name within the class.
In C#, what is the top .NET class that everything is derived from?
The top .NET class is System.Object from which everything is derived.
In C#, what does the keyword virtual mean in the method definition?
When you use the virtual keyword, it specified that the method can be over-ridden.
In C#, can you override private virtual methods?
We cannot override private virtual methods. Again, you cannot access private methods in inherited classes, it have to be protected in the base class to allow any sort of access.
In C# can you prevent your class from being inherited and becoming a base class for some other classes?
Yes we can prevent it, we use the keyword sealed in the class definition. The developer while he is trying to derive from a class will get a message: cannot inherit from Sealed class WhateverBaseClassName.This concept is analogous to final class in Java.
Explain in brief how can you allow class to be inherited, but prevent the method from being over-ridden?
We just need to leave the class public and make the method sealed to prevent the method from being overridden.
We just need to leave the class public and make the method sealed to prevent the method from being overridden.
Explain in brief what is an abstract class in C#.NET?
A class in C# cannot be instantiated. There is a concept in C++ which is known as pure virtual method. A class that must be inherited and have the methods over-ridden. Hence, an abstract class is a blueprint for a class without any implementation.
Under what circumstances you absolutely have to declare a class as abstract?
If there is at least one of the methods in the class which is abstract. In cases when the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
What do you mean by a interface class in C#?
A interface class is nothing but an abstract class with public abstract methods all of which must be implemented in the inherited classes.
Why you cannot specify the accessibility modifier for methods inside the C# interface?
They all must be declared public. Hence, you are not allowed to specify any accessibility, it is public by default.
Can you inherit multiple interfaces in C#? What if they have conflicting method names?
Yes, we can inherit multiple inheritance in C#
It depends on the programmer to implement the method inside his own class, so implementation is left entirely up to the coder. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data
It depends on the programmer to implement the method inside his own class, so implementation is left entirely up to the coder. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data
Explain in brief what is the difference between an interface and abstract class?
In the interface class, its a necessary condition that all methods must be abstract; in the abstract class some methods can also be concrete. In the interface class no accessibility modifiers are allowed, whereas in abstract classes, they are allowed.
Can you tell me how can you overload a method?
We can overload by using different parameter data types, different number of parameters, different order of parameters.
Consider a scenario: If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?
Yes, we just need to place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.
Differentiate between System.String and System.StringBuilder classes?
The class System.String is immutable whereas System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed on it.
Explain what is the main advantage of using System.Text.StringBuilder over System.String?
The main advantage is that StringBuilder is more efficient in cases, where a lot of manipulation is done to the text. We know that Strings are immutable, hence each time it is being operated on, a new instance is created for it.
Can you tell me if we can store multiple data types in System.Array?
No, we cannot store.
No, we cannot store.
Differentiate between the System.Array.CopyTo() and System.Array.Clone()?
System.Array.CopyTo() is used to perform a deep copy of the array whereas System.Array.Clone() is shallow.
What will you do to sort the elements of the array in descending order?
To sort the elements in descending order, we have to call Sort() and then Reverse() methods.
Name the .NET datatype that allows the retrieval of data by a unique key?
The datatype is HashTable.
What is class SortedList underneath?
A sorted HashTable.
In a structured exception handling, will finally block get executed if the exception had not occurred? Yes, the finally block will always get executed.
What is 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 {}.
Can multiple catch blocks inside a program be executed?
Multiple catch cannot be executed. 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.
Multiple catch cannot be executed. 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.
Why is it a not a good practice to throw your own exceptions in C#?
When 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. When you trow your own exceptions, it signifies some design flaws in the C# project.
What do you mean by a delegate in C#?
A delegate is nothing but a object encapsulates a reference to a method. In C++ they were referred to as function pointers.
What do you mean by a multicast delegate in C#?
Multicast delegate a delegate that points to and eventually fires off several methods.
Can you tell me how the DLL Hell problem is solved in .NET?
By using the concept of Assembly versioning which 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.
Tell us the ways to deploy an assembly in C#?
It can be done in three simple steps - An MSI installer, a CAB archive, and XCOPY command.
What do you mean by a satellite assembly in C#?
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.
Name the two namespaces that are necessary to create a localized application in C#?
The tow are System.Globalization and System.Resources.
Differentiate between // comments, /* */ comments and /// comments?
Single-line, multi-line and XML documentation comments.
How can you generate documentation from the C# file commented properly with a command-line compiler?
Compile it with a /doc switch.
Can you tell me if XML is case-sensitive or not?
Yes, so and are different elements.
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.
Where is the output of TextWriterTraceListener redirected?
To the Console or a text file depending on the parameter passed to the constructor.
Can you tell me how do you debug an ASP.NET Web application?
For debugging a ASP.NET web application you have to attach the aspnet_wp.exe process to the DbgClr debugger.
Name and explain in brief the 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).
Negative test cases (broken or missing data, proper handling)
Exception test cases (exceptions are thrown and caught properly).
Can you change the value of a variable while debugging a C# application?
Yes you can change, if you are debugging via Visual Studio.NET, just go to Immediate window.
Name the three services model (three-tier application).
The three services model are - Presentation (UI), business (logic and underlying code) and data (from storage or other sources).
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.
Can you tell me 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.
What is the use of the wildcard character in SQL?
The wildcard character is %, the proper query with LIKE would involve ‘La%’.
Explain in detail the ACID rule of thumb for transactions.
Transaction must be Atomic i.e it is one unit of work and does not dependent on previous and subsequent transactions,
Transactions must be Consistent i.e data is either committed or roll back, no “in-between” case where something has been updated and something has not.
Transactions must be Isolated i.e no transaction sees the intermediate results of the current transaction
Transactions must be Durable i.e the values persist if the data had been committed even if the system crashes right after
Transactions must be Consistent i.e data is either committed or roll back, no “in-between” case where something has been updated and something has not.
Transactions must be Isolated i.e no transaction sees the intermediate results of the current transaction
Transactions must be Durable i.e the values persist if the data had been committed even if the system crashes right after
What are the connections does Microsoft SQL Server use to support?
MS SQL Server supports Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and passwords).
Can you tell me which one is trusted and which one is untrusted connection?
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.
Under what circumstances would you use a untrusted verificaion?
Web Services might use it, as well as non-Windows applications.
What is the use of the parameter Initial Catalog defined inside Connection String?
It specifies the database name to connect to.
Name the data provider that is used to connect to Access database?
The data provider is Microsoft.Access.
What is the significance of the Dispose method in the connection object?
The dispose method deletes the object from the memory.
Explain in brief what is a pre-requisite for connection pooling?
For connection pooling, multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.
Related Posts
No comments :