Friday, May 13, 2016

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers :: SET-2

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers

Q1) What will be output of the program below?
void print (int n)
{
if (n>0)
{
printf(“hello”);
print(n-1);
}
printf(“world”);
}


Q2) Which statement will cause problem?
int main()
{
Statement 1-> const char *p1=”hello world”;
Statement 2-> char *const p2=”hello world”;
Statement 3-> p1=”welcome”;
Statement 4-> p2=”good bye”;
Statement 5-> *(p1+3) = ‘A’;
Statement 6­-> *(p2+3) = ‘A’;
return 0;
}

Q3)  What will be output ?
int function()
{   static int a=0;
    a=a+1;
    return a;
}
main()
{   function();
    function();
    printf("%d", function());
}

Q4)  What will be output ?
int main(){
    int x=5;
    printf("%d %d %d\n",x,x<<2,x>>2);
}

Q5)
int main(){
 int i=5;
 printf("%f",i);
    return 0;
}

Q6) What is the Error?
main()
{ int x=3;
return;
}

Q7) What will be output ?
struct ABC
{
    int var1,var2;
    void (*func);
};
int max (int x, int y)
{
     return (x>y)?x:y;
}
int main () {
   struct ABC abc;
   abc.var1=5; abc.var2=8;
   abc.func = max(abc.var1,abc.var2);
   printf("%d",abc.func);
   return 0;
}


---------------------------------------------------------------------------------------------------------
ANSWERS::
1)
It will print, N times “hello” and then by N+1 times “world”.
First it will call print function recursively N times and will print world after it comes out of the if block. “world” is printed N+1 times since when n ==0, it will not go inside if block and will print world and return.

2)
Answer:: Statement 4 and 6

3)
Answer::3

4)
Answer:: 5 20 1

5)
Answer::0.0033 or any decimal value

6)
Answer:: Return type of main is not defined even-though return statement is used

7)
Answer::8  :: This is the way in which we can define function for structure in C. We define pointer of function pointer instead of function itself in structure.

No comments:

Post a Comment