Interview Questions for Dot net, ASP .Net MVC and C#
I had appeared for an interview for a opening of 2-4 years experience in for ASP .Net MVC and C#. Below are the some asked questions and my given answers for reference
Question 1: What is OOPS concept?
Answer
Object Oriented Programming with the following main features:
Encapsulation- Enclosing all data members (fields and properties) and actions on them (methods) together in a class.
Abstraction- Exposing the operations, while hiding their internal implementation details.
Inheritance- Reusing properties and methods of a parent class in another child class.
Polymorphism- The same method behaves differently in different scenarios.
Question 2: When does a session actually start?
Answer
A session actually starts when a visitor requests your site for the first time. A new session starts when the request doesn’t contain any SessionID or the sessionID references an expired session. The Session_OnStart event in Global.asax can be used for tracking session-related information.
Question 3: How is a session maintained?
Answer
When a web app is requested for the first time, the server creates a sessionID and saves it in the cookie of the client browser. This sessionID is sent to the server in all the subsequent requests. If cookieless is made true, sessionID is sent in the URL else the cookie file is sent. This way the session is maintained with SessionID.
Question 6: What are the various session modes in ASP.NET?
In-proc
Session data is stored in the same machine as that of the server. So session data is lost, when the server restarts. Session data is overhead on the server.
Out-proc
Session data is stored in a different machine as that of the server. So session data is not lost, when the server restarts.
State server
Session data is stored in a separate machine.
SQL Server
Session data is stored in a SQL Server database and kept centrally.
Question 7: You have a user control that has 2 fields. The user will enter data and you need to validate this data and show valid/invalid data in a page. Tell the various ways to do this.
Answer
You can use javascript or jQuery to get the data entered in the element on the page using the id, process data on the user control, and then write the response of element. This can be done by both Javascript, jQuery, AJAX and postback.
Question 8: Name the ActionResults you know
Answer: The various ActionResults in MVC are as follows:
- View
- PartialView
- Content
- Empty
- JSON
- Redirect
- RedirectToRoute
- File
Question 9: What are Html helpers?
Answer
Html helpers are like ASP controls, but these are lightweight.
Question 10: Why do we need HTTP handlers and models?
Answer
To process requests in a way different than the regular IIS way.
Question 11: The default order in which the view is searched
Answer
If Home/Index is requested, views will be searched in the following order:
~/Views/Home/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Home/Index.cshtml
Question 12: Describe the Page life cycle of ASP .NET?
Answer
PreInit: Sets the Master page and theme property dynamically.
Init: Each controls the UniqueID set. This event is fired first for the innermost element in the DOM, then the parent controls, and finally for the page itself.
InitComplete: Changes to the view state and any functionality that requires all the controls to be loaded can be done here.
PreLoad: ViewState of the page and all the controls are loaded. Postback data is also loaded.
Load: The Load event is raised for the page object itself and then recursively for all the child controls until the page itself and all its controls are loaded.
Dynamic controls can be created here.
IsPostBack andIsValid values can be checked to avoid redundant code.
Here all the control values are restored.
PreRender: Allows final changes to the page and its controls. This event is fired for the page first and then for the controls.
Render: Generates the client-side HTML, DHTML, and script that are necessary to display the page on the browser.
UnLoad: This event is fired for the controls and then for the page itself. All the unused objects are disposed of.
Page Life cycle
Question 13: MVC life cycle?
Answer
The RouteTable collection is created when the MVC application is requested for the first time. For the subsequent requests, UrlRoutingModule intercepts the request. From the requested URL, this module determines which controller and which action method is requested and executes the respective ActionMethod.
Question 14: What are the various ways to send data from a controller to view?
Answer
ViewBag: It is a dynamic property.
Controller:: ViewBag.Name = “Anis”;
View:: @ViewBag.Name
ViewData: It is a Dictionary object derived from the ViewDataDictionary class. It requires typecasting and null checks for complex data types.
Controller:: ViewData[“Name”] = “Anis”;
View:: @ViewData[“Name”]
ViewBag and ViewData span for a service call.
TempData: It is a dictionary derived from the TempDataDictionary class. It stores data in a short live session and spans an HTTP request. It is used when moving from one controller to another or from an action method to another. It is for the current and subsequent requests only. It requires typecasting and null checks for complex data types.
Question 15: How to retain a TempData value
Answer
Using Peek and Keep.
If you read the TempData and then call the Keep() or you read the TempData by using Peek(), the TempData will be persisted.
@TempData[“Name”]; TempData.Keep(“Name”);
TempData.Peek(“Name”)
Question 16: Stored Procedure vs Function
Answer
A Stored Procedure can return zero, single or multiple values. But, a function must return a single value.
A Stored Procedure can have both input and output parameters whereas Functions can have only input parameters.
Functions allow only a SELECT command whereas a Stored Procedure allows SELECT and other DML commands (INSERT, UPDATE and DELETE).
A Stored Procedure can call a function, but a function can’t call a Stored Procedure.
A Stored Procedure can use a try-catch block to handle exceptions. But try-catch is not allowed in functions.
Functions can be used in a SELECT statement, but procedures are not allowed in a select statement.
SQL transactions can be used in Stored Procedures but not in Functions.
Question 17: What are the various ways to send content from one page to another?
Answer
- Response. Redirect()
- Server.Transfer()
- WebClient.DownloadFile()
Question 18: How to call a code behind a method asynchronously
Answer
Create a public static method and decorate it with the WebMethod Attribute.
Add an asp: ScriptManager control with “EnablePageMethods = true” in the aspx page.
Now either you can make a jQuery ajax request to it or can call from JavaScript using the PageMethods class.
[WebMethod]
public static string Operate()
{
return “Welcome”;
}
<asp:ScriptManager runat=”server” EnablePageMethods = true></asp:ScriptManager> Window.PageMethods.Operate();
Question 19: What does asp: ScriptManager do to call a WebMethod?
Answer
JavaScript proxies are automatically generated by the ScriptManager that can call the page methods in the code-behind page. The proxy is accessed on the client side using a special class generated by the ScriptManager called PageMethods.
ScriptManager
Question 20: What are all the things that AJAX can’t do?
Answer
AJAX is not supported by old browsers that don’t support the XMLHttpRequest JavaScript object.
AJAX will not work if JavaScript is disabled in the browser.
Back functionality will not work with ajax since dynamic pages don’t register themselves with the browser history engine.
If the page is bookmarked, the content implemented with AJAX will not reflect.
Question 21: Two interfaces have the same method. A class implements both of these interfaces. You need to override both of these methods in your class. How can you do this? If another class inherits this class, how can you call both of these methods?
Answer
public interface Interface1 { void Add(); }
public interface Interface2 { void Add(); }
public class BaseClass : Interface1, Interface2
{ void Interface1.Add() { Console.WriteLine(“Interface1”); }
void Interface2.Add() { Console.WriteLine(“Interface2”); } }
class Program: BaseClass { static void Main(string[] args)
{
BaseClass obj1= new Interface1();//Instantiate base class with respect to Interface1
BaseClass obj2= new Interface2();//Instantiate base class with respect to Interface2
obj1.Add();
obj2.Add();
}
}
Output:
Interface1
Interface2
Question 22: What is late binding?
Answer
Late binding is also known as dynamic polymorphism or method overriding.
It has the same method name and signature in both the parent and child classes mainly used with inheritance. The abstract and Virtual keyword are used in the base class method and the override keyword is in the child class method. Late binding is also known as dynamic polymorphism or method overriding.
Question 23: What will be the output?
Given:
Class test {
String str1 = “Hello”;
Public static void Main() { Str1 +=” World”; Console.WriteLine(Str1); } }
Answer
Compilation error: non-static fields can’t be accessed in a static context
Question 24: Sealed vs Static
Answer
A Sealed class can’t be inherited. A static class can’t be instantiated.
Question 25: virtual vs override
Answer
When you want a base class method to be overridden in a child class, the method in the base class should have the “virtual” key and the method in the child class should have the “override” keyword.
virtual-override
Question 26: The usage of partial keyword
Answer
The “partial” keyword is used with a class, interface, or structure when you want to have its definition split within the same namespace. A partial class can have a partial method when you need to have the method declaration in one partial class and its body in another partial class.
Question 27: What does AutoEventWireUp in the page directive tag mean?
Answer
If “AutoEventWireUp = true”, you don’t need to attach event handlers to the events. Automatically, the event handler will be executed when the event fires up. You just need to override the required event handler.
Question 28: If you have 1000 users requesting your application, how many times Application_Start event will be fired?
Answer
Application_Start will be fired only once at the application level. In other words, when the application is requested for the first time.
Question 29: Property vs field
Answer
Fields are private elements whereas properties are public elements with a get/ set.
Question 30: for vs foreach
Answer
foreach is generally used with a collection.
for uses, lesser stack space than foreach since foreach creates a local copy of the item to traverse through and the item that changes in the loop.
Question 31: String is immutable. What does that mean? Explain with an example.
Answer
A string value can’t be changed. Each time you change a string variable, it actually creates a new string instance and dereferences the old value.
Now, Str1 will have the value “Hello World”. But, “Hello ” also exists in memory but it is not referenced by any variable.
Question 32: How to declare a global variable in JavaScript?
Answer
You can define the variable outside functions using the “var” keyword in the variable declaration.
Example: var Str1=’Welcome’;
Question 33: alert (5+”5”)
Answer Error
JavaScript is not a strongly typed language. It will take the data type of the first input