Sunday, 22 June 2014

Basic Interview Questions and Answers for ASP .NET

Basic Interview Questions and Answers for ASP .NET
What is asp.net life cycle ?
Life Cycle Events

PreInit
The properties like IsPostBack have been set at this time.
This event will be used when we want to:
1.        Set master page dynamically.
2.        Set theme dynamically.
3.        Read or set profile property values.
4.        This event is also preferred if want to create any dynamic controls.
Init
1.        Raised after all the controls have been initialized with their default values and any skin settings have been applied.
2.        Fired for individual controls first and then for page.
LoadViewState
1.        Fires only if IsPostBack is true.
2.        Values stored in HiddenField with id as _ViewState decoded and stored into corresponding controls.
LoadPostData
Some controls like:
1.        Fires only if IsPostBack is true.
2.        Some controls like Textbox are implemented from IPostBackDataHandler and this fires only for such controls.
3.        In this event page processes postback data included in the request object pass it to the respective controls.
PreLoad
  • Used only if want to inject logic before actual page load starts.
Load
  • Used normally to perform tasks which are common to all requests, such as setting up a database query.
Control events
1.        This event is fired when IsPostBack is true.
2.        Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
PreRender
Raised after the page object has created all the controls that are required for rendering which includes child controls and composite controls.
1.        Use the event to make final changes to the contents of the page or its controls before the values are stored into the viewstate and the rendering stage begins.
2.        Mainly used when we want to inject custom JavaScript logic.
SaveViewState
  • All the control values that support viewstate are encoded and stored into the viewstate.
RenderGenerates output (HTML) to be rendered at the client side.
  • We can add custom HTML to the output if we want here.
Unload
1.        Fired for individual controls first and then for page.
2.       Used to perform cleanup work like closing open files and database connections.  
If I have more than one version of one assemblies, then how will I use old version (how/where to specify version number?) in my application?

The version number is stored in the following format: …. The assembly manifest can then contain a reference to which version number we want to use.
What is the difference between Array and LinkedList?
An array is a collection of the same type. The size of the array is fixed in its declaration.
A linked list is similar to an array but it doesn’t have a limited size.
How can you write a class to restrict that only one object of this class can be created (Singleton class)?

Use the singleton design pattern.
 
 public sealed class Singleton 
 { 
   static readonly Singleton Instance=new Singleton(); 
      static Singleton() 
      { 
      } 
      Singleton() 
      { 
      } 
      public static Singleton Instance 
      { 
           get 
           { 
                return Instance; 
           } 
      } 
 }
 
What is close method? How its different from Finalize and Dispose?
 
finalise is the process that allows the garbage collector to clean up any unmanaged resources before it is destroyed.
The finalise method can not be called directly; it is automatically called by the CLR. In order to allow more control over the release of unmanaged resources
the .NET framework provides a dispose method which unlike finalise can be called directly by code.
Close method is same as dispose. It was added as a convenience.
What is Boxing and UnBoxing?
Converting the value type data type in to the Reference type is called as Boxing. Converting the Reference type data type and keep its value to stack is called as the reference type.

byte b= 45;
Object o = b.Tostring();
The Advantage of boxing and unboxing is that we can convert the type of the object in to another type. The disadvantage is that it requires lot of memory and CPU cycles to convert from one type to another type.
Object o=10;
Int i= Convert.ToInt32(o.ToString());
What is AutoPostBack?
If you want a control to postback automatically when an event is raised, you need to set the AutoPostBack property of the control to True
 
 
Why do you use the App_Code folder in ASP.NET?
The App_Code folder is automatically present in the project. It stores the files, such as classes, typed data set, text files, and reports. If this folder is not available in the application, you can add this folder. One of the important features of the App_Code folder is that only one dll is created for the complete folder, irrespective of how many files it contains.
In which event of page cycle is the ViewState available?
After the Init() and before the Page_Load().
 
How long the items in ViewState exists?
They exist for the life of the current page.

Where the viewstate is stored after the page postback?
ViewState is stored in a hidden field on the page at client side. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source.
What is the difference between web config and machine config?
Web config file is specific to a web application where as machine config is specific to a machine or server. There can be multiple web config files into an application where as we can have only one machine config file on a server.
 
When the View state is saved, and when is it loaded? How to enable/ disable View states?
A. View State data is stored in the current page in base64 encoded format. It gets loaded with the page and displays the values to the controls after the decoded. Internally it actually saves the check-sum of all the control data where the view state is enabled.so that when the page gets loaded due to any post back, it again finds the check-sum and then decodes the Base64 encoded string and gets back the same data to the controls. We can see the view state base 64 encoded string in View Source of the page. It will be like _VIEWETATE="DSDSDF8DGDGDFGFD5FDGGDJFF23BNN457M9UJOG" this.
View state won't take the client or server memory to keep the view state data.
Difference between Server Controls and User controls?
User controls are used for the re-usability for the controls in the application. By using the user control, we can use the same control in the various pages. User controls can be created by combining more than one control. To use the user controls, first we need to register them in the web page where we want to use that control. A separate copy is need in each page where we want to use the user control. User controls can't be included in to the toolbox.
Server controls are those controls which can be found in the toolbox and can be directly drag to the application like textbox, button etc. For the server control, only 1 copy of the control is needed irrespective of the number of web pages. If we want 10 text-boxes to be added in our web page, we need only 1 copy of the textbox in the toolbox and can be dragged 10 times.

No comments:

Post a Comment