Home ASP.NET.NET interview questions: - Different steps to swap two variable data without using third variable?
A .NET interview questions especially for fresher where the interviewer asks to check skills and many of the junior developer fails to answer this question, which is very sad.
So, let's create a sweet and simple example to see how exactly we can swap the two variables data without using third variable.
Before we go ahead and create an example of swapping two variables data let first see how we can swap two variable data using the third variable and later we will create an example for swapping two variables data without using third variable.
In order to see it practically you just need to follow the following steps.
Step1: - create a new Console Application for that just go to >> File >> New >> Project >> Windows >> Select Console Application.
Step2: - Now simply just add the below code in to your program.cs file of your Console Application.
class Programstatic void Main(string[] args)int a = 10;// Created int Variable a with value =10.int b = 5;// Created int Variable b with value =5.int temp = 0;// Creted a temp variable.temp = a;//Passing the value of a to temp.a = b;//passing the value of b to a.b = temp;//pasiing the value of temp to b.Console.WriteLine("The Value Of Variable a is:"+a);Console.WriteLine("The Value Of Variable b is:" +b);Console.ReadLine();}}
In the above code snippet you can clearly see that I have used the third variable to swap the two variables values.
Now, simply just run your application and you will see the result like below diagram.
Step3: - Now, let's see an example for swapping two variables value without using third variable.
Below is the code snippet for the same.
class Programstatic void Main(string[] args)int a = 10;// Created int Variable a with value =10.int b = 5;// Created int Variable b with value =5.a = a + b;b = a - b;a = a - b;Console.WriteLine("The Value Of Variable a is:"+a);Console.WriteLine("The Value Of Variable b is:" +b);Console.ReadLine();}}
In the above code snippet you can clearly see that now I have not used the third variable temp to swap the value of two variables instead I have just added the few line of code like below code lines.
a = a + b;
b = a - b;
a = a - b;
Now, simply just run your application and will see the result like below diagram.
See the following video on use of shadowing done in C#: -
Avail from the link more interview questions and answers for .NET for preparation.
Regards,
Refer author's other blog for complete Most asked Dotnet interview questions





Write a Comment
All fields marked with * are mandatory
In this article we will show difference between WCF fault exceptions and normal .NET exceptions. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Aug 28th, 2012 | WCF
.NET interview questions: - Can you elaborate project life cycle?
In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jan 17th, 2012 | .Net
WCF Interview questions:- Which binding do we need to use for WCF REST?
In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More
By : Shiv Prasad Koirala | Nov 16th, 2011 | WCF
In this article we will explain about Sub-Queries and co-related queries. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jul 2nd, 2012 | SQL
Five most important SharePoint 2010 interview questions
This article will give answers to 5 most asked questions in the interview.... Read More
By : Shiv Prasad Koirala | Aug 3rd, 2011 | Share Point
In this article we will show difference between WCF fault exceptions and normal .NET exceptions. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Aug 28th, 2012 | WCF
.NET interview questions: - Can you elaborate project life cycle?
In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jan 17th, 2012 | .Net
WCF Interview questions:- Which binding do we need to use for WCF REST?
In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More
By : Shiv Prasad Koirala | Nov 16th, 2011 | WCF
In this article we will explain about Sub-Queries and co-related queries. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jul 2nd, 2012 | SQL
Five most important SharePoint 2010 interview questions
This article will give answers to 5 most asked questions in the interview.... Read More
By : Shiv Prasad Koirala | Aug 3rd, 2011 | Share Point
Article Categories
YouTube Videos