Mar 07

Some interview questions

ASP.Net | General | Tips and Tricks | Visual Studio 121 Comments »

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Today I was surfing on the blog of Sheikh Ahmad and I saw some interview questions related to .Net. I will try to give answers of some of the questions.

Here is a question from his blog:

How can we get the variables of first form from second form without using query string and session etc?

Answer:

In these kinds of questions the interviewrs are actually try to detrmine that how much do you know about the .Net 2.0. It is very simple to get any control of the Page by using the this keyword or just from the ID of the control like

lbltest.Text = "hello";

But now the critical part of the question is that how to get the variables of first form from second form without using query string or session etc. This question itself suggests that we can access variable of one form from another using the query string or session etc but the interviewers has asked you not to use this approach.

What if I ecounter this problem in my work life? To solve this I will try to use an approach in which I can get the instance of the first page in the second page. Now the problem is that how to pass the instance of the first page in the second page without using query string or session etc. The answer is Cross Page posting which is a new feature of .Net 2.0. By default Controls like button , image button of ASP.Net post back to same page. If you want to post back to some other page instead of the current page then you can set the postbackUrl of these controls (i.e. Buttons,LinkButton,Image buttons). This is called the Cross Page posting and it is the feature of the .net 2.0. By using this appraoch you can navigate to the second page and in that second page you can access the first page too.

Suppose you have two pages page1.aspx and page2.aspx. In page1.aspx you have a button and a lable. Set the PostbackUrl of the button to page2.aspx and in the page2.aspx use previouse page property to get the previouse page's instance like this:

Page source = this.PreviousePage.

By using this approach you able to get the instance of the first page in the second page without using the Session or Query string. Now you can use this instance to get the variables, View state etc of the first page. 

 

Question: What are collections and why we use them?

Answer: Simply speaking Collection is a container which is used to hold the objects.

The second part of the question is that why we use them?

Well in programming we write differnt kinds of classes then we declare the objects of those classes. Those objects perform differnt functions and communicate with each other too and after performing their action they give output to some other kind of objects. So we use the container or collection to hold these objects. 

The question arise that what is the need of this container in the presense of the array? The answer is that array can not be used to hold differnt kinds of objects. Secondly we need to set the length of the array when we declare it but this is not the case with collection. The size of collection is dynamic. You just need to declare the collection and that is it. In collection you do not need to know that how many and what types of objects you are going to hold in collection.

Like other programming language C# has differnt kinds of container or collections i.e.

  • Stack
  • Queues
  • Hashtable
  • ArrayList 

Actually what ever you add in a collection becomes an object. It is said that every thing in .Net is an object, so what ever you add in a collection becomes object thus loosing its identity because in collection they are now in the form of object. The conversion of types into object is done by the .Net itself and it is called the Boxing. And this conversion of types to object is also called upcasting and it is always safe. Why it is always safe? Because you are converting the types into their super class (i.e. object of your class into System.Object) and we know that every thing in .net is derived from Object so this conversion will be safe always. Now when we get back our data from the collection it will be in the form of Object instead of the form in which we have added it in the collection. So when we get the data back from the collection then we have to cast it back into its orignal type. The conversion of the object into some specific type is called unboxing. Unboxing is also called downcasting. Downcasting should be done carefully because if you try to cast it into some wrong type then you will get the error. e.g. Casting the Circle into rectangle is wrong. So downcasting should be done carefully. Upcasting is done by the .Net itself and can be done programmatically but it is not dangeriouse while downcasting could be dangerious if not done properly.

That can be explained like "Circle and rectangle are always shape but it is not necessary that Shape is always a circle or rectangle"

Question: Shellow Copy vs Deep Copy?

I already wrote an article that how to impliment Shallow copy in .Net. Here is the Link of that article. It might help you.

http://www.shahidriaz.com/post/2008/12/07/ShallowCopy.aspx

 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: ,

Comments

Add comment


(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading