Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


there are 2 variables called x and y ,x contains 1,2 and y
contains 3,4 we have to swap the values from x to y and y
to x with out using dummy variables and it can be done only
by using a single statement ? how?

Answers were Sorted based on User's Feedback



there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / ravin

Buddies, why are you all selecting from and updating the
tables. The question says, just two variables to be swapped
without using any extra variable and also with a single
statement.
And Ramakanth's answer is right i.e. y=x+y-(x=y) . Thats it.

^^x contains 1,2 - didn't understand though.

Is This Answer Correct ?    13 Yes 2 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / ramakanth

y=x+y-(x=y)

Is This Answer Correct ?    6 Yes 3 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / roopesh kumar

declare
x number:= 1;
y number:= 2;
begin
x := x + y;
dbms_output.put_line (x); -- output 3;
y := x - y;
dbms_output.put_line (y); -- output 1;
x := x - y;
dbms_output.put_line (x); -- output 2;
end;
/

Is This Answer Correct ?    8 Yes 5 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / radha sri seshu.kolla

YES SMITA I HAVE ONE DOUBT IN YOUR QUESTION. X CONTAIN 1,2
HOW ONE VARIABLE CAN HOLD TWO VALUES AT A TIME. TRY TO BE
MORE SPECIFIC IN YOUR QUESTION

Is This Answer Correct ?    3 Yes 0 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / v

yeah completely agree with u(the above person).The answer
was supposed to be in a single statement and everybody
wrote an history 4 it....hehe
no offence meant

Is This Answer Correct ?    0 Yes 0 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / divya c

(y!=x)||(x!=y)?x=y,y=x:y=x,x=y;

Is This Answer Correct ?    0 Yes 0 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / senthil kumar

declare
x number:= 1;
y number:= 3;
begin
x := x + y;
dbms_output.put_line (x); -- output 4;
y := x - y;
dbms_output.put_line (y); -- output 1;
x := x - y;
dbms_output.put_line (x); -- output 3;
end;
/

Now Y got 1 and x got 3 samae way of x = 2, y =4

Is This Answer Correct ?    0 Yes 0 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / sheshadri

String x="1,2";
String y="3,4";
x=(y+"$"+(y=x)).split("[$]")[0];

Is This Answer Correct ?    0 Yes 0 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / nanne saheb c

Assume that the sample table is as below
X Y
------------
1 3
2 4

Update sample set
X=decode(X,1,3,2,4),
Y=decode(Y,3,1,4,2);

(OR)
Update sample set
X = (case when 1 then 3
when 2 then 4
end),
Y = (case when 3 then 1
when 4 then 2
end);

Is This Answer Correct ?    1 Yes 1 No

there are 2 variables called x and y ,x contains 1,2 and y contains 3,4 we have to swap the values..

Answer / raji_4u

IF YOU WANT TO UPDATE THE TABLE,

UPDATE TABLE_NAME
SET X = Y, Y = X;

--------------------------
IF YOU WANT TO JUST DISPLAY,

SELECT Y X,X Y FROM TABLE_NAME;

ISN'T THIS SIMPLE THAN SANTHI.K DID.

Is This Answer Correct ?    5 Yes 7 No

Post New Answer

More SQL PLSQL Interview Questions

Explain what is dbms?

0 Answers  


How do you modify a trigger?

0 Answers  


what is the different between now() and current_date()? : Sql dba

0 Answers  


if table named a is there and 4 records are there then how to swap (1 and 3) and (2 and 4) records at a time

1 Answers  


what are the performance and scalability characteristics of mysql? : Sql dba

0 Answers  


What are the different types of database management systems?

0 Answers  


what is the order of pre-defined exceptions.

6 Answers   TCS,


hi..........i m Accenture employee...Accenture is an IT company having itz branches in India at Mumbai, Pune,Delhi, Hyderabad, Chennai and Bangalore....Presently there are openings here for SAP, Testing, Oracle,Java,.NET,MAinframe and Peoplesoft...... I am a software engineer working with Accenture,Mumbai....u can mail me in your resume..i`ll forward it to our HR team...my mail id: pankit.lodaya@accenture.com

8 Answers   Wipro,


in procedure how to return a value

3 Answers   Wipro,


How to load data with sql*loader?

0 Answers  


What is sqlite used for?

0 Answers  


What is the difference between microsoft sql and mysql?

0 Answers  


Categories