1 November 2012

C Technical Questions Part 7

C Technical Questions


1. # include <stdio.h>
int one_d[]={1,2,3};
main()
{
 int *ptr;
 ptr=one_d;
 ptr+=3;
 printf("%d",*ptr);
}
//if ptr+=2 then 3 will printed
Ans: garbage

2. main()
{
 char *p;
 int *q;
 long *r;
 p=q=r=0;
 p++;
 q++;
 r++;
 printf("%p...%p...%p",p,q,r);
}

Ans : 1 2 4

3. #include<stdio.h>
main()
{
  int a[2][2][2] = { {10,2,3,4}, {5,6,7,8}  };
  int *p,*q;
  p=&a[2][2][2];
  *q=***a;
  printf("%d..%d",*p,*q);
}

Ans: 10 garbage

4. main()
            {
                int *j;
                {
                 int i=10;
                 j=&i;
                 }
                 printf("%d",*j);
}
Ans: 10

5. main()
            {
            char *cptr,c;
            void *vptr,v;
            c=10;  v=0;
            cptr=&c; vptr=&v;
            printf("%c%v",c,v);
            }

Ans: error

6. main()
{
 int  i, n;
 char *x = “girl”;
 n = strlen(x);
 *x = x[n];
 for(i=0; i<n; ++i)
   {
printf(“%s\n”,x);
x++;
   }
 }

Ans: irl
            rl
             l


7. main ( )
{
 static char *s[ ]  = {“black”, “white”, “yellow”, “violet”};
 char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
 p = ptr;
 **++p;
 printf(“%s”,*--*++p + 3);
}

Ans::ck
8. main( )
{
 void *vp;
 char ch = ‘g’, *cp = “goofy”;
 int j = 20;
 vp = &ch;
 printf(“%c”, *(char *)vp);//g
 vp = &j;
 printf(“%d”,*(int *)vp);//20
 vp = cp;
 printf(“%s”,(char *)vp + 3);//fy
}
Ans: g 20 fy


9. main( )
{
 char  *q;
 int  j;
 for (j=0; j<3; j++) scanf(“%s” ,(q+j));
 for (j=0; j<3; j++) printf(“%c” ,*(q+j));
 for (j=0; j<3; j++) printf(“%s” ,(q+j));
}

Ans: Error (Null pointer assignment)

10. main( )
{
 static int  a[ ]   = {0,1,2,3,4};
 int  *p[ ] = {a,a+1,a+2,a+3,a+4};
 int  **ptr =  p;
 ptr++;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 *ptr++;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 *++ptr;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 ++*ptr;
       printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);


11. main( )
{
  int a[ ] = {10,20,30,40,50},j,*p;
  for(j=0; j<5; j++)
    {
printf(“%d” ,*a);
a++;
    }
    p = a;
   for(j=0; j<5; j++)
      {
printf(“%d ” ,*p);
p++;
      }
 }

Ans: Error

12. main( )
{
  int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
  printf(“%u %u %u %d \n”,a,*a,**a,***a);
        printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
       }

Ans:  let base address be 1000.

1000 1000 1000 2
          1012 1004 1002 3


13. #include<stdio.h>
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8}  };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}

Ans: garbage garbage

14. main()
{
             int c[ ]={2.8,3.4,4,6.7,5};
             int j,*p=c,*q=c;
             for(j=0;j<5;j++)
 {
                        printf(" %d ",*c);
                        ++q;     }
             for(j=0;j<5;j++){
printf(" %d ",*p);
++p;     }
}

Ans: 2 2 2 2 2
        2 3 4 6 5


15. void main()
{
            int  const * p=5;
            printf("%d",++(*p));
}

Ans:  error


16.         # include<stdio.h>
aaa() {
  printf("hi");
 }
bbb(){
 printf("hello");
 }
ccc(){
 printf("bye");
 }
main()
{
  int (*ptr[3])();
  ptr[0]=aaa;
  ptr[1]=bbb;
  ptr[2]=ccc;
  ptr[2]();
}

Ans: bye.

17. In the following pgm add a  stmt in the function  fun such that the address of
'a' gets stored in 'j'.
main(){
  int * j;
  void fun(int **);
  fun(&j);
 }
 void fun(int **k) {
  int a =0;
  /* add a stmt here*/
 }

Ans: *k=&a;

18.         main(){
 char a[100];
 a[0]='a';a[1]='b';a[2]='c';a[4]='d';
 abc(a);
}
abc(char a[]){
 a++;
             printf("%c",*a);
 a++;
 printf("%c",*a);
}

Ans: b c

19.         func(a,b)
int a,b;
{
 return( a= (a==b) );
}

main()
{
int process(),func();
printf("The value of process is %d !\n ",process(func,3,6));
}

process(pf,val1,val2)
int (*pf) ();
int val1,val2;
{
return((*pf) (val1,val2));
 }

Ans: error


20.         #define prod(a,b) a*b
main()
{
            int x=3,y=4;
            printf("%d",prod(x+2,y-1));
}

Ans: 10

21. int swap(int *a,int *b)
{
 *a=*a+*b;*b=*a-*b;*a=*a-*b;
}
main()
{
                        int x=10,y=20;
            swap(&x,&y);
                        printf("x= %d y = %d\n",x,y);
}

Ans:  20 10


22. Explain the statement:       
void ( * abc( int, void ( *def) () ) ) ();





23. Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?


11. Is there any difference between the two declarations,
int foo(int *arr[]) and
int foo(int *arr[2])


24. char *someFun()
            {
            char *temp = “string constant";
            return temp;
            }
           
int main()
            {
            puts(someFun());
            }

Ans: string constant.

25. main()
 {
 int i=_l_abc(10);
printf(”%d\n”,–i);
 }

 int _l_abc(int i)
{
return(i++);
}

Ans: -10
26. main()
{
 char a[100];
a[0]=’a';
a[1]]=’b';a[2]=’c';a[4]=’d'; abc(a);
}

 abc(char a[])
{
 a++;
 printf(”%c”,*a);
 a++;
printf(”%c”,*a);
 }

27. void main()
 {
 static int i=5;
 if(–i)
{
main();
 printf(”%d “,i);
}
}

Ans:Infinite Loop No Output



1 comment:

  1. this is very poor part.....in compare to other parts...try with explanation frnd..thn it will b a good blog..jst tke as a suggestion..:)

    ReplyDelete

Followers