Sunday 8 December 2013

ASP.NET Interview Questions with Answers for 2+ years Experience

DOT NET interview questions for professionals above 2 years of Experience.  It will be helpful for those who are looking to brush up their key fundamentals on the topic. Will be posting more set of questions. Let me know if you need any improvement in the below mentioned questions or answers.

  • Difference between Const and Read Only


CONST
Read Only
Can't be static.
Can be either instance-level or static
Value is evaluated at compile time.
Value is evaluated at run time
Initialized at declaration only
Can be initialized in declaration or by code in the constructor.
Ex: public const int i = 2
public static read only i Read = Date Time.Now;

  • Difference between finalize and Dispose.


Finalize
Dispose
Finalize() is called by Garbage Collector implicitly to free unmanaged resources
Dispose () of IDisposable  interface is called by the programmer to explicitly release resources when they are no longer being used
Finalize() is the C# equivalent of destructor
Dispose() has to be implemented in classes implementing IDispose interface
Finalize() is called by the runtime 
Dispose() is called by the user

  • What are Anonymous methods.


Anonymous methods are methods without a name. It is used in conjunction with the events and delegate. An anonymous method uses the keyword delegate, instead of a method name. This is followed by the body of the method.
Example:
public Form1()
{
Button btnHello = new Button();
btnHello.Text = "Hello";

btnHello.Click += delegate
{
MessageBox.Show("Hello");
};
Controls.Add(btnHello);
}

  • What are Static Constructors.


It is invoked automatically by the CLR before the first object of a class is created. It is executed only Once. Only 1 Static constructor is allowed per class. It doesnot have access modifiers nor parameters and doesnot execute non-static members of the class. Executes only the static members of the class. User has no control when the static constructor is executed.
Example:
class SomeClass
{
private static Logger logger;

static SomeClass()
{
logger = new Logger();
logger.Init();
}
}

  • Explain ASP.NET page life cycle

After the request is passed through the HTTP Pipelines(HTTPModule and HTTPHandler), a ProcessRequest() method is called which triggers the different events in the ASP.NET page life cycle.
The sequence is as follows:

Pre-Init : Entry point of the .net page life cycle. Controls can be created in this event. Master page and themes can be accessed.

Init: Fires when all controls have been initialized. It wont fire until all control init event have  triggered. It occurs bottom up.

LoadViewState:  View State is loaded.

LoadPostData: Updates the information pulled from the viewstate under LoadViewState.

Page_Load: All code is executed once at the beginning of the page.

RaisePostBackDataChangedEvent: Fires when user completes the form and hit the submit button.

RaisePostBackEvent: Determines what has occurred and which event to call. If btnSubmit is clicked it will call the btnSubmit.Click event

Page_PreRender: Before the page is submitted, viewstate can be changed in this method.

SaveViewState: Saves the viewstate to be used on the page.

Page_Render: Writes the HTML output on the browser page.

Unload: Releases the resources held by the browser.

  •  Difference between Abstract Class and Interface.                           

Abstract Class
Interface
Abstract class is a class that cannot be instantiated but can contain code.
Interface contains only method definition but does not contain any code
Abstract class can implement more than one interfaces, but can inherit only one class
Interface can inherit more than one interfaces
Can consist of Abstract members  and Virtual members
Only consist of the return type and method name with parameters
Can have access modifiers
Does not have access modifiers. By default is Public.
Single Level Inheritance
Multiple level inheritance possible
All methods are implicitly virtual
All methods are implicitly Abstract hence all methods are virtual. Hence programmers choice to go with either Abstract Class or Interface.
Example: We have a class Animal. Cat and Dog both can inherit from Abstract Class Animal. This Animal class can have a virtual methodBreathe(), which we can over ride in derived class for Fish which doesnot breathe like other animals.
Example: In our class Animal, if we want to implement iFeedable(), iLikeable() method, we create an Interface which will be common for all animals.

  • Difference between string and stringbuilder

String
StringBuilder
String object is immutable
StringBuilder object is mutable
Immutable means the value of string object cannot be changed. Internally a new string object is created.
Mutable means we can change the value of the stringbuilder object by making use of functions like append() or appendline()
Performance issue, if we change the string object  frequently
Performance efficiency is achieved.                                     

What are WebFarmsWhen a lot of request is coming from client to server and the server is not able to handle all the request then there is a need for a standalone servers to handle the load. So we use multiple servers and divide the traffic using Load balancer. When we host a single website on multiple web servers, it is known as webfarms.
In-Proc (default session mode) is not supported in web farms as Load balancer can move request to any server with less load. Hence out-proc(state server or SQL server) session mode is preferable.
  • Can a class be declared as private.

If the class is nested in some other class, it can be declared as Private.
Otherwise a class cannot be declared as private. It will throw a compiler error message, which concludes this question “Namespace elements cannot be explicitly declared as private, protected, or protected internal”. 

No comments:

Post a Comment