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

What is the main use of a finally block in exception handling ?

Finally block is always followed by the Try block.
This is because, it is used to free the resources that are used in the try block.

Example:

using System;
using System.IO;

class FinallyDemo
{
static void Main(string[] args)
{
FileStream outStream = null;
FileStream inStream = null;

try
{
outStream = File.OpenWrite("DestinationFile.txt");
inStream = File.OpenRead("BogusInputFile.txt");
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (outStream != null)
{
outStream.Close();
Console.WriteLine("outStream closed.");
}
if (inStream != null)
{
inStream.Close();
Console.WriteLine("inStream closed.");
}
}
}
}

How to raise an Exception ?

An exception can be raised using th Throw keyword. You have to use this try statement within your exception-handling structure, which passes the control flow to the catch statement when an exception occurs.

Example:


using System;
class HandledException
{
public static void Main()
{
int x = 0;
int intTemp = 0;
try
{
intTemp = 100/x;
Console.WriteLine(?Program terminated before this statement?);
}
catch(DivideByZeroException de)
{
Console.WriteLine("Division by Zero occurs");
}
finally
{

Console.WriteLine("Result is {0}", intTemp);
}
}
}

How do you detect the user's language preference on ones computer?

By using the Request object’s UserLanguages property you can return a list of the user’s language preferences. In that displayed UserLanguages, the first element of the array is the user’s current language on his/her computer.
Example:
In your web.config file, you need to set the following globalization tag with the values shown below:

<system.web>
<globalization
uiCulture="auto"
culture="auto"
enableClientBasedCulture="true" />
</system.web>


The above code is enough to have the user use his preferred language with his/her web application.
How do you change the writing direction to "right to left" in a webform ?

You can change the writing direction of a webform from "left to right" to "right to left".
This is done by using the HTML dir attribute as shown below:
<body dir="rtl">

What is the use of HTTP modules ?

HTTP modules are used to implement various application features, such as forms authentication, caching, session state, and client script services.
HTTP modules are executed before and after the handler and are used to provide a method for interacting with the request.
HTTP modules will let you know and examine the incoming requests and take action based on the request.They are also used to let you know and examine the outgoing response and modify it.
Discuss the advantages of using Dependency Injection..

Below mentioned are some of the advantages of using Dependency Injection.

1) Loosely coupled systems can be developed using Dependency Injection.
2) As long as the component implements the interface type, it is easy to swap in a different implementation of a component.
3) The main advantage of this Dependency Injection is that, it allows the objects to be mocked with in the Unit Tests.
Discuss the differences between Named skins and Default skins ?

Here are some of the differences between Named skins and Default skins.

Default Skin: This type of skin will be applied to all the controls which are of same type when a theme is applied to a page. If a control does not have a skinID attribute, then that control skin is a default skin.
Example:
If you create a default skin for a Calendar control, the control skin applies to all Calendar controls on pages that use the theme.

Default skins are matched exactly by control type, so that a Button control skin applies to all Button controls, but not to LinkButton controls or to controls that derive from the Button object.

Named Skin: These type of skins do not apply to the controls automatically by type. This named skin is a control skin with a SkinID property set.By creating named skins, you can allow to set different skins for different instances of the same control in an application.
What is a Flat File and what is the use of it?

Flat File:Is the name given to text, which can be read or written only sequentially.
OR
Collection of data stored and which can be accessed sequentially.
Example: Microsoft Excel Sheet .
Used to get the data in sequential manner.
What is impersonation in ASP.NET?

It is a technique to access application resources using the identity of some other user.

By default, Impersonation is off

To enable impersonation:

<identity impersonate="true" userName="domain name\username" password="password"/>
How can a Theme be applied to a web application ?

There are 3 levels at which a theme can be applied for a web application.
They are discussed as below:
1. At the page level - In this level, you have to use the Theme or StyleSheetTheme attribute of the @ Page directive.

2. At the application level - In this level, a theme can be applied to all pages in an application by setting the <pages> element in the application configuration file.

3. At the web server level - In this level, you have to define the <pages> element in machine.config file. This will apply the theme to all the web applications on that web server.
How to get user's culture at run time ?

Below are the steps to get user's culture at run time.

1. In the first step, you have to get the Request object’s UserLanguages property.
2. Then, you have to use the returned value with the CultureInfo class to create an object representing the user’s current culture.

Example:

The following code gets the user’s culture and displays the English name and the abbreviated name of the culture in a label the first time the page is displayed:

private void Page_Load(object sender, System.EventArgs e)
{
// Run the first time the page is displayed
if (!IsPostBack)
{
// Get the user's preferred language.
string sLang = Request.UserLanguages[0];
// Create a CultureInfo object from it.
CultureInfo CurrentCulture = new CultureInfo(sLang);
lblCulture.Text = CurrentCulture.EnglishName + ": " +
CurrentCulture.Name;
}
}

Discuss the differences between Theme and CSS ?

1. By using Themes, not only style properties can be defined, but also many properties of a control or page can be defined.
For example, using themes, you can specify the graphics for a TreeView control, the template layout of a GridView control, and so on.

2. Graphics can also be included in Themes.

3. Themes will not cascade the way style sheets do. By default, any property values defined in a theme referenced by a page's Theme property override the property values declaratively set on a control, unless you explicitly apply the theme using the StyleSheetTheme property.

4. You cannot apply multiple themes to a page, unlike style sheets where multiple style sheets can be applied. Only one theme can be applied to each page.
While using themes, What are the security concerns to be remembered ?

When Themes are used on your Web site,they may cause security issues. Malicious themes can be used to:

1. You have to alter a control's behavior. So, it does not behave as expected.

2. For posing a cross-site scripting risk, you have to Inject client-side script.

3. Sensitive information hs to be exposed.

4. The mitigations for these common threats are:

5. Protect the global and application theme directories with proper access control settings. Only trusted users should be allowed to write files to the theme directories.

6. Do not use themes from an untrusted source. Always examine any themes from outside your organization for malicious code before using them on you Web site.

7. Do not expose the theme name in query data. Malicious users could use this information to use themes that are unknown to the developer and thereby expose sensitive information.
Advantages of using Master Pages..

1. You can make updates in one place as they allow you to centralize the common functionality of your pages.
2. With the help of Master pages, it is easy to create one set of controls and code and apply the results to a set of pages.
For example, you can use controls on the master page to create a menu that applies to all pages.
3. They are used to give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered.
4. You can provide an object model which allows you to customize the master page from individual content pages.
Explain how the content pages can be attached to Master pages ?

There are 3 levels by which the content pages can be attached to Master pages. Each level is explained briefly as follows.

1) At thye Page Level: To bind a content page to a master page, You can use a page directive in each content page.

2) At the application level: You can specify all the ASP.NET pages (.aspx files) in the application to automatically bind to a master page, by making a setting in the pages element of the application's configuration file (Web.config).

3)At the folder level: In this level, it is like binding at the application level, except that you make the setting in a Web.config file in one folder only. The master-page bindings then apply to the ASP.NET pages in that folder.
How can you dynaimically assign a Master Page ?

You can assign a master page dynamically during the PreInit stage by using the Page class MasterPageFile property as shown in the below code snippet:


void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/MasterPage.master";
}

Is there any way to access controls on the Master Page without using FindControl() method ?

Yes, You can access controls on the Master Page by casting the Master to your MasterPage as shown below:


protected void Page_Load(object sender, EventArgs e)
{
MyMasterPage MMP = this.Master;
MMP.MyTextBox.Text = "Text Box Found";
}

Inside a content page, Can you access non public properties and non public methods of a master page ?

. No, you cannot access non public properties and non public methods of a master page inside a content page.

. The properties and methods of a master page must be public in order to access them on the content page.
What will happen to an application’s Web.config file when you make changes to it ?

If any changes are made to the application’s Web.config file, IIS automatically restarts the application and applies the changes. This may also cause the resetting of current Application or Session state variables, which can adversely affect users.
When you access the Web.config file from a browser, What will happen ?

You can’t access the Web.config file from a browser due to the security reasons.
The user will receive an error message as "This type of page is not served", If a user requests the Web.config file from your Web site.
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