ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Code Snippets >> Programming Code >> C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions (401)
 C++ Code interview questions  C++ Code Interview Questions (121)
 VC++ Code interview questions  VC++ Code Interview Questions (2)
 Java Code interview questions  Java Code Interview Questions (57)
 Dot Net Code interview questions  Dot Net Code Interview Questions (113)
 Visual Basic Code interview questions  Visual Basic Code Interview Questions (8)
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions (37)
Question
How to swap two variables, without using third variable ?
 Question Submitted By :: C-Code
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
Hi this question was asked in my interview.
Ans is :

a=a+b;
b=a-b;
a=a-b;

Any other solution?
 
Is This Answer Correct ?    1713 Yes 218 No
Swapna
 
Answer
# 2
use xor to swap

a = a^b
b= a^b
a= a^b
 
Is This Answer Correct ?    532 Yes 161 No
Guest
 
 
 
Answer
# 3
Ans is :

a=a*b;
b=a/b;
a=a/b;
 
Is This Answer Correct ?    525 Yes 178 No
Somasekhar
 
Answer
# 4
a=a+b;
b=a-b;
a=a-b
 
Is This Answer Correct ?    292 Yes 58 No
Kiran
 
Answer
# 5
x = x + y;

y = x - y;

x = x - y;
 
Is This Answer Correct ?    104 Yes 23 No
Mohit
 
Answer
# 6
Only the Xor answer (#2) is correct (in cases where the
variables are the same size). With all the other answers,
you could run into over/under flow problems.
A = 01111111
B = 01111101

A = A^B = 00000010
B = A^B = 01111111 (Original A)
A = A^B = 01111101 (Original B)
 
Is This Answer Correct ?    87 Yes 21 No
Bruce Tuskey
 
Answer
# 7
a=b-a+(b=a); 
Is This Answer Correct ?    113 Yes 76 No
Partha
 
Answer
# 8
2 one will work correct 
Is This Answer Correct ?    80 Yes 44 No
Gowtham
 
Answer
# 9
void main()
{
int a,b;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swap number is : %d %d",a,b);
getch();
}
 
Is This Answer Correct ?    39 Yes 9 No
Pavan.
 
Answer
# 10
Answer no 12 is wrong. the logic fails when a=0; 
Is This Answer Correct ?    41 Yes 17 No
Yash
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.   3
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft 7
find simple interest & compund interest   2
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.   6
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above HCL 1
What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }   1
how to return a multiple value from a function? Wipro 5
main() { extern int i; i=20; printf("%d",i); } Value-Labs 1
main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }   1
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }   1
Give a oneline C expression to test whether a number is a power of 2? Motorola 25
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above HCL 4
 
For more C Code Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com