In this article I will show the most common programming questions and how to answer it perfectly
1-Swap two number(a,b) without using temp variable
//1st method
int a=9,b=7;
a=a+b-(b=a);
//------------------------------------------
//2nd method
a = a ^ b;
b = a ^ b; // b= (a^b)^b= a { As b^b = Zero }
a = a ^ b;
//------------------------------------------
the 2nd method is the prefered one as the 1st one including addition that may cause an overflow and the values will be incorrect.
2-Add Any Numbers Without “+” Operator
Bitwise operators can be used to perform the addition (+) operation as mentioned in below example:
//--------------------------------------------------------
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
//-----------------------------------------------------------
3-Write a program to print "Hello World" without using semicolon anywhere in the code
//----------------------------------------------------------
void main()
{
if(printf("Hello World")) //prints Hello World and returns 11
{//embty body}
}
//-----------------------------
The if statement checks for condition whether the return value of printf("Hello World") is greater than 0.
printf function returns the length of the string printed. Hence the statement if (printf("Hello World")) prints the string
"Hello World".
4-Explain the variable assignment in the declaration
int *(*p[10])(char *, char *);
It is an array of function pointers that returns an integer pointer. Each function has two arguments which in turn
are pointers to character type variable. p[0], p[1],....., p[9] are function pointers.
return type : integer pointer.
p[10] : array of function pointers
char * : arguments passed to the function
I hope that it was usful for you and I will update the article with more interesting questions as I can.
Comments
Post a Comment