Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

C Questions: Predict The Output or Error(s) For The Following

The document contains 45 coding questions related to C programming. Each question contains sample code with errors or undefined behavior and asks the reader to predict the output or errors. The questions cover a wide range of C concepts like pointers, arrays, structures, typedefs, macros, conditional statements, loops and more.

Uploaded by

Anju Priya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

C Questions: Predict The Output or Error(s) For The Following

The document contains 45 coding questions related to C programming. Each question contains sample code with errors or undefined behavior and asks the reader to predict the output or errors. The questions cover a wide range of C concepts like pointers, arrays, structures, typedefs, macros, conditional statements, loops and more.

Uploaded by

Anju Priya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Placement Cell

C  Questions

 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");

-1-
Placement Cell

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);

-2-
Placement Cell

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");

-3-
Placement Cell

                                     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;

-4-
Placement Cell

                 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;

-5-
Placement Cell

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;

-6-
Placement Cell

                 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()

-7-
Placement Cell

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);

27)        main()

-8-
Placement Cell

clrscr();

clrscr(); 

28)        enum colors {BLACK,BLUE,GREEN}

 main()

   printf("%d..%d..%d",BLACK,BLUE,GREEN);

    return(1);

29)        void main()

 char far *farther,*farthest;

 printf("%d..%d",sizeof(farther),sizeof(farthest));

  

 } 

30)        main()

 int i=400,j=300;

 printf("%d..%d");

31)        main()

 char *p;

 p="Hello";

-9-
Placement Cell

 printf("%c\n",*&*p);

32)        main()

    int i=1;

    while (i<=5)

    {

       printf("%d",i);

       if (i>2)

              goto here;

       i++;

    }

fun()

   here:

     printf("PP");

33)        main()

   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};

    int i;

    char *t;

    t=names[3];

    names[3]=names[4];

- 10 -
Placement Cell

    names[4]=t;  

    for (i=0;i<=4;i++)

            printf("%s",names[i]);

34)        void main()

            int i=5;

            printf("%d",i++ + ++i);

35)        void main()

            int i=5;

            printf("%d",i+++++i);

}  

36)        #include<stdio.h>

main()

int i=1,j=2;

switch(i)

 {

 case 1:  printf("GOOD");

                break;

 case j:  printf("BAD");

               break;

 }

- 11 -
Placement Cell

37)        main()

int i;

printf("%d",scanf("%d",&i));  // value 10 is given as input here

38)        #define f(g,g2) g##g2

main()

int var12=100;

printf("%d",f(var,12));

            } 

39)        main()

int i=0;

for(;i++;printf("%d",i)) ;

printf("%d",i);

40)        #include<stdio.h>

main()

  char s[]={'a','b','c','\n','c','\0'};

  char *p,*str,*str1;

  p=&s[3];

- 12 -
Placement Cell

  str=p;

  str1=s;

  printf("%d",++*p + ++*str1-32);

41)        #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);

42)        #include<stdio.h>

main()

struct xx

 {

              int x;

              struct yy

               {

                 char s;

- 13 -
Placement Cell

                 struct xx *p;

               };

                         struct yy *q;

            };

            } 

43)        main()

 extern int i;

 i=20;

 printf("%d",sizeof(i));

44)        main()

printf("%d", out);

int out=100; 

45)        main()

 extern out;

 printf("%d", out);

 int out=100;                 

46)        main()

 show();

- 14 -
Placement Cell

void show()

 printf("I'm the greatest");

47)        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);

       } 

48)        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++;

      }

- 15 -
Placement Cell

 } 

49)        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);

50)        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));

51)        main( )

- 16 -
Placement Cell

 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);

52)        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);

53)        main()

 int  i, n;

 char *x = “girl”;

 n = strlen(x);

 *x = x[n];

- 17 -
Placement Cell

 for(i=0; i<n; ++i)

   {

printf(“%s\n”,x);

x++;

   }

 }

54)        int i,j;

            for(i=0;i<=10;i++)

            {

            j+=5;

            assert(i<5);

            }

55)        main()

            {

            int i=-1;

            +i;

            printf("i = %d, +i = %d \n",i,+i);

            } 

56)        What are the files which are automatically opened when a C file is executed?

57) what will be the position of the file marker?

            a: fseek(ptr,0,SEEK_SET);

            b: fseek(ptr,0,SEEK_CUR);

58)        main()

            {

            char name[10],s[12];

- 18 -
Placement Cell

            scanf(" \"%[^\"]\"",s);

            }

            How scanf will execute?  

59)        What is the problem with the following code segment?

            while ((fgets(receiving array,50,file_ptr)) != EOF)

                                    ;

60)        main()

            {

            main();

            }

61)        main()

            {

            char *cptr,c;

            void *vptr,v;

            c=10;  v=0;

            cptr=&c; vptr=&v;

            printf("%c%v",c,v);

            } 

62)        main()

            {

            char *str1="abcd";

            char str2[]="abcd";

            printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));

            }

- 19 -
Placement Cell

63)        main()

            {

            char not;

            not=!2;

            printf("%d",not);

            } 

64)        #define FALSE -1

            #define TRUE   1

            #define NULL   0

            main() {

               if(NULL)

                        puts("NULL");

               else if(FALSE)

                        puts("TRUE");

               else

                        puts("FALSE");

               } 

65)        main()

            {

            int k=1;

            printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");

            } 

66)        main()

            {

            int y;

- 20 -
Placement Cell

            scanf("%d",&y); // input given is 2000

            if( (y%4==0 && y%100 != 0) || y%100 == 0 )

                 printf("%d is a leap year");

            else

                 printf("%d is not a leap year");

            } 

67)        #define max 5

            #define int arr1[max]

            main()

            {

            typedef char arr2[max];

            arr1 list={0,1,2,3,4};

            arr2 name="name";

            printf("%d %s",list[0],name);

            } 

68)        int i=10;

            main()

            {

             extern int i;

              {

                 int i=20;

                        {

                         const volatile unsigned i=30;

                         printf("%d",i);

                        }

- 21 -
Placement Cell

                  printf("%d",i);

               }

            printf("%d",i);

            } 

69)        main()

            {

                int *j;

                {

                 int i=10;

                 j=&i;

                 }

                 printf("%d",*j);

70)        main()

            {

            int i=-1;

            -i;

            printf("i = %d, -i = %d \n",i,-i);

            } 

71)        #include<stdio.h>

main()

 {

   const int i=4;

   float j;

   j = ++i;

- 22 -
Placement Cell

   printf("%d  %f", i,++j);

 } 

72)        #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);

73)        #include<stdio.h>

main()

 {

    register i=5;

    char j[]= "hello";                    

     printf("%s  %d",j,i);

74)        main()

              int i=5,j=6,z;

              printf("%d",i+++j);

             }

                  

- 23 -
Placement Cell

76)        struct aaa{

struct aaa *prev;

int i;

struct aaa *next;

};

main()

 struct aaa abc,def,ghi,jkl;

 int x=100;

 abc.i=0;abc.prev=&jkl;

 abc.next=&def;

 def.i=1;def.prev=&abc;def.next=&ghi;

 ghi.i=2;ghi.prev=&def;

 ghi.next=&jkl;

 jkl.i=3;jkl.prev=&ghi;jkl.next=&abc;

 x=abc.next->next->prev->next->i;

 printf("%d",x);

77)        struct point

 {

 int x;

 int y;

 };

struct point origin,*pp;

main()

- 24 -
Placement Cell

pp=&origin;

printf("origin is(%d%d)\n",(*pp).x,(*pp).y);

printf("origin is (%d%d)\n",pp->x,pp->y);

}                        

78)        main()

 int i=_l_abc(10);

             printf("%d\n",--i);

int _l_abc(int i)

 return(i++);

79)        main()

 char *p;

 int *q;

 long *r;

 p=q=r=0;

 p++;

 q++;

 r++;

 printf("%p...%p...%p",p,q,r);

- 25 -
Placement Cell

 80)       main()

 char c=' ',x,convert(z);

 getc(c);

 if((c>='a') && (c<='z'))

 x=convert(c);

 printf("%c",x);

convert(z)

  return z-32;

81)        main(int argc, char **argv)

 printf("enter the character");

 getchar();

 sum(argv[1],argv[2]);

sum(num1,num2)

int num1,num2;

 return num1+num2;

- 26 -
Placement Cell

82)        # include <stdio.h>

int one_d[]={1,2,3};

main()

 int *ptr;

 ptr=one_d;

 ptr+=3;

 printf("%d",*ptr);

83)        # 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;

- 27 -
Placement Cell

  ptr[2]();

85)        #include<stdio.h>

main()

FILE *ptr;

char i;

ptr=fopen("zzz.c","r");

while((i=fgetch(ptr))!=EOF)

printf("%c",i);

}.

 86)        main()

 int i =0;j=0;

 if(i && j++)

            printf("%d..%d",i++,j);

printf("%d..%d,i,j);

}     

87)        main()

 int i;

 i = abc();

 printf("%d",i);

abc()

- 28 -
Placement Cell

 _AX = 1000;

88)        int i;

            main(){

int t;

for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))

                        printf("%d--",t--);

                        }

            // If the inputs are 0,1,2,3 find the o/p         

89)        main(){

  int a= 0;int b = 20;char x =1;char y =10;

  if(a,b,x,y)

        printf("hello");

 } 

90)        main(){

 unsigned int i;

 for(i=1;i>-2;i--)

                        printf("c aptitude");

91)        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 **);

- 29 -
Placement Cell

  fun(&j);

 }

 void fun(int **k) {

  int a =0;

  /* add a stmt here*/

 }     

92)        What are the following notations of defining functions known as?

i.      int abc(int a,float b)

                        {

                        /* some code */

 }

ii.    int abc(a,b)

        int a; float b;

                        {

                        /* some code*/

                        } 

93)        main()

char *p;

p="%d\n";

           p++;

           p++;

           printf(p-2,300);

- 30 -
Placement Cell

94)        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);

}               

95)        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;

- 31 -
Placement Cell

return((*pf) (val1,val2));

 } 

96)        void main()

            static int i=5;

            if(--i){

                        main();

                        printf("%d ",i);

            }

97)        void main()

            int k=ret(sizeof(float));

            printf("\n here value is %d",++k);

int ret(int ret)

            ret += 2.5;

            return(ret);

98)        void main()

            char a[]="12345\0";

            int i=strlen(a);

            printf("here in 3 %d\n",++i);

- 32 -
Placement Cell

}           

99)        void main()

            unsigned giveit=-1;

            int gotit;

            printf("%u ",++giveit);

            printf("%u \n",gotit=--giveit);

100)      void main()

            int i;

            char a[]="\0";

            if(printf("%s\n",a))

                        printf("Ok here \n");

            else

                        printf("Forget it\n");

- 33 -

You might also like