ASP.NET Interview Questions and Answers (1544) - Page 40

How to use Exception Handling?

What is Exception?
An Exception is an unexpected error or problem.
What is Exception Handling?
It is a method to handle the error and solution to recover from it which makes the program to work smoothly.

What is try Block?
The try block consists of the code that might generate an error.
This try block must be associated with one or more catch blocks or by finally block.
The try block may not have a catch block associated with it every time but in this case,it must have a finally block associated with it instead of catch block.

Example:

Format1


try
{
//Code that might generate error
}
catch(Exception error)
{
}

Format2

try
{
//Code that might generate error
}
catch(ExceptionA error)
{
}
catch(ExceptionB error)
{
}

Format3

try
{
//Code that might generate error
}
finally
{
}


What is catch Block?
catch block is used to recover from error generated in the try block.
In case of multiple catch blocks,only the first matching catch block is excecuted.
you need to arrange the multiple catch blocks from specific exception type to more generic type.
If none of the matching catch blocks are able to handle the exception, the default behaviour of web page is to terminate the processing of the web page.

Example:

Format1


try
{
//Code that might generate error
}
catch(Exception error)
{
//Code that handle errors occurred in try block
}

Format2:

try
{
//Code that might generate error
}
catch(DivideByZeroException error)
{
//Code that handle errors occurred in try block
//It is the most specific error we are trying to catch
}
catch(Exception error)
{
//Code that handle errors occurred in try block
//It is not specific error we are catching
}
catch
{
//Code that handle errors occurred in try block
//It is the least specific error in hierarchy
}


What is finally Block?
The finally block contains the code that executes,whether an exception occurs or not.
The finally block is used to write code to close files,database connections,etc.
Only one finally block is associated with each try block.
finally block must appear only after the catch block.
If there are any control statements such as goto,break or continue in either try or catch block,the transfer happens only after the code in the finally block is excecuted.
If the control statements are used in finally block,there occurs a compile time error.

Example:

Format1


try
{
//Code that might generate error
}
catch(Exception error)
{
//Code that handle errors occurred in try block
}
finally
{
//Code to dispose all allocated resources
}

Format2

try
{
//Code that might generate error
}
finally
{
//Code to dispose all allocated resources
}

What is DLL Hell?

DLL Hell:

DLL Hell refers to the set of problems while sharing multiple applications under a single common component like Dynamic Link Library(DLL) or a Component Object Model(COM) class.
Simply, it is the problem which occurs while registering the DLL components with a common name.
DOT Net has removed this problem by introducing the concept of versioning.

Versioning:

Versioning is a process where, every shared application creates its own version number.

For Example, Let us assume that the version number of the first registered DLL takes as V1.0.
And Now, you want to overwrite the first registered DLL by installing another DLL with same name.
Then the version number of the second registered DLL would be taken as V2.0.

By this way we can avoid the conflicts of DLL registration and can overwrite the registered DLLs successfully.
What is the difference between Abstract Class and Interface?

Abstract Class:

a) An abstract class is a special kind of class that cannot be instantiated.
b) It allows the other classes to inherit from it but cannot be instantiated.
c) It is used to carry the same hierarchy or standards for all the subclasses.


Interface:

a) An interface is not a class,but it is an entity and has no implementation.
b) It has only the definition of the methods without the body.


Differences:

1) A class may inherit several interfaces, but inherit only one abstract class.
2) An abstract class cn provide complete code, but interface provides just the signature.
3) An abstract class can contain access modifiers for the subs, functions, properties, but an interface cannot.
4) An abstract class defines the core identity of a class, but interfaces are used to define the peripheral abilities of a class.
5) An abstract class is fast whereas the interfaces requires more time to find the actual method in the corresponding classes.
6) If we add a new method to an abstract class, there we can provide the default implementation and therefore all the existing code might work properly whereas in interface, we have to track down all the implementations of the interface and define implementation for the new method.
7) In Interface, we cannot define the fields whereas in an Abstract class, we can define the fields and constants.
Can we implement caching with PDF files?

Say our PDF files are on the disk then we should let the IIS to handle it. The reason being the caching done through IIS is much faster than ASP.NET cache.

In IIS the caching done through IIS static file handler.
How can we clear total cache of a give page?

At times we need to make sure that all the cache which was used must be cleared.

This can be done in many ways here is one of the way using the dictionary object

foreach(DictionaryEntry objClearItem in Cache)   

{
//Response.Write (objClearItem .Key.ToString ());
Cache.Remove(objClearItem .Key.ToString () ) ;
}

Can we access from a compiled class?

Yes we can access cache from a compiled class. Here is the code to it.

MyVariable=System.Web.HttpContext.Current.Cache("<CacheName>");  

</CacheName>

What is the use of AutoPostBack ?

AutoPostBack is a feature available that is available on few controls.
The main purpose of this feature is that, if there is any change done on the client side, then it should be handled on the server side.

Usage:

<asp:TextBox AutoPostBack="TRUE|FALSE" runat="server"/> 


Example:

<form runat="server">

<asp:TextBox id="txtBox1" runat="server" AutoPostBack="TRUE" />
</form>

What is the difference between Server.Transfer and Response.Redirect ?

Both are objects of ASP.Net and are used to transfer user from one page to another page.

Syntax:

Response.Redirect("Default.aspx");


Server.Transfer("Default.aspx");


Server.Transfer() can directly access the values, controls and properties of the previous page whereas you cannot do with Response.Redirect().
Explain about Authentication and Authorization ?

Authentication:

Authentication is the process of verifying the credentials such as username and password of the user and then allows that user to access the server.
This process can be done in many ways like :

Password based authentication

Device based authentication

Biometric authentication

For Example, if you use

Windows based Authentication and are browsing an ASP.NET page from server -- ASP.NET/IIS would automatically use NTLM to authenticate you as user1.

Forms based Authentication, then you would use an html based forms page to enter username/password -- which would then check a database and authenticate you against the username/password in the database.


Authorization:

Authorization is a process of verifying whether the user has got the permission to do the operation that he is requesting.
What is difference between Web site and Web application ?

Both function and perform similarly, but still differ in following ways.

Web application:

a) We can't include c# and vb page in single web application.
b) We can set up dependencies between multiple projects.
c) Can not edit individual files after deployment without recompiling.
d) Right choice for enterprise environments where multiple developers work unitedly for creating,testing and deployment.


Web site:

a) Can mix vb and c# page in single website.
b) Can not establish dependencies.
c) Edit individual files after deployment.
d) Right choice when one developer will responsible for creating and managing entire website.
What is the default mapping logic used by ASP.NET MVC ?

The default mapping logic used by ASP.NET MVC is as follows :-

/[Controller]/[ActionName]/[Parameters]


For example,
controller = "Home", action = "Index", id = UrlParameter



Thanks and Regards
Akiii
Which is the default method that will be called on a controller if one is not explicitly specified in Asp.Net MVC ?

NOTE: This is objective type question, Please click question title for correct answer.
What is Caching and what are different types of caching?

caching is generally used to catch frequently accessed data.
While using caching, you have to consider some of the parameters like parameter, time, etc.

Types of Caching:

1)Page Level Caching (called Output Caching) - Used to fetch page level data.

2)Page Fragment Caching (called Partial-Page Output Caching) - Used to catch the information of a structure level.

3)Programmatic or Data Caching (called Application Caching) - Used to fetch tha information of an Application.
What is CORS in asp.net web API ?

CORS (Cross-Origin Resource Sharing) is a new specification which allow exchanged resources between the client and the server from different domain.

CORS is supported by most modern web browsers ( Firefox 3.5 and above, Safari 4 and above, Chrome 3 and above, IE 10 and above)

It allow the server to relax the cross-domain restrictions for all HTTP verbs, not only GET.

It removes limitation of JSONP (only support GET verb).
How can we create custom controls in ASP .NET ?

We can create user controls using .ASCX in ASP .NET.
Once .ASCX file is created, the two things to be done inprder to use ASCX in your project are:

1. Register the ASCX control in page using <%@ Register directive%> tag.

Example:

<%@ Register tagprefix="Accounting" Tagname="footer" Src="Footer.ascx" %>


2. Now to use the above accounting footer in the page tou may use the below directive.

<Accounting:footer runat="server">

What is the use of "GLOBAL.ASAX" file ?

It is also known as ASP .NET application file.
The uses of GLOBAL.ASAX are:

1. There can be only one file per application which we cannot create another file with same name in another location.
2. It is used to set the variables or other global settings.
3. It is parsed and dynamically compiled by ASP.NET.
4. External users cannot download or view the code written in it.
What is the difference between "Web.Config" and "Machine.Config" ?

"Web.Config" files apply changes to each web application.
"Machine.Config" files apply settings to all ASP.NET applications.

You can override settings in the Machine.Config file for all the applications in a particular Web site by placing a Web.Config file in the root directory of the Web site as follows:

\InetPub\wwwroot\Web.Config
What is the method to customize columns in DataGrid ?

We can use the Template Column for customizing columns in DataGrid.

Example:

<asp:TemplateField HeaderText="Birth Date">

<ItemTemplate>
<asp:Label ID="BirthDateLabel" Runat="Server" Text='<%# Eval("BirthDate", "{0:d}") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Calendar ID="EditBirthDateCalendar" Runat="Server"
VisibleDate='<%# Eval("BirthDate") %>' SelectedDate='<%# Bind("BirthDate") %>' />
</EditItemTemplate>
</asp:TemplateField>

How can we format data inside the DataGrid ?

We can use DataFormatString Property to format the data inside the DataGrid.

If the DataFormatString property is not set, the field's value is displayed without any special formatting.

Example:

<Columns>  

<asp:BoundField HeaderText="Product ID" DataField="ProductID" />
<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" DataFormatString="{0:F0}" />
</Columns>

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories