Searchhhhhhhh

Thursday, September 13, 2012

C Aptitude - I


C  Questions

Note : All the programs are tested under Turbo C/C++ compilers. 
It is assumed that,
Ø  Programs run under DOS environment,
Ø  The underlying machine is an x86 system,
Ø  Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

1.      void main()
{
            int  const * p=5;
            printf("%d",++(*p));
}
2.      main()
{
            char s[ ]="man";
            int i;
            for(i=0;s[ i ];i++)
            printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
3.      main()
{
            float me = 1.1;
            double you = 1.1;
            if(me==you)
printf("I love U");
else
                        printf("I hate U");
}
4.      main()
            {
            static int var = 5;
            printf("%d ",var--);
            if(var)
                        main();
            }
5.      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;     }
}
6.      main()
{
            extern int i;
            i=20;
printf("%d",i);
}
7.      main()
{
            int i=-1,j=-1,k=0,l=2,m;
            m=i++&&j++&&k++||l++;
            printf("%d %d %d %d %d",i,j,k,l,m);
}
8.      main()
{
            char *p;
            printf("%d %d ",sizeof(*p),sizeof(p));
}
9.      main()
{
            int i=3;
            switch(i)
             {
                default:printf("zero");
                case 1: printf("one");
                           break;
               case 2:printf("two");
                          break;
              case 3: printf("three");
                          break;
              } 
}
10.  main()
{
              printf("%x",-1<<4);
}
11.  main()
{
            char string[]="Hello World";
            display(string);
}
void display(char *string)
{
            printf("%s",string);
}
12.  main()
{
            int c=- -2;
            printf("c=%d",c);
}

13.  #define int char
main()
{
            int i=65;
            printf("sizeof(i)=%d",sizeof(i));
}
14.  main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
15.  #include<stdio.h>
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
16.  #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);
}
17.  #include<stdio.h>
main()
{
struct xx
{
      int x=3;
      char name[]="hello";
 };
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}
 18.  #include<stdio.h>
main()
{
struct xx
{
int x;
struct yy
{
char s;
            struct xx *p;
};
struct yy *q;
};
}
19.  main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
20.  main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
21.  #define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}
22.  main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s   %s",p,p1);
}
23.  #include <stdio.h>
#define a 10
main()
{
#define a 50
printf("%d",a);
}
24.  #define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}
25.  main()
{
printf("%p",main);
}
26.  main()
{
clrscr();
}
clrscr();
           
27.  enum colors {BLACK,BLUE,GREEN}
 main()
{
 
 printf("%d..%d..%d",BLACK,BLUE,GREEN);
  
 return(1);
}
28.  void main()
{
 char far *farther,*farthest;
 
 printf("%d..%d",sizeof(farther),sizeof(farthest));
  
 }
29.  main()
{
 int i=400,j=300;
 printf("%d..%d");
}
30.  main()
{
 char *p;
 p="Hello";
 printf("%c\n",*&*p);
} 
31.  main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
              goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}
32.  main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t;
    for (i=0;i<=4;i++)
            printf("%s",names[i]);
}
33.  void main()
{
            int i=5;
            printf("%d",i++ + ++i);
}
34.  void main()
{
            int i=5;
            printf("%d",i+++++i);
}
35.  #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
                break;
 case j:  printf("BAD");
               break;
 }
}

36.  main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}
37.  #define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
            }
38.  main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
39.  #include<stdio.h>
main()
{
  char s[]={'a','b','c','\n','c','\0'};
  char *p,*str,*str1;
  p=&s[3];
  str=p;
  str1=s;
  printf("%d",++*p + ++*str1-32);
}
40.  #include<stdio.h>
main()
{
  struct xx
   {
      int x=3;
      char name[]="hello";
   };
struct xx *s=malloc(sizeof(struct xx));
printf("%d",s->x);
printf("%s",s->name);
}
41.  #include<stdio.h>
main()
{
struct xx
 {
              int x;
              struct yy
               {
                 char s;
                 struct xx *p;
               };
                         struct yy *q;
            };
            }
42.  main()
{
 extern int i;
 i=20;
 printf("%d",sizeof(i));
}
43.  main()
{
printf("%d", out);
}
int out=100;
44.  main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
45.  main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}
46.  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);
       }
47.  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++;
      }
 }
48.  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);
}
49.  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));
}
50.  main( )
{
 void *vp;
 char ch = ‘g’, *cp = “goofy”;
 int j = 20;
 vp = &ch;
 printf(“%c”, *(char *)vp);
 vp = &j;
 printf(“%d”,*(int *)vp);
 vp = cp;
 printf(“%s”,(char *)vp + 3);
}


Answers for all the above are listed in : C Aptitude - I (Answers with Explanation)
                                                                          

No comments:

Post a Comment