Wednesday 29 October 2014

.net interview questions

What is an assembly? 

It is the building blocks of the .NET framework. They are the logical grouping of the functionality in a physical file. 

What are different types of Assemblies? 

Single file and multi file assembly. Assemblies can be static or dynamic. Private assemblies and shared assemblies

what are the advantages of an assembly?

It increases the performance.
Better code management and encapsulation.
It also introduces the n-tier concepts and business logic.

What is the purpose of an Assembly?

It handles many things in an application. They handle versioning, type and class scope, security permissions, as well as
other metadata including references to other assemblies and resources.
The rules described in an assembly are enforced at runtime.

What a static assembly consist of in general?

In general, a static assembly consists of the following four elements:

Assembly Manifest, which contains the assembly metadata.
Type Metadata.
MSIL code that implements the types.
A set of resources.

From above all only the manifest is required, however the other types and resources add the additional functionality to the assembly.

How to view an assembly?

We can use the tool "ildasm.exe" known as "Assembly Disassembler" to view the assembly.

What are the satellite assemblies?

In a multilingual or multi-cultural application, the localized assemblies, which contain language specific instructions that modify the core application, 
are kept separately and they are called satellite assemblies.


Which tool is used to deploy an assembly, so as the assembly is available to all the application?

The GacUtil.exe is the tool, which allows us to add any assembly to the windows GAC (Global Assembly Catche).

What is an assembly?

Precompiled code that can be executed by the .NET runtime environment. It can be an .exe or .DLL

what are different kind of assemblies? What are satellite assembly and public assembly?

There are three types of assemblies:
Public Assembly: DLL which can be used by multiple applications at a time. It is generally stored in GAC (Global Assembly Cache). 
Private Assembly: It is used by only one application and generally stored in the Application specific folder.
Satellite Assembly: A Satellite Assembly contains only static objects such as images and other non-executable files such as resource-only files that are required by the application.

Can we have 2 assemblies with the same name in GAC?

Yes we can have 2 assemblies with the same name in GAC as long as they have different strong names. The strong names of the assemblies can differ in name/version/culture/processor architecture/public key token.

What is the prerequisite to deploy an assembly in GAC?

The assembly must be strong named. A 'strongly named' assembly is an assembly that is signed with a key to ensure its uniqueness

what is Strong Name?

A strong name is a combination of its name, version number, and culture information (if provided) and a public key and a digital signature. This ensures that the DLL is unique.

A strong name provides an integrity check that ensures that the contents of the assembly have not been changed since it was built.

What are indexers?

Indexers are used for treating an object as an array. 



What is the difference between abstract classes and interfaces?

Interface must have only methods without implementation whereas abstract classes can have methods with and without implementation that is you can define a behavior in an abstract class. An interface cannot contain access modifiers whereas an abstract class can contain access modifiers. A class can implement multiple interfaces whereas a class can inherit only one abstract class. An interface cannot have variables whereas an abstract class can have variables and constants defined. When we add a new method to an interface, we have to track all the classes which implement this interface and add the functionality to this method whereas in case of abstract class we can add default behavior for the newly added method and thus the classes which inherit this class works properly.

What are generics? How do you increase performance using generics?

Generic allows you to define type-safe data structures, without specifying the actual data types. This increases the performance of the code and code reuse. Thus generics allow you to write a class or method that can work with any data type. Since Generics is type safe it avoids boxing and unboxing which increases the performance of the code because you need not convert an object to a specific data type and vice versa.

How do you compare two dictionary objects?

You can use the SequenceEqual method of the dictionary object to compare 2 dictionaries as shown below:
bool result = dic1.SequenceEqual(dic2);
Here result will be true if the two dictionary objects are equal else false.

Have you heard about Icomparable? How does it work?

A Class can implement IComparable interface if you want to compare the objects of that class. Then in that class we have to implement the CompareTo method and provide the implementation to compare two class objects.


What is the difference between ref and out keyword? Is out a Reference variable?

Ref is initialized before entering the function which is called, whereas out variable is initialized inside the function. The out keyword used for a variable causes it to be passed by reference. It is used when a method has to return multiple values (one using return statement other using out keyword).

What is Managed code?

Managed code is the code whose execution is managed by the .NET Framework Common Language Runtime (CLR). The CLR ensures managed execution environment that ensures type safety, exception handling, garbage collection and array bound & index checking.

What is Unmanaged Code?

Applications that do not run under the control of the CLR are said to be unmanaged. This is the code that is directly executed by the operating system.

How unmanaged code is taken care by .Net Framework?

In unmanaged code the memory allocation, type safety, security and other things have to be taken care by the developer. This can lead to memory leaks and buffer overrun.

What is Dispose and Finalize?

The Dispose method is generally called by the code that created your class so that we can clean up and releases any resources we have acquired (database connections/file handles) once the code is done with the object. The finalize method is called when the object is garbage collected and it’s not guaranteed when this will happen.

ASP.NET Page Lifecycle events?

Page_PreInit,Page_Init,LoadViewState(If it is a Postback),LoadPostBackData(If it is a Postback),Page_Load,Control event handler execution,Page_PreRender,SaveViewState,Page_Render,Page_Unload

In an ASP.NET page if you have a DropdownList and whenever you select some option in the dropdown list? It makes a post back to server. How can you increase the performance of this page?

put the dropdownlist inside an AJAX UpdatePanel so it updates only that part of the page instead of a full page reload or we can also use AJAX webservice.

Have you used Caching in asp.net?

Yes

What are difference Session Management Techniques in ASP.NET

Sessions,Cookies,QueryString,ViewState,Application objects,Hidden Fields.

Difference between Internal and Protected access modifiers.

A protected type or member can be accessed by code in the same class or in a derived class whereas an internal type or member can be accessed by code in the same assembly.


What is Difference between ReadOnly and Const.?

A Const can be initialized only once whereas ReadOnly can be initialized at runtime in the constructor.

Constants are static by default that is they are compile time constants
whereas Readonly are runtime constants.

Constants must have a value at compilation-time and they are copied into every assembly that uses them.

Readonly instance fields must be set before the constructor exits because they are evaluated when object is created.

Web Services Questions:

How do you mark a service method as webservice method?

Decorate the method with the WebMethod attribute as shown below:
[System.Web.Services.WebMethod()]
public string GetData(string message)
{
return "My Service";
}

Does C# support multiple-inheritance? 
No.

Who is a protected class-level variable available to? 

It is available to any sub-class (a class inheriting this class).

Are private class-level variables inherited? 

Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited. 

Describe the accessibility modifier “protected internal”. 

It is available to classes that are within the same assembly and derived from the specified base class. 

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

System.Object. 


What does the term immutable mean?

The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory. 

What’s the difference between System.String and System.Text.StringBuilder classes?

System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed. 

What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.

Can you store multiple data types in System.Array?

No. 

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identical object.

How can you sort the elements of the array in descending order?

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

What’s the .NET collection class that allows an element to be accessed using a unique key?

HashTable. 

What class is underneath the SortedList class?

A sorted Hash Table. 

Will the finally block get executed if an exception has not occurred?­

Yes. 

What’s the C# syntax to catch 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 be executed for a single try statement?

No. Once the proper catch block processed, control is transferred to the finally block (if there are any). 

Explain the three services model commonly known as a three-tier application.

Presentation (UI),
Business (logic and underlying code) and Data (from storage or other sources). 

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. 

What’s an abstract class?

A Class that can’t be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation. 

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

1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden. 
2. When at least one of the methods in the class is abstract. 

What is an interface class?

Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. 



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 happens if you inherit multiple interfaces and they have conflicting method names?

It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay. 


What’s the difference between an interface and abstract class?

In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers. 

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. 

Method and Property Questions 

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. 

What does the keyword “virtual” declare for a method or property? 

The method or property can be overridden. 

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. 

Can you declare an override method to be static if the original method is not static? 

No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override) 


What are the different ways a method can be overloaded? 

Different parameter data types, different number of parameters, different order of parameters. 

If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific base constructor?
Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

Events and Delegates

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

What’s a multicast delegate? 

A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.

XML Documentation Questions 

Is XML case-sensitive? 

Yes. 

What’s the difference between // comments, /* */ comments and /// comments? 

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

How do you generate documentation from the C# file commented properly with a command-line compiler? 

Compile it with the /doc switch.

Debugging and Testing Questions 

What debugging tools come with the .NET SDK?

1. CorDBG – command-line debugger. To use CorDbg, you must compile the original C# file using the /debug switch. 
2. DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR. 

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. 

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. 

Why are there five tracing levels in System.Diagnostics.TraceSwitcher? 

The tracing dumps can be quite verbose. For applications that are constantly running you run the risk of overloading the machine and the hard drive. Five levels range from None to Verbose, allowing you 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. 

How do you debug an ASP.NET Web application? 

Attach the aspnet_wp.exe process to the DbgClr debugger. 

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. 

ADO.NET and Database Questions 

What is the role of the DataReader class in ADO.NET connections? 

It returns a read-only, forward-only rowset from the data source. A DataReader provides fast access when a forward-only sequential read is needed. 

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. OLE-DB.NET is a .NET layer on top of the OLE layer, so it’s not as fastest and efficient as SqlServer.NET. 


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%’. 

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 had been committed even if the system crashes right after. 

What connections does Microsoft SQL Server support? 

Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and password). 

Between Windows Authentication and SQL Server Authentication, 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. 

What does the Initial Catalog parameter define in the connection string? 

The database name to connect to. 


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.

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 are the ways to deploy an assembly? 

An MSI installer, a CAB archive, and XCOPY command. 

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. 

What namespaces are necessary to create a localized application? 

System.Globalization and System.Resources.

What is the smallest unit of execution in .NET?

an Assembly.

When should you call the garbage collector in .NET?

As a good rule, you should not call the garbage collector. However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory. However, this is usually not a good practice.

How do you convert a value-type to a reference-type?

Use Boxing.

What happens in memory when your Box and Unbox a value-type?

Boxing converts a value-type to a reference-type, thus storing the object on the heap. Unboxing converts a reference-type to a value-type, thus storing the value on the stack.

SQL SERVER

What is Difference between stored procedure and function?

A Stored Procedure can return zero or more values whereas function must return a value.
Stored Procedures can have both input and output parameters whereas functions can have only input parameters. 
Stored Procedure allows select and Insert, Update, Delete statements in it whereas function can have only select statement in it. 
Functions can be called from Stored Procedure whereas procedures cannot be called from a function. 

WCF and Web Services

What is Difference between WCF and Web Services?

Web services can only use HTTP/HTTPS protocol. We cannot use TCP/MSMQ protocols with it.
WCF can use HTTP/TCP/MSMQ/Named Pipes.
Web service has to be hosted in IIS whereas WCF service can be self-hosted or hosted in IIS/WAS/windows Service.
WCF uses DataContract Serialization whereas WebService uses XML serialization.

How do we know that which address the client is connecting to in case of a WCF Service?

Using endpoint which has address where the WCF service is located.

C#

What is Difference between Structures and Classes?

Classes are Reference types and Structures are Values types.
Classes will support an Inheritance whereas Structures won’t.
We cannot have instance Field initializers in structs but classes can have instance Field initializers.
Classes can have constructors but structs do not have default constructors

does Structure implement Inheritance/Interface?

There is no inheritance concept for structs but a struct can implement interfaces.

Can Structures have constructors? How the members of the structures are initialized.
Structures don’t have default constructors. Whereas parameterized constructors are allowed.

What is Difference between Value Types and Referenec Types.

A variable that is declared pf value type(int,double...), stores the data, while a variable of a reference type stores a reference to the data.
Value type is stored on stack whereas a reference type is stored on heap.
Value type has its own copy of data whereas reference type stores a reference to the data.
 
When integer variables are declared in class. Is this integer variable a value type or a reference type.

It is a value type but the memory is allocated on heap.

What is Difference between ref keyword and out keyword?

Ref keyword tells the compiler that the object is initialized before it enters the function, whereas the out keyword tells the compiler that the object will be initialized inside the function. 

Is there any difference when you pass a value type using a ref keyword and a reference type using a ref keyword?

No Difference because when you pass any value type or a reference type to a method by reference using the 'ref' keyword, it will pass the reference to the actual data. There is no copying of the value.

What is Difference between readonly and const keywords? Can you use these keywords with reference types?


The value of const is set at compile time and can't change at runtime 
Const are static by default. 
The readonly keyword marks the field as unchangeable. However it can be changed inside the constructor of the class 


0 comments:

Post a Comment