InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is View State? |
|
Answer» ViewState is used to persist the data of page or page controls on postback. Viewstates are present on a page as a hidden field with data in an encrypted format. We can store any kind of data. Advantages:
Disadvantages:
Example: public class Person { public string FirstName {get;set;} public string LastName {get;set;} } public PARTIAL class default : Web.UI.Pages { Person p = new Person(); p.FirstName = "Harshit"; p.LastName = "Mittal"; ViewState["PersonDetails"]=p; } |
|
| 2. |
What are the advantages of using session? |
|
Answer» Advantages of using session:
|
|
| 3. |
Explain Cookie-less Session in ASP.NET? |
|
Answer» Sessions are used in ASP.NET applications for state management. Sessions also make use of COOKIES to associate sessions with the correct user. But when BROWSER cookies are turned off then in that case COOKIE-less sessions are used. It APPENDS the values to the browser’s URL that is required to associate with the user. By default sessions use a cookie in the background, to enable cookie-less sessions, the following settings are required to make in web.config file. <sessionState cookieless="true" />The possible options to set cookieless options are:
|
|
| 4. |
What is the difference between ASP.NET Web API and WCF? |
||||||||||||
Answer»
|
|||||||||||||
| 5. |
What all are the differences between Rectangular and Jagged Array? |
|
Answer» A ‘RECTANGULAR array’ is a multidimensional array where all the rows have the same number of elements, all elements are STORED continuously in memory, these are very compact in memory example: INT[] arr = {1,2,4}; int[,] multiArr = new int[2,3];A ‘jagged array’ is also known as an array of arrays. It is also the multidimensional array one in which the length of the rows need not be the same. For example, string[][] jaggedArray = new string[3][]{ new string[] {"TeamCS", "JOHN", "James", "Garry", "Linus"}, new string[] {"TeamMath", "Ramanujan", "Hardy"}, new string[] {"TeamSc", "Albert", "Tesla", "Newton"}, };The above-jagged array consists of 3 rows, with each row having another array of different size. The elements in the jagged array are a reference of the other arrays, which can be anywhere in the memory |
|
| 6. |
Why do we need Reflection? |
|
Answer» It is a powerful tool for determining the contents of unknown assembly which can be USED for a wide variety of classes in simple WORDS Reflection provides the ability to determine things and execute code at runtime. Usage of Reflection:
The Namespace used for reflection is System.Reflection. |
|
| 7. |
What are indexers in C# .NET? What is the difference between property & indexers? |
||||||||||
|
Answer» When we use indexers in class it behaves just LIKE the virtual array. The user sets the value of the index and can retrieve the data without POINTING an instance or a type member. using System; class clsIndexer { private string[] val = new string[3]; public string this[int index] { get { return val[index]; } set { val[index] = value; } } } class main { public static void Main() { clsIndexer ic = new clsIndexer(); ic[0] = "C"; ic[1] = "CPP"; ic[2] = "CSHARP"; Console.Write("Printing values stored in objects used as arrays\n"); Console.WriteLine("First value = {0}", ic[0]); Console.WriteLine("Second value = {0}", ic[1]); Console.WriteLine("THIRD value = {0}", ic[2]); } }
|
|||||||||||
| 8. |
What is the difference between the “throw” and “throw ex” in .NET? |
|
Answer» In THROW, the original EXCEPTION stack information is retained. Example: For Throw : try { // some operation that can FAIL } catch (Exception ex) { throw; }For Throw Ex: In Throw Ex the original information is overridden by the External information and you will LOSE the original information. try { // do some operation that can fail } catch (Exception ex) { // do some local cleanup throw; } |
|
| 9. |
What are extension methods and where can we use them? |
|
Answer» EXTENSION methods allow you to add our own custom method in any custom class without modifying into it and in our custom class and it will be available throughout the application by SPECIFYING namespace only where it is defined. Example: namespace ExtensionMethod { public STATIC class IntExtension { public static bool IsGreaterThan(this int i, int value) { return i > value; } } }Here the first parameter specifies the type on which the extension method is APPLICABLE. We are going to use this extension method on int type. So the first parameter MUST be int preceded with the modifier. using ExtensionMethod; class Program { static void Main(string[] args) { int i = 10; bool result = i.IsGreaterThan(100); Console.WriteLine(result); } }Output: False |
|
| 10. |
How to use Nullable Types in .NET? |
|
Answer» As we know we cannot assign the null value to any value type so for that we use nullable type where we can assign null value to the value type variables. Syntax: Nullable<T> where T is a type. Nullable type is an instance of System.Nullable<T> STRUCTNullable Struct : [Serializable] public struct Nullable<T> where T : struct { public bool HasValue { get; } // it will return true if we assigned a value, if there is no value or if we assign a null value it will return false; public T Value { get; } } HasValue : static void Main(STRING[] args) { Nullable<int> i = null; if (i.HasValue) Console.WriteLine(i.Value); else Console.WriteLine("Null"); }Example: [Serializable] public struct Nullable<T> where T : struct { public bool HasValue { get; } public T Value { get; } } static void Main(string[] args) { Nullable<int> i = null; if (i.HasValue) Console.WriteLine(i.Value); // or Console.WriteLine(i) else Console.WriteLine("Null"); }Output: NullIt will check WHETHER an object has been assigned a value if it is having null value it will return false and the else PART will be executed. To get the value of i using the nullable type you have to use GetValueOrDefault() method to get the ACTUAL value. static void Main(string[] args) { Nullable<int> i = null; Console.WriteLine(i.GetValueOrDefault()); } |
|
| 11. |
What is the difference between Server.Transfer and Response.Redirect? |
||||||||||
Answer»
|
|||||||||||
| 12. |
What is the difference between Hash Table and Dictionary? |
||||||||||||
Answer»
|
|||||||||||||
| 13. |
What are the different state management techniques in .NET? |
|
Answer» State management basically stores the information of user/Page or object when a user makes a REQUEST on a web page, since HTTP is a protocol which doesn’t store the information during each request made by the user.so Asp.net state management is used to preserve the data.
Server Side:- 1) Session: when we want to store the data of web page over multiple requests we use session. In this TECHNIQUE we can store any kind of information or object in server memory it basically stores the information. The session is user specific since it stores the information of user separately. When we store data to Session state, a session cookie named is ASP.NET_SessionId is created automatically. Example: Storing the data in Session object Session ["UserName"] = txtName.Text;Retrieving the data from the Session object Label1.Text = Session ["UserName"].ToString();Session timeout is 20 min if we want to increase the timeout of session then we need to configure the web config <configuration> <system.web> <sessionState timeout="60" /> </system.web> </configuration>2). Application object: In application object, we can store the information at the application level rather than User level, where every user can access that information .the main disadvantage is that sometimes there is a concurrency problem, so for that, we use a lock and unlock method. So if multiple requests came for same data then only one thread can do work. Example: Application["Message"] = "Hello to all";We can use an application object to count the number of VISITORS on that website. Client Side: 1) Query Strings: When we want to transfer the data over the pages from the URL then we use query string, it cannot HANDLE the huge data and it is compatible with all browsers, we cannot transfer the objects and controls in query strings, so it is not meant for transferring sensitive data through query string. Example: Response.Redirect( "SecondPage.aspx?Type=01&Query=Query1" );Or if we want to use query string to transfer the data then we can encrypt the sensitive data and we can transfer the data. 2). Control State: It is used to transfer the data which is stored in controls .it is just like same as view state but here we can store the data of the controls like dropdown list`s selected items, we cannot disabled the control state if viewstate is disabled then control state works same as view state. 3). View State: It is one of the finest methods to preserve the data, in view state we can store any kind of data like controls data, and even variables and raw data, view state encrypt the data when it will render to the user, so if user wants to see the value of view state in view source it will contain the symbols only which are very hard to decrypt. The only disadvantage is that page contains much space over the page. To handle the view state we just need to "EnableViewState = true" attribute to any control to save its state, Example: public class Person { public string FirstName {get;set;} public string LastName {get;set;} } public partial class default : Web.UI.Pages { Person p = new Person(); p.FirstName = "Harshit"; p.LastName = "Mittal"; ViewState["PersonDetails"]=p; }4). Cookies: Cookies are used to identify the user on a web page whenever the user makes the request for the first time it stores that request data in the cookies and when the user visit that web page for the second time then browser check the cookies data with second request data made by the user if the result matches browser to consider them as a previous user else consider a new user. Example: The Case of Remember me on every login page. There are 2 types of cookies:
Example: Response.Cookies["nameWithPCookies"].Value = "This is A Persistence Cookie"; Response.Cookies["nameWithPCookies"].Expires = DateTime.Now.AddSeconds(10);
Example: Response.Cookies["nameWithNPCookies"].Value = "This is A Non Persistence Cookie";5) Hidden Field - When we want to store the small amount of data or we can say the if want to save the single value, hidden field controls is not rendered on the client browser, it is invisible to the browser. Example: <asp:HiddenField ID="HiddenField1" runat="server" />At the server code protected void Page_Load(object sender, EventArgs e) { if (HiddenField1.Value != null) { int val= Convert.ToInt32(HiddenField1.Value) + 1; HiddenField1.Value = val.ToString(); Label1.Text = val.ToString(); } } |
|
| 14. |
What are partial classes? |
|
Answer» While programming in C#, we can split the definition of a class over two or more source files. The source files contain a SECTION of the definition of class, and all parts are combined when the APPLICATION is compiled. For splitting a class definition, we need to use the PARTIAL keyword. Example: We have a file with a partial class named as Record namespace HeightWeightInfo { class File1 { } public partial class Record { private int h; private int w; public Record(int h, int w) { this.h = h; this.w = w; } } }Here is another file named as File2.cs with the same partial class Record namespace HeightWeightInfo { class File2 { } public partial class Record { public void PrintRecord() { Console.WriteLine("Height:"+ h); Console.WriteLine("Weight:"+ w); } } }The main method of the project namespace HeightWeightInfo { class Program { static void Main(string[] args) { Record myRecord = new Record(10, 15); myRecord.PrintRecord(); Console.ReadLine(); } } } |
|
| 15. |
How can we cache multiple responses from a single Web form? |
|
Answer» We can cache multiple response from a single web form by using any one of the attributes from VaryByParam, VaryByHeaders or VaryByCustom attributes. Example: If we want display different COUNTRY data on our page where we can CHOOSE country name from the drop down list. On SelectedIndexChanged event of the Drop down we have to write some LOGIC to FETCH the country data according to the selected value and display the data of the particular country. By setting the VaryByParam attribute to the dropdown will cache all the responses received from server on SelectedIndexChanged event of drop down. <%@ OutputCache DURATION="20" Location="Server" VaryByParam="countryDropDown%> <aspx:DropDownList ID="countryDropDown" AutoPostBack="true" runat="server" onselectedindexchanged=" countryDropDown _SelectedIndexChanged"> <asp:ListItem Text="Germany" Value=" Germany "></asp:ListItem> <asp:ListItem Text="Singapore" Value=" Singapore "></asp:ListItem> <asp: ListItem Text="India" Value=" India "></asp: ListItem> </asp:DropDownList> |
|
| 16. |
What are the advantages and disadvantages of Query Strings? |
|
Answer» Advantages:
Disadvantages:
ASP NET Core developers are sought after by companies for their niche skill sets. These interview QUESTIONS on ASP.NET will give you a fair idea of the questions which the interviewer ASKS during an ASP.NET interview. So, prepare well and good luck! |
|
| 17. |
How to create a cookie in asp.net website? |
|
Answer» By using HttpCookie CLASS object. After creating COOKIE it will be attached to Response Object to send it to the CLIENT. Ex: HttpCookie cookie1=new HttpCookie ("USERNAME","Ritu"); Response.Cookies.Add (cookie1); |
|
| 18. |
What are the advantages and disadvantages of cookies? |
|
Answer» Advantages:
Disadvantages:
|
|
| 19. |
What is the maximum size of a cookie, and how many can be stored in a browser for each web site? |
|
Answer» 50 Cookies per domain, with a maximum of 4 KB per cookie (or even 4 KB in total). If you want to SUPPORT most browsers, then do not EXCEED 50 cookies per domain and 4093 BYTES per domain. That is, the size of all cookies should not exceed 4093 bytes. |
|
| 20. |
What are the types of cookies? |
Answer»
|
|
| 21. |
What are cookies in ASP.NET? |
|
Answer» Cookie is a small piece of information stored on the client machine. This file is located on client MACHINES "C:\Document and Settings\Currently_Login user\Cookie" path. It is USED to store user preference information LIKE Username, Password, City and PhoneNo ETC. on client machines. We need to import namespace called System.Web.HttpCookie before we use a cookie. |
|
| 22. |
What are the different validators in ASP.NET? |
Answer»
|
|
| 23. |
How can you cache different versions of the same page using the ASP.NET Cache object? |
|
Answer» OUTPUT cache FUNCTIONALITY is achieved using the OutputCache attribute in the ASP.NET page header. Below is the syntax: <%@ OutputCache Duration="20" Location="Server" Vary By Param="state" Vary By Custom="minor version" Vary By Header="Accept-Language"%>
|
|
| 24. |
What are the types of caching in asp.net? Explain. |
Answer»
It keeps a copy of the response that was sent to the client in memory and subsequent requests are then responded with the cached output until the cache expires, which INCREDIBLY improves the ASP.NET web application performance. It is implemented by placing an OutputCache directive at the top of the .aspx page at design time. Example: <%@OutputCache Duration="10" VaryByParam= "Id"%>
Sometimes we might want to cache just portions of a page. For example, we might have a HEADER for our page which will have the same content for all users. To specify that a user control should be cached, we use the @OutputCache directive just like we USED it for the page. <%@OutputCache Duration=10 VaryByParam="None" %>
Data Cache is used to store frequently used data in the Cache memory. It's much efficient to retrieve data from the data cache instead of database or other sources. We need use System.Web.Caching namespace. The scope of the data caching is WITHIN the application DOMAIN unlike "session". Every user can able to access these objects.
|
|
| 25. |
Does ViewState affect performance? What is the ideal size of a ViewState? How can you compress a ViewState? |
|
Answer» If the application is storing a LOT of data in ViewState it can AFFECT the overall responsiveness of the page, thereby affecting performance since data is stored on the page itself in hidden controls. The ideal size of ViewState should be LESS than 30% of the page size. DeflateStream and GZipStream are the classes in ASP.NET that provides methods and properties to COMPRESS and decompress streams. Using these classes to compress ViewState will REDUCE its size to around 40%. |
|
| 26. |
What are the advantages and disadvantages of ViewState? |
|
Answer» Advantages:
Disadvantages:
|
|
| 27. |
What is ViewState in ASP.NET? |
|
Answer» ViewState is a client-side state management MECHANISM in ASP.NET. It is a default technique used by ASP.NET to persist the VALUE of the page and controls during postbacks. In ASP.NET ViewState the values are encrypted and stored in a hidden FIELD ( named _VIEWSTATE) on the page as an encoded BASE64 string. By default, ViewState is sent to the client browser and then returned to the server in the form of a hidden input control on your page. |
|