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.

Thursday, May 12, 2016

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

Basic puzzle questions in C :: MUST KNOWN concepts to every programmers
Please try the below puzzles, which are very basic and also very useful in clearing the concept. Find the answer of all the puzzles in last page.

What will be the output of C puzzles below :-
Q 1>
#include<stdio.h>
int main()
{
  char *p=(char *)malloc(sizeof(char)*4); p="abc";
   char q[]="abc123";
     if(p[0]=q[0])
   {
       printf("%c, %c\n",*p,*q);
   }
}

Q 2>
#include<stdio.h>
int main()
{
  char *p=(char *)malloc(sizeof(char)*4); p="abc";
   char q[]="abc123";
     if(q[0]=p[0])
   {
       printf("%c, %c\n",*p,*q);
   }
}


Q 3>
main(){
    int num[] = {1,4,8,12,16};
    int *p,*q;
    int i;
    p =  num;
    q = num+2;
    i = *p++;
    printf("%d, %d, %d\n",i, *p, *q);
}

Q 4>
main(){
char *a[4]={"jaya","mahesh","chandra","swapant"};
int i = sizeof(a)/sizeof(char *);
printf("i = %d\n", i);
}

Q 5>
void fun(int *a, int *b)
{
 int *t;
 t=a;
 a=b;
 b=t;
printf(%d, %d\n",*a, *b);
}
main()
{
     int a=2;
     int b=3;
     fun(&a,&b);
     printf(%d, %d\n",a, b);
}

Q 6>
main(){
    int i =0;
    i = 2+3, 4>3, 3;
    printf("%d", i);
}

Q 7>
main(){
    printf("%u", -1);
}

Q 8>
struct ABC{
    float a:10;
    int b:6;
    char c;
} structure;
int main(){
    int i = sizeof(struct ABC);
    printf("%d", i);
}

Q 9>
#include<stdio.h>
int main() {
    char x[] = "abc";
    char *y = x;
    *y ='1';
    printf("%s",y);
}

Q 10>
main(){
    int i =0;
    i = 4>3, 2+3, 3;
    printf("%d", i);
}

Q 11>
#include<stdio.h>
void fun(int str,...);
int main()
{
    fun(6,"Hello","World","5","6","7","8");
    return 0;
}
void fun(int str,...)
{
    int i;
    int *n=&str;
    for(i=1;i<=str;i++)
    printf("%s ",*(n+i));
    return 0;

}

ANSWERS::
1> Segmentation Fault
   Reason-> We can't assign value to pointer valriable like this.
Different ways to assign value to pointer valriable. 
char *p="Hello World";
char *p=new char[11];  p="Hello World";
Wrong ways which lead to segmentation fault::
p[0]='a'; if(p[0]=='a'){};  *p='a'; if(*p=='a'){};
2> a, a

3> 1, 4, 8

4> 4

5> 3, 2
   2, 3
6> 5
   Explanation:: It stores only the first value.

7> 4294967295   :: As the size of unsigned int is from 0 to 4,294,967,295. -1 will print the back side of zero i.e. 4294967295.

8> Compilation Error:: We cannot use but filed for Float.

9> 1bc

10> 1  ::4>3 return 1 as it is true.

11> Hello World 5 6 7 8


Please comment if you find any thing wrong also please post the questions which you feel need to be shared.