C Aptitude

Document Sample
C Aptitude Powered By Docstoc
					                C Questions                        number/displacement from the base
                                                   address. So, indirecting it with * is same
Note : All the programs are tested under           as s[i]. i[s] may be surprising. But in the
Turbo C/C++ compilers.                             case of C it is same as s[i].
       It is assumed that,
             Programs run under DOS               3. main()
                 environment,                          {
             The underlying machine                        float me = 1.1;
                 is an x86 system,                          double you = 1.1;
             Program is compiled                           if(me==you)
                 using     Turbo   C/C++                            printf("I love U");
                 compiler.                                  else
       The program output may depend                                printf("I hate U");
on the information based on this                       }
assumptions (for example sizeof(int) == 2                   Answer:
may be assumed).                                                    I hate U
Predict the output or error(s) for the                              For       floating    point
following:                                         numbers (float, double, long double) the
                                                   values cannot be predicted exactly.
1. void main()                                     Depending on the number of bytes, the
    {                                              precession with of the value represented
        int const * p=5;                           varies. Float takes 4 bytes and long double
        printf("%d",++(*p));                       takes 10 bytes. So float stores 0.9 with less
    }                                              precision than long double.
        Answer:                                             Rule of Thumb:
                 Compiler error: Cannot                             Never compare or at-least
modify a constant value.                           be cautious when using floating point
        Explanation:                               numbers with relational operators (== , >,
                 p is a pointer to a               <, <=, >=,!= ) .
"constant integer". But we tried to change
the value of the "constant integer".               4. main()
2. main()                                                  static int var = 5;
    {                                                      printf("%d ",var--);
         char s[ ]="man";                                  if(var)
         int i;                                                      main();
         for(i=0;s[ i ];i++)                               }
         printf("\n%c%c%c%c",s[                i           Answer:
],*(s+i),*(i+s),i[s]);                                               54321
    }                                                      Explanation:
         Answer:                                                     When static storage class
         mmmm                                      is given, it is initialized once. The change
                  aaaa                             in the value of a static variable is retained
                  nnnn                             even between the function calls. Main is
         Explanation:                              also treated like any other ordinary
                  s[i], *(i+s), *(s+i), i[s] are   function, which can be called recursively.
all different ways of expressing the same
idea. Generally array name is the base             5. main()
address for that array. Here s is the base            {
address.        i       is      the       index           int c[ ]={2.8,3.4,4,6.7,5};
         int j,*p=c,*q=c;                               Explanation :
         for(j=0;j<5;j++) {                                     Logical operations always
                 printf(" %d ",*c);            give a result of 1 or 0 . And also the
                 ++q;      }                   logical AND (&&) operator has higher
         for(j=0;j<5;j++){                     priority over the logical OR (||) operator.
                 printf(" %d ",*p);            So the expression „i++ && j++ && k++’
                 ++p;      }                   is executed first. The result of this
    }                                          expression is 0 (-1 && -1 && 0 = 0).
                                               Now the expression is 0 || 2 which
         Answer:                               evaluates to 1 (because OR operator
         2222223465                            always gives 1 except for „0 || 0‟
         Explanation:                          combination- for which it gives 0). So the
                  Initially pointer c is       value of m is 1. The values of other
assigned to both p and q. In the first loop,   variables are also incremented by 1.
since only q is incremented and not c , the
value 2 will be printed 5 times. In second     8. main()
loop p itself is incremented. So the values         {
2 3 4 6 5 will be printed.                              char *p;
                                                        printf("%d                    %d
6. main()                                      ",sizeof(*p),sizeof(p));
   {                                                }
      extern int i;
      i=20;                                             Answer:
      printf("%d",i);                                   12
   }                                                    Explanation:
                                                                The sizeof() operator
         Answer:                               gives the number of bytes taken by its
                 Linker Error : Undefined      operand. P is a character pointer, which
         symbol '_i'                           needs one byte for storing its value (a
         Explanation:                          character). Hence sizeof(*p) gives a value
         extern storage class in the           of 1. Since it needs two bytes to store the
following declaration,                         address of the character pointer sizeof(p)
                         extern int i;         gives 2.
specifies to the compiler that the memory
for i is allocated in some other program       9. main()
and that address will be given to the             {
current program at the time of linking. But          int i=3;
linker finds that no other variable of name          switch(i)
i is available in any other program with              {
memory space allocated for it. Hence a                   default:printf("zero");
linker error has occurred .                              case 1: printf("one");
7. main()                                               case 2:printf("two");
   {                                                            break;
        int i=-1,j=-1,k=0,l=2,m;                       case 3: printf("three");
        m=i++&&j++&&k++||l++;                                   break;
        printf("%d      %d     %d %d                   }
%d",i,j,k,l,m);                                   }
   }                                                 Answer :
        Answer:                                               three
        00131                                        Explanation :
               The default case can be                  Explanation:
placed anywhere inside the loop. It is                           Here unary minus (or
executed only when all other cases doesn't     negation) operator is used twice. Same
match.                                         maths rules applies, ie. minus * minus=
10. main()                                              Note:
     {                                                           However you cannot give
          printf("%x",-1<<4);                  like --2. Because -- operator can only be
     }                                         applied to variables as a decrement
        Answer:                                operator (eg., i--). 2 is a constant and not a
                 fff0                          variable.
        Explanation :
                 -1       is      internally   13. #define int char
represented as all 1's. When left shifted          main()
four times the least significant 4 bits are        {
filled with 0's.The %x format specifier                int i=65;
specifies that the integer value be printed            printf("sizeof(i)=%d",sizeof(i));
as a hexadecimal value.                            }
11. main()                                                      sizeof(i)=1
    {                                                  Explanation:
        char string[]="Hello World";                            Since the #define replaces
         display(string);                              the string int by the macro char
    void display(char *string)                 14. main()
    {                                              {
         printf("%s",string);                         int i=10;
    }                                                 i=!i>14;
         Answer:                                      Printf ("i=%d",i);
                 Compiler Error : Type             }
         mismatch in redeclaration of                 Answer:
         function display                                      i=0
         Explanation :
                 In third line, when the
function display is encountered, the                   Explanation:
compiler doesn't know anything about the                        In the expression !i>14 ,
function display. It assumes the arguments     NOT (!) operator has more precedence
and return types to be integers, (which is     than „ >‟ symbol. ! is a unary logical
the default type). When it sees the actual     operator. !i (!10) is 0 (not of true is false).
function display, the arguments and type       0>14 is false (zero).
contradicts with what it has assumed
previously. Hence a compile time error         15. #include<stdio.h>
occurs.                                            main()
12. main()                                             char s[]={'a','b','c','\n','c','\0'};
    {                                                  char *p,*str,*str1;
       int c=- -2;                                     p=&s[3];
       printf("c=%d",c);                               str=p;
    }                                                  str1=s;
       Answer:                                         printf("%d",++*p + ++*str1-32);
                c=2;                               }
         Answer:                                         Answer:
                  77                                             Compiler Error
         Explanation:                                    Explanation:
         p is pointing to character '\n'. str1                   You should not initialize
is pointing to character 'a' ++*p. "p is                 variables in declaration
pointing to '\n' and that is incremented by
one." the ASCII value of '\n' is 10, which       18. #include<stdio.h>
is then incremented to 11. The value of              main()
++*p is 11. ++*str1, str1 is pointing to 'a'         {
that is incremented by 1 and it becomes                   struct xx
'b'. ASCII value of 'b' is 98.                            {
          Now performing (11 + 98 – 32),                      int x;
we get 77("M");                                               struct yy
          So we get the output 77 :: "M"                      {
(Ascii is 77).                                                     char s;
                                                                   struct xx *p;
16. #include<stdio.h>                                         };
    main()                                                    struct yy *q;
    {                                                     };
        int a[2][2][2] = { {10,2,3,4},               }
{5,6,7,8} };                                              Answer:
        int *p,*q;                                                 Compiler Error
        p=&a[2][2][2];                                    Explanation:
        *q=***a;                                                   The structure yy is nested
        printf("%d----%d",*p,*q);                within structure xx. Hence, the elements
    }                                            are of yy are to be accessed through the
        Answer:                                  instance of structure xx, which needs an
                 SomeGarbageValue---1            instance of yy to be known. If the instance
        Explanation:                             is created after defining the structure the
                 p=&a[2][2][2]          you      compiler will not know about the instance
declare only two 2D arrays, but you are          relative to xx. Hence for nested structure
trying to access the third 2D(which you          yy you have to declare member.
are not declared) it will print garbage
values. *q=***a starting address of a is         19. main()
assigned integer pointer. Now q is                   {
pointing to starting address of a. If you               printf("\nab");
print *q, it will print first element of 3D             printf("\bsi");
array.                                                  printf("\rha");
17. #include<stdio.h>                                   Answer:
    main()                                                       hai
    {                                                   Explanation:
        struct xx                                                \n - newline
        {                                                        \b - backspace
            int x=3;                                             \r - linefeed
            char name[]="hello";
         };                                      20. main()
        struct xx *s;                                {
        printf("%d",s->x);                               int i=5;
        printf("%s",s->name);                            printf("%d%d%d%d%d%d",i++,i
    }                                            --,++i,--i,i);
    }                                          converted to „!‟. Thus, we obtain value in
        Answer:                                p becomes “ibj!gsjfoet” and since p
                 45545                         reaches „\0‟ and p1 points to p thus
        Explanation:                           p1doesnot print anything.
                 The arguments in a
function call are pushed into the stack        23. #include <stdio.h>
from left to right. The evaluation is by           #define a 10
popping out from the stack. and the                main()
evaluation is from right to left, hence the        {
result.                                                #define a 50
21. #define square(x) x*x                          }
    main()                                             Answer:
    {                                                          50
        int i;                                         Explanation:
        i = 64/square(4);                                      The           preprocessor
        printf("%d",i);                        directives can be redefined anywhere in
    }                                          the program. So the most recently
        Answer:                                assigned value will be taken.
        Explanation:                           24. #define clrscr() 100
                 the macro call square(4)           main()
will substituted by 4*4 so the expression           {
becomes i = 64/4*4 . Since / and * has                  clrscr();
equal priority the expression will be                   printf("%d\n",clrscr());
evaluated as (64/4)*4 i.e. 16*4 = 64                }
22. main()                                                       100
    {                                                   Explanation:
         char *p="hai friends",*p1;                              Preprocessor executes as a
         p1=p;                                 seperate pass before the execution of the
         while(*p!='\0') ++*p++;               compiler. So textual replacement of
         printf("%s %s",p,p1);                 clrscr() to 100 occurs.The input program
    }                                          to compiler looks like this :
         Answer:                                                 main()
                 ibj!gsjfoet                                     {
         Explanation:                                               100;
                 ++*p++ will be parse in                            printf("%d\n",100);
the given order                                                  }
 *p that is value at the location                      Note:
    currently pointed by p will be taken                         100; is an executable
 ++*p the retrieved value will be             statement but with no action. So it doesn't
    incremented                                give any problem
 when ; is encountered the location will
    be incremented that is p++ will be         25. main()
    executed                                       {
Hence, in the while loop initial value                  printf("%p",main);
pointed by p is „h‟, which is changed to „i‟       }
by executing ++*p and pointer moves to                  Answer:
point, „a‟ which is similarly changed to „b‟                    Some address will be
and so on. Similarly blank space is            printed.
                 Function names are just                  }
addresses (just like array names are                     Answer:
addresses).                                                      4..2
main() is also a function. So the address of             Explanation:
function main will be printed. %p in printf                      the second pointer is of
specifies that the argument is an address.       char type and not a far pointer
They are printed as hexadecimal numbers.
                                                 30)    main()
27)     main()                                          {
        {                                                int i=400,j=300;
        clrscr();                                        printf("%d..%d");
        }                                               }
        clrscr();                                       Answer:
        Answer:                                         Explanation:
               No output/error                                   printf takes the values of
        Explanation:                                             the first two assignments
               The first clrscr() occurs                         of the program. Any
               inside a function. So it                          number of printf's may be
               becomes a function call.                          given. All of them take
               In the second clrscr(); is a                      only the first two values.
               function       declaration                        If more number of
               (because it is not inside                         assignments given in the
               any function).                                    program,then printf will
                                                                 take garbage values.
28)  enum                               colors
{BLACK,BLUE,GREEN}                               31)     main()
      main()                                            {
     {                                                   char *p;
printf("%d..%d..%d",BLACK,BLUE,GRE                      }
EN);                                                    Answer:
         return(1);                                     Explanation:
        }                                                        * is a dereference operator
        Answer:                                                  & is a reference operator.
                 0..1..2                                         They can be applied any
        Explanation:                                             number of times provided
                 enum assigns numbers                            it is meaningful. Here p
        starting from 0, if not explicitly                       points to        the first
        defined.                                                 character in the string
                                                                 "Hello". *p dereferences it
29)     void main()                                              and so its value is H.
        {                                                        Again & references it to
         char far *farther,*farthest;                            an     address    and     *
                                                                 dereferences it to the
                                                                 value H.
hest));                                          32)    main()
        {                                             {
            int i=1;                                          int i=5;
            while (i<=5)                                      printf("%d",i++ + ++i);
            {                                         }
              printf("%d",i);                         Answer:
              if (i>2)                                         Output      Cannot     be
                    goto here;                        predicted exactly.
              i++;                                    Explanation:
            }                                                  Side effects are involved
        }                                             in the evaluation of i
        {                                       35)   void main()
          here:                                       {
           printf("PP");                                     int i=5;
        }                                                    printf("%d",i+++++i);
        Answer:                                       }
                Compiler              error:          Answer:
        Undefined label 'here' in function                   Compiler Error
        main                                          Explanation:
        Explanation:                                         The expression i+++++i is
                Labels have functions                        parsed as i ++ ++ + i
                scope, in other words The                    which is an illegal
                scope of the labels is                       combination of operators.
                limited to functions . The
                label 'here' is available in    36)   #include<stdio.h>
                function fun() Hence it is            main()
                not visible in function               {
                main.                                 int i=1,j=2;
33)       main()                                       {
         {                                             case 1: printf("GOOD");
           static                        char                     break;
names[5][20]={"pascal","ada","cobol","fo               case j: printf("BAD");
rtran","perl"};                                                  break;
            int i;                                     }
            char *t;                                  }
            t=names[3];                               Answer:
            names[3]=names[4];                                 Compiler Error: Constant
            names[4]=t;                               expression required in function
            for (i=0;i<=4;i++)                        main.
                   printf("%s",names[i]);             Explanation:
         }                                                     The case statement can
         Answer:                                               have      only   constant
                   Compiler error: Lvalue                      expressions (this implies
         required in function main                             that we cannot use
         Explanation:                                          variable names directly so
                   Array names are pointer                     an error).
         constants. So it cannot be                   Note:
         modified.                                             Enumerated types can be
                                                      used in case statements.
34)     void main()
37)     main()                                            p=&s[3];
        {                                                 str=p;
        int i;                                            str1=s;
        printf("%d",scanf("%d",&i));   //                 printf("%d",++*p + ++*str1-32);
value 10 is given as input here                          }
        }                                                Answer:
        Answer:                                                 M
                1                                        Explanation:
        Explanation:                                            p is pointing to character
                Scanf returns number of                         '\n'.str1 is pointing to
                items successfully read                         character       'a'    ++*p
                and not 1/0. Here 10 is                         meAnswer:"p is pointing
                given as input which                            to '\n' and that is
                should have been scanned                        incremented by one." the
                successfully. So number                         ASCII value of '\n' is 10.
                of items read is 1.                             then it is incremented to
                                                                11. the value of ++*p is
38)    #define f(g,g2) g##g2                                    11.                  ++*str1
       main()                                                   meAnswer:"str1             is
       {                                                        pointing to 'a' that is
       int var12=100;                                           incremented by 1 and it
       printf("%d",f(var,12));                                  becomes 'b'. ASCII value
       }                                                        of 'b' is 98. both 11 and 98
       Answer:                                                  is added and result is
               100                                              subtracted from 32.
                                                                i.e. (11+98-32)=77("M");
39)    main()
       {                                         41)     #include<stdio.h>
       int i=0;                                          main()
       for(;i++;printf("%d",i)) ;                          struct xx
               printf("%d",i);                              {
       }                                                      int x=3;
       Answer:                                                char name[]="hello";
               1                                            };
       Explanation:                                      struct xx *s=malloc(sizeof(struct
               before entering into the          xx));
               for loop the checking                     printf("%d",s->x);
               condition is "evaluated".                 printf("%s",s->name);
               Here it evaluates to 0                    }
               (false) and comes out of                  Answer:
               the loop, and i is                                Compiler Error
               incremented (note the                     Explanation:
               semicolon after the for                           Initialization should not
               loop).                                            be done for structure
                                                                 members       inside  the
40)    #include<stdio.h>                                         structure declaration
       {                                         42)     #include<stdio.h>
         char s[]={'a','b','c','\n','c','\0'};           main()
         char *p,*str,*str1;                             {
      struct xx                                             The rule is that a variable
       {                                                    is available for use from
        int x;                                              the point of declaration.
        struct yy                                           Even though a is a global
         {                                                  variable, it is not available
           char s;                                          for main. Hence an error.
           struct xx *p;
         };                                   45)   main()
       struct yy *q;                                {
       };                                            extern out;
      }                                              printf("%d", out);
      Answer:                                       }
                Compiler Error                       int out=100;
      Explanation:                                  Answer:
                in the end of nested                         100
      structure yy a member have to be              Explanation:
      declared.                                              This is the correct way of
                                                    writing the previous program.
43)   main()
      {                                       46)   main()
       extern int i;                                {
       i=20;                                         show();
       printf("%d",sizeof(i));                      }
      }                                             void show()
      Answer:                                       {
               Linker error: undefined               printf("I'm the greatest");
      symbol '_i'.                                  }
      Explanation:                                  Answer:
               extern          declaration                   Compier error: Type
               specifies that the variable          mismatch in redeclaration of
               i is defined somewhere               show.
               else. The compiler passes            Explanation:
               the external variable to be                   When the compiler sees
               resolved by the linker. So                    the function show it
               compiler doesn't find an                      doesn't know anything
               error. During linking the                     about it. So the default
               linker searches for the                       return type (ie, int) is
               definition of i. Since it is                  assumed.      But     when
               not found the linker flags                    compiler sees the actual
               an error.                                     definition     of     show
                                                             mismatch occurs since it
44)   main()                                                 is declared as void. Hence
      {                                                      the error.
      printf("%d", out);                                     The solutions are as
      }                                             follows:
      int out=100;                                                    1. declare void
      Answer:                                                show() in main() .
              Compiler error: undefined                               2. define show()
      symbol out in function main.                           before main().
                         3. declare extern                   int a[ ] = {10,20,30,40,50},j,*p;
                 void show() before the                      for(j=0; j<5; j++)
                 use of show().                                {
                                                                     printf(“%d” ,*a);
47)      main( )                                                     a++;
         {                                                     }
           int         a[2][3][2]        =                     p = a;
         {{{2,4},{7,8},{3,4}},{{2,2},{2,3                     for(j=0; j<5; j++)
         },{3,4}}};                                              {
           printf(“%u     %u      %u   %d                            printf(“%d ” ,*p);
\n”,a,*a,**a,***a);                                                  p++;
           printf(“%u     %u      %u   %d                        }
    \n”,a+1,*a+1,**a+1,***a+1);                             }
          }                                                Answer:
         Answer:                                                     Compiler error: lvalue
                  100, 100, 100, 2                         required.
                  114, 104, 102, 3
         Explanation:                                      Explanation:
                  The given array is a 3-D                        Error is in line with
    one. It can also be viewed as a 1-D                           statement     a++.    The
    array.                                                        operand must be an lvalue
                                                                  and may be of any of
                                                                  scalar type for the any
                                                                  operator, array name only
          2 4    7   8  3                4   2   2     2    3     when 4subscripted is an
        100 102 104 106 108              110                      lvalue. Simply array name
112 114 116 118 120 122                                           is    a    non-modifiable
                 thus, for the first printf
                 statement a, *a, **a give       49)      main( )
                 address of first element .               {
                 since the indirection ***a                static int a[ ] = {0,1,2,3,4};
                 gives the value. Hence,                   int *p[ ] = {a,a+1,a+2,a+3,a+4};
                 the first line of the output.             int **ptr = p;
                 for the second printf a+1                 ptr++;
                 increases in the third                    printf(“\n %d %d %d”, ptr-p,
                 dimension thus points to        *ptr-a, **ptr);
                 value at 114, *a+1                        *ptr++;
                 increments in second                      printf(“\n %d %d %d”, ptr-p,
                 dimension thus points to        *ptr-a, **ptr);
                 104, **a +1 increments                    *++ptr;
                 the first dimension thus                  printf(“\n %d %d %d”, ptr-p,
                 points to 102 and ***a+1            *ptr-a, **ptr);
                 first gets the value at first             ++*ptr;
                 location        and      then             printf(“\n %d %d %d”, ptr-p,
                 increments it by 1. Hence,          *ptr-a, **ptr);
                 the output.                              }
48)    main( )                                                      111
       {                                                            222
                344                                              factor, so it becomes1004.
       Explanation:                                              Hence, the outputs for the
  Let us consider the array and the two                          third printf are ptr – p = 3,
       pointers with some address                                *ptr – a = 3, **ptr = 3.
                                                                 After execution of ++*ptr
                                                                 value in ptr remains the
                          0   1     2             3          4   same, the value pointed
                        100     102                              by      the     value       is
               104    106 108                                    incremented       by      the
                                                                 scaling factor. So the
                                                                 value in array p at
                                   p                             location 1006 changes
                           100      102   104     106        108 from 106 10 108,. Hence,
                         1000        1002                        the outputs for the fourth
1004   1006 1008                                                 printf are ptr – p = 1006 –
                                    ptr                          1000 = 3, *ptr – a = 108 –
                             1000                                100 = 4, **ptr = 4.
               After execution of the           50)        main( )
               instruction ptr++ value in                  {
               ptr becomes 1002, if                         char *q;
               scaling factor for integer                   int j;
               is 2 bytes. Now ptr – p is                   for (j=0; j<3; j++) scanf(“%s”
               value in ptr – starting          ,(q+j));
               location of array p, (1002                   for (j=0; j<3; j++) printf(“%c”
               – 1000) / (scaling factor)       ,*(q+j));
               = 1, *ptr – a = value at                     for (j=0; j<3; j++) printf(“%s”
               address pointed by ptr –         ,(q+j));
               starting value of array a,                  }
               1002 has a value 102 so                     Explanation:
               the value is (102 –                                Here we have only one
               100)/(scaling factor) = 1,                         pointer to type char and
               **ptr is the value stored in                       since we take input in the
               the location pointed by                            same pointer thus we keep
               the pointer of ptr = value                         writing over in the same
               pointed by value pointed                           location,     each     time
               by 1002 = value pointed                            shifting the pointer value
               by 102 = 1. Hence the                              by 1. Suppose the inputs
               output of the firs printf is                       are MOUSE,         TRACK
               1, 1, 1.                                           and VIRTUAL. Then for
               After execution of *ptr++                          the first input suppose the
               increments value of the                            pointer starts at location
               value in ptr by scaling                            100 then the input one is
               factor, so it becomes1004.                         stored as
               Hence, the outputs for the                         M      O      U     S      E    \0
               second printf are ptr – p =                         When the second input is
               2, *ptr – a = 2, **ptr = 2.                         given the pointer is
               After execution of *++ptr                           incremented as j value
               increments value of the                             becomes 1, so the input is
               value in ptr by scaling
                filled in memory starting                    static char *s[ ] = {“black”,
                from 101.                           “white”, “yellow”, “violet”};
                M      T     R      A       C     K     \0 char **ptr[ ] = {s+3, s+2, s+1, s},
                The third input       starts        ***p;
      filling from the location 102                          p = ptr;
                M       T       V       I       R      T **++p; A
                                                                U                 L       \0
                This is the final value                      printf(“%s”,*--*++p + 3);
           stored .                                         }
                The first printf prints the                 Answer:
      values at the position q, q+1 and                               ck
      q+2 = M T V                                           Explanation:
                The second printf prints                              In this problem we have
      three strings starting from                                     an array of char pointers
      locations q, q+1, q+2                                           pointing to start of 4
                 i.e       MTVIRTUAL,                                 strings. Then we have ptr
           TVIRTUAL and VIRTUAL.                                      which is a pointer to a
                                                                      pointer of type char and a
51)   main( )                                                         variable p which is a
      {                                                               pointer to a pointer to a
       void *vp;                                                      pointer of type char. p
       char ch = „g‟, *cp = “goofy”;                                  hold the initial value of
       int j = 20;                                                    ptr, i.e. p = s+3. The next
       vp = &ch;                                                      statement increment value
       printf(“%c”, *(char *)vp);                                     in p by 1 , thus now value
       vp = &j;                                                       of p = s+2. In the printf
       printf(“%d”,*(int *)vp);                                       statement the expression
       vp = cp;                                                       is evaluated *++p causes
       printf(“%s”,(char *)vp + 3);                                   gets value s+1 then the
      }                                                               pre decrement is executed
      Answer:                                                         and we get s+1 – 1 = s .
                g20fy                                                 the indirection operator
      Explanation:                                                    now gets the value from
                Since a void pointer is                               the array of s and adds 3
                used it can be type casted                            to the starting address.
                to any other type pointer.                            The string is printed
                vp = &ch stores address                               starting from this position.
                of char ch and the next                               Thus, the output is „ck‟.
                statement prints the value
                stored in vp after type             53)     main()
                casting it to the proper                    {
                data type pointer. the                       int i, n;
                output is „g‟. Similarly                     char *x = “girl”;
                the output from second                       n = strlen(x);
                printf is „20‟. The third                    *x = x[n];
                printf statement type casts                  for(i=0; i<n; ++i)
                it to print the string from                    {
                the 4th value hence the                               printf(“%s\n”,x);
                output is „fy‟.                                       x++;
52)   main ( )                                               }
      {                                                     Answer:
                 (blank space)                                   and this will disable all
                 irl                                      the assertions from the source
                 rl                                       code. Assertion
                 l                                               is a good debugging tool
                                                          to make use of.
                 Here a string (a pointer to      55)     main()
                 char) is initialized with a              {
                 value “girl”. The strlen                 int i=-1;
                 function returns the length              +i;
                 of the string, thus n has a              printf("i = %d, +i = %d \n",i,+i);
                 value 4. The next                        }
                 statement assigns value at               Answer:
                 the nth location („\0‟) to                          i = -1, +i = -1
                 the first location. Now the              Explanation:
                 string becomes “\0irl” .                            Unary + is the only
                 Now the printf statement                           dummy operator in C.
                 prints the string after each                       Where-ever it comes you
                 iteration it increments it                         can just ignore it just
                 starting position. Loop                            because it has no effect in
                 starts from 0 to 4. The                            the expressions (hence the
                 first time x[0] = „\0‟ hence                       name dummy operator).
                 it prints nothing and
                 pointer        value        is   56)    What are the files which are
                 incremented. The second          automatically opened when a C file is
                 time it prints from x[1] i.e     executed?
                 “irl” and the third time it             Answer:
                 prints “rl” and the last                        stdin,    stdout,     stderr
                 time it prints “l” and the              (standard            input,standard
                 loop terminates.                        output,standard error).
54)     int i,j;
        for(i=0;i<=10;i++)                        57) what will be the position of the file
        {                                         marker?
        j+=5;                                             a: fseek(ptr,0,SEEK_SET);
        assert(i<5);                                      b: fseek(ptr,0,SEEK_CUR);
        Answer:                                             Answer :
                 Runtime error: Abnormal                           a: The SEEK_SET sets
        program termination.                      the file position marker to the starting of
                          assert failed (i<5),    the file.
<file name>,<line number>                                          b: The SEEK_CUR sets
        Explanation:                              the file position marker to the current
                 asserts are used during          position
                 debugging to make sure                            of the file.
                 that certain conditions are
                 satisfied. If assertion fails,   58)     main()
                 the       program        will            {
                 terminate reporting the                  char name[10],s[12];
                 same. After debugging                    scanf(" \"%[^\"]\"",s);
                 use,                                     }
                          #undef NDEBUG                   How scanf will execute?
        Answer:                                                Compiler error (at line
              First it checks for the                   number 4): size of v is Unknown.
              leading white space and                   Explanation:
              discards    it.Then     it                       You can create a variable
              matches with a quotation                         of type void * but not of
              mark and then it reads all                       type void, since void is an
              character upto another                           empty type. In the second
              quotation mark.                                  line you are creating
                                                               variable vptr of type void
59)     What is the problem with the                           * and v of type void hence
following code segment?                                        an error.
        while               ((fgets(receiving
array,50,file_ptr)) != EOF)                     62)    main()
                          ;                            {
        Answer & Explanation:                          char *str1="abcd";
                 fgets returns a pointer. So           char str2[]="abcd";
                 the correct end of file               printf("%d                        %d
                 check is checking for !=       %d",sizeof(str1),sizeof(str2),sizeof("abcd"
                 NULL.                          ));
60)     main()                                         Answer:
        {                                                       255
        main();                                        Explanation:
        }                                                       In first sizeof, str1 is a
        Answer:                                                 character pointer so it
                Runtime error : Stack                           gives you the size of the
        overflow.                                               pointer     variable.      In
        Explanation:                                            second sizeof the name
                main function calls itself                      str2 indicates the name of
                again and again. Each                           the array whose size is 5
                time the function is                            (including       the      '\0'
                called its return address                       termination       character).
                is stored in the call stack.                    The third sizeof is similar
                Since there is no                               to the second one.
                condition to terminate the
                function call, the call         63)     main()
                stack     overflows       at            {
                runtime. So it terminates               char not;
                the program and results                 not=!2;
                in an error.                            printf("%d",not);
61)     main()                                          Answer:
        {                                                       0
        char *cptr,c;                                   Explanation:
        void *vptr,v;                                           ! is a logical operator. In
        c=10; v=0;                                              C the value 0 is
        cptr=&c; vptr=&v;                                       considered to be the
        printf("%c%v",c,v);                                     boolean value FALSE,
        }                                                       and any non-zero value is
        Answer:                                                 considered to be the
                                                                boolean value TRUE.
               Here 2 is a non-zero value                     (this    is     called    as
               so TRUE. !TRUE is                              "stringization" operation).
               FALSE (0) so it prints 0.                      So the string is as if it is
                                                              given as "%d==1 is %s".
64)    #define FALSE -1                                       The conditional operator(
       #define TRUE 1                                         ?: ) evaluates to "TRUE".
       #define NULL 0
       main() {                                66)    main()
         if(NULL)                                     {
                puts("NULL");                         int y;
         else if(FALSE)                               scanf("%d",&y); // input given is
                puts("TRUE");                  2000
         else                                        if( (y%4==0 && y%100 != 0) ||
                puts("FALSE");                 y%100 == 0 )
         }                                               printf("%d is a leap year");
       Answer:                                       else
                TRUE                                     printf("%d is not a leap year");
       Explanation:                                  }
                The input program to the             Answer:
                compiler after processing                     2000 is a leap year
                by the preprocessor is,              Explanation:
                main(){                                       An ordinary program to
                if(0)                                check if leap year or not.
                else if(-1)                    67)    #define max 5
                         puts("TRUE");                #define int arr1[max]
                else                                  main()
                         puts("FALSE");               {
                }                                     typedef char arr2[max];
                Preprocessor        doesn't           arr1 list={0,1,2,3,4};
                replace the values given              arr2 name="name";
                inside the double quotes.             printf("%d %s",list[0],name);
                The check by if condition             }
                is boolean value false so it          Answer:
                goes to else. In second if -                    Compiler error (in the line
                1 is boolean value true               arr1 list = {0,1,2,3,4})
                hence "TRUE" is printed.              Explanation:
                                                                arr2 is declared of type
65)    main()                                                   array of size 5 of
       {                                                        characters. So it can be
       int k=1;                                                 used to declare the
       printf("%d==1                   is                       variable name of the type
""%s",k,k==1?"TRUE":"FALSE");                                   arr2. But it is not the case
       }                                                        of arr1. Hence an error.
       Answer:                                        Rule of Thumb:
                1==1 is TRUE                                    #defines are used for
       Explanation:                                             textual         replacement
                When two strings are                            whereas typedefs are used
                placed     together   (or                       for declaring new types.
                separated by white-space)
                they are concatenated          68)    int i=10;
        main()                                                The variable i is a block
        {                                                     level variable and the
         extern int i;                                        visibility is inside that
          {                                                   block only. But the
            int i=20;                                         lifetime of i is lifetime of
                  {                                           the function so it lives
                   const volatile unsigned                    upto the exit of main
i=30;                                                         function. Since the i is
                 printf("%d",i);                              still allocated space, *j
                }                                             prints the value stored in i
           printf("%d",i);                                    since j points i.
        printf("%d",i);                        70)    main()
        }                                             {
        Answer:                                       int i=-1;
                30,20,10                              -i;
        Explanation:                                  printf("i = %d, -i = %d \n",i,-i);
                 '{' introduces new block             }
                 and thus new scope. In               Answer:
                 the innermost block i is                       i = -1, -i = 1
                 declared as,                         Explanation:
                         const      volatile                    -i is executed and this
        unsigned                                                execution doesn't affect
                which       is   a     valid                    the value of i. In printf
                declaration. i is assumed                       first you just print the
                of type int. So printf                          value of i. After that the
                prints 30. In the next                          value of the expression -i
                block, i has value 20 and                       = -(-1) is printed.
                so printf prints 20. In the
                outermost block, i is          71)    #include<stdio.h>
                declared as extern, so no             main()
                storage space is allocated             {
                for it. After compilation is             const int i=4;
                over the linker resolves it              float j;
                to global variable i (since              j = ++i;
                it is the only variable                  printf("%d %f", i,++j);
                visible there). So it prints           }
                i's value as 10.                      Answer:
                                                                 Compiler error
69)     main()                                        Explanation:
        {                                                        i is a constant. you cannot
          int *j;                                     change the value of constant
           int i=10;                           72)     #include<stdio.h>
           j=&i;                                       main()
           }                                           {
           printf("%d",*j);                              int a[2][2][2] = { {10,2,3,4},
        }                                      {5,6,7,8} };
        Answer:                                          int *p,*q;
                 10                                      p=&a[2][2][2];
        Explanation:                                     *q=***a;
        printf("%d..%d",*p,*q);                                int i;
      }                                                        struct aaa *next;
      Answer:                                                  };
                garbagevalue..1                        main()
      Explanation:                                     {
                p=&a[2][2][2]            you            struct aaa abc,def,ghi,jkl;
                declare only two 2D                     int x=100;
                arrays. but you are trying              abc.i=0;abc.prev=&jkl;
                to access the third                     abc.next=&def;
                2D(which you are not
                declared) it will print        def.i=1;def.prev=&abc;def.next=&ghi;
                garbage values. *q=***a                 ghi.i=2;ghi.prev=&def;
                starting address of a is                ghi.next=&jkl;
                assigned integer pointer.
                now q is pointing to           jkl.i=3;jkl.prev=&ghi;jkl.next=&abc;
                starting address of a.if you             x=abc.next->next->prev->next-
                print *q meAnswer:it will      >i;
                print first element of 3D                printf("%d",x);
                array.                                  }
73)   #include<stdio.h>                                          2
      main()                                            Explanation:
        {                                                        above all statements form
          register i=5;                        a double circular linked list;
          char j[]= "hello";                                     abc.next->next->prev-
           printf("%s %d",j,i);                         >next->i
      }                                                          this one points to "ghi"
      Answer:                                           node the value of at particular
                hello 5                                 node is 2.
                if you declare i as register   77)    struct point
                compiler will treat it as              {
                ordinary integer and it                int x;
                will take integer value. i             int y;
                value may be         stored            };
                either in register or in              struct point origin,*pp;
                memory.                               main()
74)   main()                                          pp=&origin;
      {                                               printf("origin
        int i=5,j=6,z;                         is(%d%d)\n",(*pp).x,(*pp).y);
        printf("%d",i+++j);                           printf("origin is (%d%d)\n",pp-
       }                                       >x,pp->y);
      Answer:                                         }
      Explanation:                                     Answer:
                the expression i+++j is                       origin is(0,0)
      treated as (i++ + j)                                    origin is(0,0)
76)   struct aaa{                                             pp is a pointer to
               struct aaa *prev;                              structure. we can access
              the elements of the                   printf("%c",x);
              structure either with arrow          }
              mark or with indirection             convert(z)
              operator.                            {
      Note:                                          return z-32;
               Since structure point is            }
      globally declared x & y are                  Answer:
      initialized as zeroes                                 Compiler error
78)   main()                                                declaration of convert and
      {                                            format of getc() are wrong.
       int i=_l_abc(10);
       printf("%d\n",--i);                   81)   main(int argc, char **argv)
      }                                            {
      int _l_abc(int i)                             printf("enter the character");
      {                                             getchar();
       return(i++);                                 sum(argv[1],argv[2]);
      }                                            }
      Answer:                                      sum(num1,num2)
               9                                   int num1,num2;
      Explanation:                                 {
               return(i++) it will first            return num1+num2;
               return    i    and    then          }
               increments. i.e. 10 will be         Answer:
               returned.                                    Compiler error.
79)   main()                                                argv[1] & argv[2] are
      {                                                     strings. They are passed to
       char *p;                                             the function sum without
       int *q;                                              converting it to integer
       long *r;                                             values.
       p++;                                  82)   # include <stdio.h>
       q++;                                        int one_d[]={1,2,3};
       r++;                                        main()
       printf("%p...%p...%p",p,q,r);               {
      }                                             int *ptr;
      Answer:                                       ptr=one_d;
               0001...0002...0004                   ptr+=3;
      Explanation:                                  printf("%d",*ptr);
               ++ operator when applied            }
               to pointers increments              Answer:
               address according to their                   garbage value
               corresponding data-types.           Explanation:
                                                            ptr pointer is pointing to
80)   main()                                       out of the array range of one_d.
       char c=' ',x,convert(z);              83)   # include<stdio.h>
       getc(c);                                    aaa() {
       if((c>='a') && (c<='z'))                      printf("hi");
       x=convert(c);                                }
      bbb(){                                          Answer:
       printf("hello");                                      0..0
       }                                              Explanation:
      ccc(){                                                 The value of i is 0. Since
       printf("bye");                                        this information is enough
       }                                                     to determine the truth
      main()                                                 value of the boolean
      {                                                      expression.     So      the
        int (*ptr[3])();                                     statement following the if
        ptr[0]=aaa;                                          statement is not executed.
        ptr[1]=bbb;                                          The values of i and j
        ptr[2]=ccc;                                          remain unchanged and get
        ptr[2]();                                            printed.
      Answer:                                 87)     main()
               bye                                    {
      Explanation:                                     int i;
               ptr is array of pointers to             i = abc();
               functions of return type                printf("%d",i);
               int.ptr[0] is assigned to              }
               address of the function                abc()
               aaa. Similarly ptr[1] and              {
               ptr[2] for bbb and ccc                  _AX = 1000;
               respectively. ptr[2]() is in           }
               effect of writing ccc(),               Answer:
               since ptr[2] points to ccc.                     1000
85)   #include<stdio.h>                                        Normally the return value
      main()                                                   from the function is
      {                                                        through the information
      FILE *ptr;                                               from the accumulator.
      char i;                                                  Here _AH is the pseudo
      ptr=fopen("zzz.c","r");                                  global variable denoting
      while((i=fgetch(ptr))!=EOF)                              the accumulator. Hence,
              printf("%c",i);                                  the     value    of    the
      }                                                        accumulator is set 1000 so
      Answer:                                                  the function returns value
              contents of zzz.c followed                       1000.
      by an infinite loop
      Explanation:                            88)      int i;
              The condition is checked                 main(){
              against EOF, it should be                int t;
              checked against NULL.                    for       (      t=4;scanf("%d",&i)-
86)   main()                                                     printf("%d--",t--);
      {                                                }
       int i =0;j=0;                                   // If the inputs are 0,1,2,3 find the
       if(i && j++)                           o/p
               printf("%d..%d",i++,j);                 Answer:
      printf("%d..%d,i,j);                                       4--0
      }                                                          3--1
                 2--2                            91)     In the following pgm add a stmt
         Explanation:                            in the function fun such that the address
                 Let us assume some x=           of
         scanf("%d",&i)-t    the values                  'a' gets stored in 'j'.
         during execution                                main(){
                 will be,                                  int * j;
               t     i    x                                void fun(int **);
               4     0    -4                               fun(&j);
               3     1    -2                              }
               2     2     0                              void fun(int **k) {
                                                           int a =0;
89)      main(){                                           /* add a stmt here*/
          int a= 0;int b = 20;char x =1;char              }
y =10;                                                   Answer:
          if(a,b,x,y)                                               *k = &a
              printf("hello");                           Explanation:
         }                                                          The argument of the
         Answer:                                 function is a pointer to a pointer.
         Explanation:                            92)     What are the following notations
                  The comma operator has         of defining functions known as?
                  associativity from left to             i. int abc(int a,float b)
                  right. Only the rightmost                        {
                  value is returned and the                        /* some code */
                  other values are evaluated                        }
                  and ignored. Thus the                  ii. int abc(a,b)
                  value of last variable y is                  int a; float b;
                  returned to check in if.                         {
                  Since it is a non zero                           /* some code*/
                  value if becomes true so,                        }
                  "hello" will be printed.               Answer:
                                                                   i. ANSI C notation
90)      main(){                                                   ii. Kernighan & Ritche
          unsigned int i;                                notation
                  printf("c aptitude");          93)    main()
         }                                              {
         Explanation:                                   char *p;
                  i is an unsigned integer. It          p="%d\n";
                  is compared with a signed             p++;
                  value. Since the both                 p++;
                  types     doesn't     match,          printf(p-2,300);
                  signed is promoted to                 }
                  unsigned      value.    The           Answer:
                  unsigned equivalent of -2                      300
                  is a huge value so                    Explanation:
                  condition becomes false                        The pointer points to %
                  and control comes out of                       since it is incremented
                  the loop.                                      twice     and      again
                                                                 decremented by 2, it
                 points to '%d\n' and 300 is                   returns the result of the
                 printed.                                      operation performed by
                                                               the function 'func'. The
94)     main(){                                                function func has two
         char a[100];                                          integer parameters. The
         a[0]='a';a[1]]='b';a[2]='c';a[4]='d';                 formal parameters are
         abc(a);                                               substituted as 3 for a and
        }                                                      6 for b. since 3 is not
        abc(char a[]){                                         equal to 6, a==b returns 0.
         a++;                                                  therefore the function
         printf("%c",*a);                                      returns 0 which in turn is
         a++;                                                  returned by the function
         printf("%c",*a);                                      'process'.
        Explanation:                             96)   void main()
                 The base address is                   {
                 modified only in function                     static int i=5;
                 and as a result a points to                   if(--i){
                 'b' then after incrementing                            main();
                 to 'c' so bc will be printed.                          printf("%d ",i);
95)      func(a,b)                                     }
         int a,b;                                      Answer:
         {                                                      0000
                   return( a= (a==b) );                Explanation:
         }                                                     The variable "I" is
         main()                                        declared as static, hence memory
         {                                             for I will be allocated for only
         int process(),func();                         once, as it encounters the
         printf("The value of process is %d            statement. The function main()
!\n ",process(func,3,6));                              will be called recursively unless I
         }                                             becomes equal to 0, and since
         process(pf,val1,val2)                         main() is recursively called, so the
         int (*pf) ();                                 value of static I ie., 0 will be
         int val1,val2;                                printed every time the control is
         {                                             returned.
         return((*pf) (val1,val2));
          }                                      97)   void main()
         Answer:                                       {
                  The value if process is 0 !                    int k=ret(sizeof(float));
         Explanation:                                            printf("\n here value is
                  The function 'process' has           %d",++k);
                  3 parameters - 1, a pointer          }
                  to another function 2 and            int ret(int ret)
                  3, integers. When this               {
                  function is invoked from                       ret += 2.5;
                  main,     the     following                    return(ret);
                  substitutions for formal             }
                  parameters take place:               Answer:
                  func for pf, 3 for val1 and                     Here value is 7
                  6 for val2. This function            Explanation:
                 The int ret(int ret), ie., the                 else
function name and the argument name can                                 printf("Forget
be the same.                                             it\n");
                 Firstly, the function ret()             }
        is called in which the sizeof(float)             Answer:
        ie., 4 is passed, after the first                       Ok here
        expression the value in ret will be              Explanation:
        6, as ret is integer hence the value                           Printf will return
        stored in ret will have implicit                               how           many
        type conversion from float to int.                             characters does it
        The ret is returned in main() it is                            print.       Hence
        printed after and preincrement.                                printing a null
                                                                       character returns
98)     void main()                                                    1 which makes
        {                                                              the if statement
                 char a[]="12345\0";                                   true, thus "Ok
                 int i=strlen(a);                                      here" is printed.
                 printf("here     in     3
        %d\n",++i);                               101)   void main()
        }                                                {
        Answer:                                                 void *v;
                 here in 3 6                                    int integer=2;
        Explanation:                                            int *i=&integer;
                 The char array 'a' will                        v=i;
        hold the initialized string, whose                      printf("%d",(int*)*v);
        length will be counted from 0 till               }
        the null character. Hence the 'I'                Answer:
        will hold the value equal to 5,                         Compiler       Error.   We
        after the pre-increment in the                   cannot apply indirection on type
        printf statement, the 6 will be                  void*.
        printed.                                         Explanation:
                                                                     Void pointer is a
99)     void main()                                                  generic pointer type.
        {                                                            No pointer arithmetic
                 unsigned giveit=-1;                                 can be done on it.
                 int gotit;                                          Void pointers are
                 printf("%u ",++giveit);                             normally used for,
                 printf("%u     \n",gotit=--                         1. Passing generic
        giveit);                                                         pointers        to
        }                                                                functions      and
        Answer:                                                          returning     such
                  0 65535                                                pointers.
        Explanation:                                                 2. As a intermediate
                                                                         pointer type.
100)    void main()                                                  3. Used when the
        {                                                                exact pointer type
               int i;                                                    will be known at
               char a[]="\0";                                            a later point of
               if(printf("%s\n",a))                                      time.
                        printf("Ok        here
        \n");                                     102)   void main()
       {                                                       be predicted. Still the
               int i=i++,j=j++,k=k++;                          outer printf         prints
               printf(“%d%d%d”,i,j,k);                         something and so returns
       }                                                       a non-zero value. So it
       Answer:                                                 encounters the break
                Garbage values.                                statement and comes out
       Explanation:                                            of the while statement.
                An identifier is available
       to use in program code from the        104)    main()
       point of its declaration.                      {
                So expressions such as i                       unsigned int i=10;
                =      i++    are     valid                    while(i-->=0)
                statements. The i, j and k                             printf("%u ",i);
                are automatic variables
                and so they contain some              }
                garbage value. Garbage                Answer:
                in is garbage out (GIGO).                    10 9 8 7 6 5 4 3 2 1 0
                                                      65535 65534…..
103)   void main()                                           Since i is an unsigned
       {                                                     integer it can never
                static int i=i++, j=j++,                     become negative. So the
       k=k++;                                                expression i-- >=0 will
                printf(“i = %d j = %d k =                    always be true, leading to
       %d”, i, j, k);                                        an infinite loop.
       Answer:                                105)    #include<conio.h>
                i=1j=1k=1                             main()
       Explanation:                                   {
                Since static variables are                     int x,y=2,z,a;
       initialized to zero by default.                         if(x=y%2) z=2;
104)   void main()                                             printf("%d %d ",z,x);
       {                                              }
              while(1){                                Answer:
                                                               Garbage-value 0
               if(printf("%d",printf("%d"             Explanation:
       )))                                                     The value of y%2 is 0.
                                break;                         This value is assigned to
                       else                                    x. The condition reduces
                                continue;                      to if (x) or in other words
              }                                                if(0) and so z goes
       }                                                       uninitialized.
       Answer:                                        Thumb Rule: Check all control
              Garbage values                  paths to write bug free code.
              The inner printf executes       106)    main()
              first to print some garbage             {
              value. The printf returns                        int a[10];
              no of characters printed                         printf("%d",*a+1-*a+3);
              and this value also cannot              }
       Answer:                                                 it has no effect on the
               4                                               expression and now the
       Explanation:                                            while loop is, while(i--
               *a and -*a cancels out.                         !=0) which is false and so
       The result is as simple as 1 + 3 = 4                    breaks out of while loop.
       !                                                       The value –1 is printed
                                                               due to the post-decrement
107)   #define prod(a,b) a*b                                   operator.
       {                                      113)    main()
               int x=3,y=4;                           {
               printf("%d",prod(x+2,y-                         float f=5,g=10;
       1));                                                    enum{i=10,j=20,k=50};
       }                                                       printf("%d\n",++k);
       Answer:                                                 printf("%f\n",f<<2);
               10                                              printf("%lf\n",f%g);
       Explanation:                                            printf("%lf\n",fmod(f,g));
               The macro expands and                    }
       evaluates to as:                                 Answer:
               x+2*y-1 => x+(2*y)-1 =>                            Line no 5: Error: Lvalue
       10                                               required
                                                                  Line no 6: Cannot apply
108)   main()                                           leftshift to float
       {                                                          Line no 7: Cannot apply
                unsigned int i=65000;                   mod to float
                while(i++!=0);                          Explanation:
                printf("%d",i);                                   Enumeration     constants
       }                                      cannot be modified, so you cannot apply
       Answer:                                ++.
               1                                                  Bit-wise operators and %
       Explanation:                           operators cannot be applied on float
              Note the semicolon after        values.
              the    while      statement.                        fmod() is to find the
              When the value of i             modulus values for floats as % operator is
              becomes 0 it comes out of       for ints.
              while loop. Due to post-
              increment on i the value        110)    main()
              of i while printing is 1.               {
                                                               int i=10;
109)   main()                                                  void pascal f(int,int,int);
       {                                                       f(i++,i++,i++);
                int i=0;                                       printf(" %d",i);
                while(+(+i--)!=0)                     }
                         i-=i++;                      void          pascal      f(integer
                printf("%d",i);                       :i,integer:j,integer :k)
       }                                              {
       Answer:                                                 write(i,j,k);
              -1                                      }
       Explanation:                                   Answer:
              Unary + is the only                              Compiler error: unknown
              dummy operator in C. So                 type integer
               Compiler            error:                 -128
       undeclared function write                   Explanation
       Explanation:                                       Notice          the
               Pascal keyword doesn‟t                     semicolon at the
       mean that pascal code can be                       end of the for
       used. It means that the function                   loop. THe initial
       follows Pascal argument passing                    value of the i is
       mechanism     in    calling    the                 set to 0. The inner
       functions.                                         loop executes to
                                                          increment       the
111)   void pascal f(int i,int j,int k)                   value from 0 to
       {                                                  127 (the positive
               printf(“%d %d %d”,i, j,                    range of char) and
       k);                                                then it rotates to
       }                                                  the negative value
       void cdecl f(int i,int j,int k)                    of -128. The
       {                                                  condition in the
               printf(“%d %d %d”,i, j,                    for loop fails and
       k);                                                so comes out of
       }                                                  the for loop. It
       main()                                             prints the current
       {                                                  value of i that is -
               int i=10;                                  128.
               printf(" %d\n",i);           113) main()
               i=10;                                  {
               f(i++,i++,i++);                            unsigned char i=0;
               printf(" %d",i);                           for(;i>=0;i++) ;
       }                                                  printf("%d\n",i);
       Answer:                                          }
               10 11 12 13                           Answer
               12 11 10 13                           infinite loop
       Explanation:                                  Explanation
               Pascal argument passing               The difference between
       mechanism forces the arguments       the previous question and this one
       to be called from left to right.     is that the char is declared to be
       cdecl is the normal C argument       unsigned. So the i++ can never
       passing mechanism where the          yield negative value and i>=0
       arguments are passed from right to   never becomes false so that it can
       left.                                come out of the for loop.

112). What is the output of the program     114) main()
       given below                                 {
                                                          char i=0;
              main()                                      for(;i>=0;i++) ;
                {                                         printf("%d\n",i);
                  signed char i=0;
                  for(;i>=0;i++) ;                 }
                  printf("%d\n",i);                Answer:
                }                                          Behavior            is
              Answer                        implementation dependent.
        Explanation:                                             The name error is
                 The detail if the                      used in the two meanings.
        char is signed/unsigned by                      One means that it is a
        default is implementation                       enumerator constant with
        dependent.        If    the                     value 1. The another use
        implementation treats the                       is that it is a type name
        char to be signed by                            (due to typedef) for enum
        default the program will                        errorType.      Given      a
        print –128 and terminate.                       situation the compiler
        On the other hand if it                         cannot distinguish the
        considers char to be                            meaning of error to know
        unsigned by default, it                         in what sense the error is
        goes to infinite loop.                          used:
        Rule:                                                            error g1;
                 You can write                                           g1=error;
        programs       that    have                              // which error it
        implementation dependent               refers in each case?
        behavior. But dont write                                 When           the
        programs that depend on                         compiler can distinguish
        such behavior.                                  between usages then it
                                                        will not issue error (in
115) Is the following statement a                       pure technical terms,
declaration/definition. Find what                       names can only be
does it mean?                                           overloaded in different
        int (*x)[10];                                   namespaces).
        Answer                                                   Note: the extra
                 Definition.                            comma in the declaration,
                 x is a pointer to                               enum
array of(size 10) integers.                             errorType{warning, error,
                Apply clock-wise                        is not an error. An extra
rule to find the meaning of this                        comma is valid and is
definition.                                             provided       just     for
116). What is the output for the
program given below
                                              117)         typedef struct error{int
           typedef            enum     warning, error, exception;}error;
errorType{warning,            error,                       main()
exception,}error;                                         {
           main()                                            error g1;
          {                                                  g1.error =1;
             error g1;
             g1=1;                             printf("%d",g1.error);
             printf("%d",g1);                             }
        Answer                                 Answer
                Compiler error:                                 1
Multiple declaration for error                 Explanation
         The three usages of name                     printf("%d %d\n",
errors can be distinguishable by              some ,thing);
the compiler at any instance, so              }
valid (they are in different
namespaces).                                  Answer:
                 Typedef      struct                 Compiler error :
         error{int warning, error,     undefined symbol some
         exception;}error;                    Explanation:
This error can be used only by                       This is a very
preceding the error by struct                        simple example
kayword as in:                                       for     conditional
                 struct        error                 compilation. The
         someError;                                  name something
                 typedef      struct                 is not already
         error{int warning, error,                   known to the
         exception;}error;                           compiler making
This can be used only after . (dot)                  the declaration
or -> (arrow) operator preceded by                   int some = 0;
the variable name as in :                            effectively
                 g1.error =1;                 removed from the source
                 typedef     struct    119)   #if something == 0
error{int      warning,      error,           int some=0;
exception;}error;                             #endif
This can be used to define
variables without using the                   main()
preceding struct keyword as in:               {
                 error g1;                            int thing = 0;
Since the compiler can perfectly                      printf("%d %d\n",
distinguish between these three               some ,thing);
usages, it is perfectly legal and             }
Note                                                 00
        This code is given here to            Explanation
just explain the concept behind. In                  This code is to
real programming don‟t use such                      show            that
overloading of names. It reduces                     preprocessor
the readability of the code.                         expressions are
Possible doesn‟t mean that we                        not the same as
should use it!                                       the        ordinary
                                                     expressions. If a
118)    #ifdef something                             name      is     not
        int some=0;                                  known            the
        #endif                                       preprocessor
                                                     treats it to be
        main()                                       equal to zero.
                 int thing = 0;        120). What is the output for the
                                       following program
         main()                                              Again arr2D[0] is
          {                                                  the another way
            int arr2D[3][3];                                 of telling *(arr2D
             printf("%d\n",                                  + 0). So the
((arr2D==* arr2D)&&(* arr2D ==                               expression
arr2D[0])) );                                                (*(arr2D + 0) ==
          }                                                  arr2D[0]) is true
         Answer                                              (1).
                 1                                           Since both parts
         Explanation                                         of the expression
                 This is due to the                          evaluates to true
                 close      relation                         the     result   is
                 between         the                         true(1) and the
                 arrays         and                          same is printed.
                 pointers.         N
                 dimensional                 121) void main()
                 arrays are made                  {
                 up     of    (N-1)                  if(~0 == (unsigned int)-1)
                 dimensional                         printf(“You can answer
                 arrays.                             this if you know how
                 arr2D is made up                    values are represented in
of a 3 single arrays that contains 3                 memory”);
integers each .                                   }
     arr2D                                                   You can answer
                                  arr2D[1]           this if you know how
                                  arr2D[2]           values are represented in
                                  arr2D[3]           Explanation
                                                             ~ (tilde operator
                                                             or         bit-wise
                The name arr2D                               negation operator)
                refers   to    the                           operates on 0 to
                beginning of all                             produce all ones
                the 3 arrays.                                to fill the space
                *arr2D refers to                             for an integer. –1
                the start of the                             is represented in
                first 1D array (of                           unsigned value as
                3 integers) that is                          all 1‟s and so both
                the same address                             are equal.
                as arr2D. So the
                expression (arr2D            122) int swap(int *a,int *b)
                == *arr2D) is true                    {
                (1).                                   *a=*a+*b;*b=*a-
                Similarly, *arr2D            *b;*a=*a-*b;
                is nothing but                        }
                *(arr2D + 0),                         main()
                adding a zero                         {
                doesn‟t    change                            int x=10,y=20;
                the                                          swap(&x,&y);
                      printf("x= %d y =                            works as a visual
       %d\n",x,y);                                                 clue for the reader
              }                                                    to see which
       Answer                                                      expression is first
              x = 20 y = 10                                        evaluated.
              This is one way of                   126)
              swapping two values.                        int aaa() {printf(“Hi”);}
              Simple checking will help                   int bbb(){printf(“hello”);}
              understand this.                            iny ccc(){printf(“bye”);}

       123)     main()                                    main()
               {                                          {
               char *p = “ayqm”;                          int ( * ptr[3]) ();
               printf(“%c”,++*(p++));                     ptr[0] = aaa;
               }                                          ptr[1] = bbb;
               Answer:                                    ptr[2] =ccc;
                       b                                  ptr[2]();
       124)    main()                              Answer:
               {                                           bye
                        int i=5;                   Explanation:
                                                          int (* ptr[3])() says that
        printf("%d",++i++);                               ptr is an array of pointers
                }                                         to functions that takes no
                Answer:                                   arguments and returns the
                         Compiler error:                  type     int.    By     the
Lvalue required in function main                          assignment ptr[0] = aaa; it
                Explanation:                              means that the first
                         ++i yields an                    function pointer in the
        rvalue. For postfix ++ to operate                 array is initialized with
        an lvalue is required.                            the address of the function
                                                          aaa. Similarly, the other
       125)    main()                                     two array elements also
               {                                          get initialized with the
                      char     *p       =                 addresses of the functions
               “ayqm”;                                    bbb and ccc. Since ptr[2]
                      char c;                             contains the address of the
                      c = ++*p++;                         function ccc, the call to
                      printf(“%c”,c);                     the function ptr[2]() is
               }                                          same as calling ccc(). So
               Answer:                                    it results in printing
                      b                                   "bye".
                      There     is     no   127)
                      difference                   main()
                      between         the          {
                      expression                   int i=5;
                      ++*(p++)       and           printf(“%d”,i=++i ==6);
                      ++*p++.                      }
                      Parenthesis just
        Answer:                                                No output
               1                                        Explanation:
        Explanation:                                           Ending the string constant
               The expression can be                           with \0 explicitly makes
               treated as i = (++i==6),                        no difference. So “some”
               because == is of higher                         and      “some\0”      are
               precedence      than      =                     equivalent. So, strcmp
               operator. In the inner                          returns 0 (false) hence
               expression, ++i is equal to                     breaking out of the while
               6 yielding true(1). Hence                       loop.
               the result.
                                                 131)   main()
128)    main()                                          {
        {                                                        char         str1[]       =
                 char p[ ]="%d\n";                      {„s‟,‟o‟,‟m‟,‟e‟};
                 p[1] = 'c';                                     char         str2[]       =
                 printf(p,65);                          {„s‟,‟o‟,‟m‟,‟e‟,‟\0‟};
        }                                                        while (strcmp(str1,str2))
        Answer:                                                  printf(“Strings are not
               A                                        equal\n”);
        Explanation:                                    }
               Due to the assignment                    Answer:
               p[1] = „c‟ the string                             “Strings are not equal”
               becomes, “%c\n”. Since                            “Strings are not equal”
               this string becomes the                           ….
               format string for printf                 Explanation:
               and ASCII value of 65 is                          If a string constant is
               „A‟, the same gets printed.                       initialized explicitly with
                                                                 characters, „\0‟ is not
129)    void ( * abc( int, void ( *def) () ) )                   appended automatically to
();                                                              the string. Since str1
                                                                 doesn‟t       have      null
         Answer::                                                termination,     it   treats
                   abc is a ptr to a function                    whatever the values that
                  which takes 2 parameters                       are in the following
                  .(a).      an        integer                   positions as part of the
                  variable.(b).      a ptrto a                   string until it randomly
                  funtion which returns                          reaches a „\0‟. So str1 and
                  void. the return type of                       str2 are not the same,
                  the function is void.                          hence the result.
                  Apply the clock-wise rule      132)   main()
to find the result.                                     {
                                                                int i = 3;
                                                                for                (;i++=0;)
130)    main()                                          printf(“%d”,i);
        {                                               }
        while (strcmp(“some”,”some\0”))
        printf(“Strings are not equal\n”);               Answer:
        }                                                      Compiler Error: Lvalue
        Answer:                                  required.
       Explanation:                                             the while loop, printing
                        As we know that                         the i value.
                        operators return       135)    main()
                        rvalues       and              {
                        hence it cannot                         int i=10,j=20;
                        appear on the left                      j = i, j?(i,j)?i:j:j;
                        hand side of an                         printf("%d %d",i,j);
                        assignment                     }
133)   void main()                                             10 10
       {                                               Explanation:
                int *mptr, *cptr;                              The Ternary operator ( ? :
                mptr                       =   ) is equivalent for if-then-else statement.
       (int*)malloc(sizeof(int));              So the question can be written as:
                printf(“%d”,*mptr);                            if(i,j)
                int          *cptr         =                       {
       (int*)calloc(sizeof(int),1);                            if(i,j)
                printf(“%d”,*cptr);                                j = i;
       }                                                       else
       Answer:                                                    j = j;
                garbage-value 0                                }
       Explanation:                                      else
                The       memory       space                   j = j;
                allocated by malloc is
                uninitialized,       whereas
                calloc       returns     the   136)    1. const char *a;
                allocated memory space                 2. char* const a;
                initialized to zeros.                  3. char const *a;
                                                       -Differentiate       the         above
134)   void main()                                     declarations.
              static int i;                            Answer:
              while(i<=10)                                   1. 'const' applies to char *
              (i>2)?i++:i--;                                 rather than 'a' ( pointer to
              printf(“%d”, i);                               a constant char )
       }                                                              *a='F'     : illegal
       Answer:                                                        a="Hi"       : legal
       Explanation:                                            2. 'const' applies to 'a'
              Since i is static it is                  rather than to the value of a
              initialized to 0. Inside the             (constant pointer to char )
              while loop the conditional                               *a='F'      : legal
              operator evaluates to                                    a="Hi"              :
              false, executing i--. This               illegal
              continues till the integer
              value rotates to positive                         3. Same as 1.
              value (32767). The while
              condition becomes false          137)    main()
              and hence, comes out of                  {
                int i=5,j=10;                             false && (anything) =>
                i=i&=j&&10;                  false where (anything) will not be
                printf("%d %d",i,j);         evaluated.
                                             139)      main()
       Answer:                                         {
              1 10                                              register int a=2;
       Explanation:                                             printf("Address of a =
              The expression can be          %d",&a);
              written                   as                      printf("Value of a           =
              i=(i&=(j&&10));        The     %d",a);
              inner expression (j&&10)              }
              evaluates to 1 because                Answer:
              j==10. i is 5. i = 5&1 is 1.                   Compier Error: '&' on
              Hence the result.                     register variable
                                                    Rule to Remember:
138)   main()                                                 & (address of ) operator
       {                                     cannot be applied on register variables.
              int i=4,j=7;
              j = j || i++ &&                140)      main()
printf("YOU CAN");                                     {
              printf("%d %d", i, j);                            float i=1.5;
        }                                                       switch(i)
       Answer:                                                           case            1:
              41                             printf("1");
       Explanation:                                                     case             2:
              The boolean expression         printf("2");
              needs to be evaluated only                                default              :
              till the truth value of the    printf("0");
              expression is not known. j                      }
              is not equal to zero itself            }
              means          that      the           Answer:
              expression‟s truth value is                     Compiler Error: switch
              1. Because it is followed              expression not integral
              by || and true || (anything)           Explanation:
              => true where (anything)                        Switch statements can be
              will not be evaluated. So      applied only to integral types.
              the remaining expression
              is not evaluated and so the    141)      main()
              value of i remains the                   {
              same.                                             extern i;
              Similarly      when     &&                        printf("%d\n",i);
              operator is involved in an                        {
              expression, when any of                                    int i=20;
              the operands become                                        printf("%d\n",i);
              false,       the      whole                       }
              expression‟s truth value                 }
              becomes false and hence                  Answer:
              the remaining expression                         Linker Error : Unresolved
              will not be evaluated.                   external symbol i
        Explanation:                                             pointer variable. Here the
               The identifier i is                               sizeof(a) where a is the
               available in the inner                            character array and the
               block and so using extern                         size of the array is 5
               has no use in resolving it.                       because      the     space
                                                                 necessary      for     the
142)    main()                                                   terminating         NULL
        {                                                        character should also be
                 int a=2,*f1,*f2;                                taken into account.
                 *f2+=*f2+=a+=2.5;               144) #define DIM( array, type)
                 printf("\n%d             %d     sizeof(array)/sizeof(type)
%d",a,*f1,*f2);                                          main()
       }                                                 {
       Answer:                                                    int arr[10];
                16 16 16                                          printf(“The dimension of
       Explanation:                                      the array is %d”, DIM(arr, int));
                f1 and f2 both refer to the              }
                same memory location a.                  Answer:
                So changes through f1                             10
                and f2 ultimately affects                Explanation:
                only the value of a.                              The size of integer array
                                                                  of 10 elements is 10 *
143)    main()                                                    sizeof(int). The macro
        {                                                         expands                  to
                 char *p="GOOD";                                  sizeof(arr)/sizeof(int) =>
                 char a[ ]="GOOD";                                10      *    sizeof(int)  /
                 printf("\n sizeof(p) = %d,                       sizeof(int) => 10.
                 sizeof(*p) = %d, strlen(p)
                 =      %d",        sizeof(p),   145)    int DIM(int array[])
                 sizeof(*p), strlen(p));                 {
                 printf("\n sizeof(a) = %d,              return sizeof(array)/sizeof(int );
strlen(a) = %d", sizeof(a), strlen(a));                  }
         }                                               main()
         Answer:                                         {
                 sizeof(p) = 2, sizeof(*p) =                      int arr[10];
1, strlen(p) = 4                                                  printf(“The dimension of
                 sizeof(a) = 5, strlen(a) = 4            the array is %d”, DIM(arr));
         Explanation:                                    }
                 sizeof(p) => sizeof(char*)              Answer:
=> 2                                                              1
                 sizeof(*p) => sizeof(char)              Explanation:
=> 1                                                              Arrays cannot be passed
                 Similarly,                                       to functions as arguments
                 sizeof(a) => size of the                         and only the pointers can
character array => 5                                              be passed. So the
                 When sizeof operator is                          argument is equivalent to
                 applied to an array it                           int * array (this is one of
                 returns the sizeof the                           the very few places where
                 array and it is not the                          [] and * usage are
                 same as the sizeof the                           equivalent). The return
                   statement       becomes,                     Using ^ like this is a way
                   sizeof(int *)/ sizeof(int)                   to swap two variables
                   that happens to be equal                     without using a temporary
                   in this case.                                variable and that too in a
                                                                single statement.
146)      main()                                                Inside      main(),     void
          {                                                     swap(); means that swap
                static                   int                    is a function that may take
a[3][3]={1,2,3,4,5,6,7,8,9};                                    any number of arguments
                int i,j;                                        (not no arguments) and
                static *p[]={a,a+1,a+2};                        returns nothing. So this
                for(i=0;i<3;i++)                                doesn‟t issue a compiler
                {                                               error     by      the    call
                         for(j=0;j<3;j++)                       swap(&x,&y); that has
                                                                two arguments.
          printf("%d\t%d\t%d\t%d\n",*(*(p                       This      convention       is
+i)+j),                                                         historically due to pre-
                                                                ANSI style (referred to as
          *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i                    Kernighan and Ritchie
));                                                             style) style of function
                }                                               declaration. In that style,
          }                                                     the swap function will be
          Answer:                                               defined as follows,
                          1     1    1      1                            void swap()
                          2     4    2      4                            int *a, int *b
                          3     7    3      7                            {
                          4     2    4      2                              *a ^= *b, *b ^=
                          5     5    5      5                   *a, *a ^= *b;
                          6     8    6      8                            }
                          7     3    7      3                   where the arguments
                          8     6    8      6                   follow the (). So naturally
                          9     9    9      9                   the declaration for swap
          Explanation:                                          will look like, void swap()
                 *(*(p+i)+j) is equivalent                      which means the swap
to p[i][j].                                                     can take any number of
147)      main()
          {                                     148)   main()
                   void swap();                        {
                   int x=10,y=8;                                 int i = 257;
                   swap(&x,&y);                                  int *iPtr = &i;
                   printf("x=%d y=%d",x,y);                      printf("%d          %d",
          }                                     *((char*)iPtr), *((char*)iPtr+1) );
          void swap(int *a, int *b)                     }
          {                                             Answer:
            *a ^= *b, *b ^= *a, *a ^= *b;                        11
          }                                             Explanation:
          Answer:                                                The integer value 257 is
                  x=10 y=8                                       stored in the memory as,
          Explanation:                                           00000001 00000001, so
                                                                 the individual bytes are
                 taken by casting it to char                   to it   is    00000010
                 * and get printed.                            00101100 => 556.

149)    main()                                 151)   #include <stdio.h>
        {                                             main()
                 int i = 258;                         {
                 int *iPtr = &i;                               char * str = "hello";
                 printf("%d           %d",                     char * ptr = str;
*((char*)iPtr), *((char*)iPtr+1) );                            char least = 127;
        }                                                      while (*ptr++)
        Answer:                                                   least = (*ptr<least )
                 21                                   ?*ptr :least;
        Explanation:                                           printf("%d",least);
                 The integer value 257 can            }
                 be represented in binary             Answer:
                 as, 00000001 00000001.                        0
                 Remember        that   the           Explanation:
                 INTEL machines are                            After „ptr‟ reaches the end
                 „small-endian‟ machines.                      of the string the value
                 Small-endian means that                       pointed by „str‟ is „\0‟. So
                 the lower order bytes are                     the value of „str‟ is less
                 stored in the higher                          than that of „least‟. So the
                 memory addresses and the                      value of „least‟ finally is
                 higher order bytes are                        0.
                 stored in lower addresses.
                 The integer value 258 is      152)   Declare an array of N pointers to
                 stored in memory as:                 functions returning pointers to
                 00000001 00000010.                   functions returning pointers to
150)    main()                                        Answer:
        {                                                     (char*(*)( )) (*ptr[N])( );
                 int i=300;
                 char *ptr = &i;               153)   main()
                 *++ptr=2;                            {
                 printf("%d",i);                               struct student
        }                                                      {
        Answer:                                                         char name[30];
               556                                                      struct date dob;
        Explanation:                                           }stud;
               The integer value 300 in                        struct date
               binary      notation   is:                           {
               00000001 00101100. It is                              int day,month,year;
               stored in memory (small-                              };
               endian) as: 00101100                               scanf("%s%d%d%d",
               00000001. Result of the                         stud.rollno,
               expression *++ptr = 2                           &student.dob.day,
               makes      the     memory                       &student.dob.month,
               representation         as:                      &student.dob.year);
               00101100 00000010. So                  }
               the integer corresponding              Answer:
              Compiler               Error:                     char name[30], rollno[6];
       Undefined structure date                                 }stud;
       Explanation:                                             FILE          *fp         =
              Inside the struct definition             fopen(“somefile.dat”,”r”);
              of „student‟ the member                           while(!feof(fp))
              of type struct date is                             {
              given.     The     compiler                               fread(&stud,
              doesn‟t       have        the   sizeof(stud), 1 , fp);
              definition of date structure                              puts(stud.name);
              (forward reference is not                         }
              allowed in C in this case)               }
              so it issues an error.                   Explanation:
                                                                        fread reads 10
154)   main()                                                           records and prints
       {                                                                the         names
                struct date;                                            successfully.     It
                struct student                                          will return EOF
       {                                                                only when fread
                char name[30];                                          tries    to    read
                struct date dob;                                        another     record
       }stud;                                                           and fails reading
                struct date                                             EOF            (and
                {                                                       returning EOF).
                      int day,month,year;                               So it prints the
                 };                                                     last record again.
                scanf("%s%d%d%d",                                       After this only the
                stud.rollno,                                            condition feof(fp)
                &student.dob.day,                                       becomes       false,
                &student.dob.month,                                     hence comes out
                &student.dob.year);                                     of the while loop.
       Answer:                                156) Is there any difference between
              Compiler               Error:   the two declarations,
       Undefined structure date                       1. int foo(int *arr[]) and
       Explanation:                                   2. int foo(int *arr[2])
              Only declaration of struct              Answer:
              date is available inside the                     No
              structure definition of                 Explanation:
              „student‟ but to have a                          Functions can only pass
              variable of type struct                          pointers and not arrays.
              date the definition of the                       The numbers that are
              structure is required.                           allowed inside the [] is
                                                               just for more readability.
155)   There were 10 records stored in                         So there is no difference
       “somefile.dat” but the following                        between       the     two
       program printed 11 names. What                          declarations.
       went wrong?
       void main()
       {                                      157) What is the subtle error in the
              struct student                  following code segment?
              {                                       void fun(int n, int arr[])
       {                                                   assert, the experssion
               int *p=0;                                   involves side-effects. So
               int i=0;                                    the behavior of the code
               while(i++<n)                                becomes different in case
                        p = &arr[i];                       of debug version and the
                        *p = 0;                            release    version     thus
       }                                                   leading to a subtle bug.
       Answer & Explanation:                       Rule to Remember:
                    If the body of the                     Don’t use expressions that
                    loop          never            have side-effects in assert
                    executes p is                  statements.
                    assigned          no
                    address. So p          160)    void main()
                    remains     NULL               {
                    where *p =0 may                int *i = 0x400; // i points to the
                    result in problem      address 400
                    (may     rise     to           *i = 0;         // set the value of
                    runtime        error   memory location pointed by i;
                    “NULL pointer                  }
                    assignment” and                Answer:
                    terminate        the                   Undefined behavior
                    program).                      Explanation:
                                                           The second statement
158) What is wrong with the following                      results    in    undefined
code?                                                      behavior because it points
      int *foo()                                           to some location whose
      {                                                    value    may      not    be
              int          *s          =                   available for modification.
      malloc(sizeof(int)100);                              This type of pointer in
              assert(s != NULL);                           which the non-availability
              return s;                                    of the implementation of
      }                                                    the referenced location is
      Answer & Explanation:                                known as 'incomplete
              assert macro should be                       type'.
              used for debugging and
              finding out bugs. The        161)     #define assert(cond) if(!(cond)) \
              check s != NULL is for                  (fprintf(stderr, "assertion failed:
              error/exception handling     %s, file %s, line %d \n",#cond,\
              and for that assert                    __FILE__,__LINE__), abort())
              shouldn‟t be used. A plain
              if and the corresponding              void main()
              remedy statement has to               {
              be given.                             int i = 10;
159) What is the hidden bug with the                    assert(i < 100);
following statement?                                else
               assert(val++ != 0);                      printf("This statement becomes
        Answer & Explanation:              else for if in assert macro");
               Assert macro is used for             }
               debugging and removed                Answer:
               in release version. In                         No output
        Explanation:                                      }
        The else part in which the printf is            Answer:
        there becomes the else for if in the                   Yes.
        assert macro. Hence nothing is                  Explanation:
        printed.                                               *b is a pointer to type
        The solution is to use conditional                     struct a and so is legal.
operator instead of if statement,                              The compiler knows, the
        #define assert(cond) ((cond)?(0):                      size of the pointer to a
        (fprintf (stderr, "assertion failed: \                 structure even before the
        %s, file %s, line %d \n",#cond,                        size of the structure
        __FILE__,__LINE__), abort()))                          is determined(as you
                                                               know the pointer to any
        Note:                                                  type is of same size). This
                 However this problem of                       type of structures is
                 “matching with nearest                        known          as     „self-
                 else” cannot be solved by                     referencing‟ structure.
                 the usual method of
                 placing the if statement        164)   Is the following code legal?
                 inside a block like this,              typedef struct a
                 #define assert(cond) { \                  {
                 if(!(cond)) \                                   int x;
                   (fprintf(stderr, "assertion                    aType *b;
        failed: %s, file %s, line %d                       }aType
        \n",#cond,\                                     Answer:
                  __FILE__,__LINE__),                            No
        abort()) \                                      Explanation:
                 }                                               The typename aType is
                                                                 not known at the point of
162)    Is the following code legal?                             declaring the structure
        struct a                                                 (forward references are
           {                                                     not made for typedefs).
                 int x;
                  struct a b;                    165)   Is the following code legal?
           }                                            typedef struct a aType;
        Answer:                                         struct a
                 No                                     {
        Explanation:                                             int x;
                 Is it not legal for a                           aType *b;
        structure to contain a member that              };
        is of the same                                  Answer:
                 type as in this case.                           Yes
                 Because this will cause                Explanation:
                 the structure declaration                       The typename aType is
                 to be recursive without                         known at the point of
                 end.                                            declaring the structure,
                                                                 because it is already
163)    Is the following code legal?                             typedefined.
        struct a
           {                                     166)   Is the following code legal?
                  int x;                                void main()
                 struct a *b;                           {
                 typedef struct a aType;                         a very big input (here,
                 aType someVariable;                             more than 100 chars) the
                 struct a                                        charactes will be written
        {                                                        past the input string.
                 int x;                                          When fgets is used with
                     aType *b;                                   stdin performs the same
        };                                                       operation as gets but is
        }                                                        safe.
               No                               169) Which version do you prefer of
        Explanation:                            the following two,
               When the declaration,                             1) printf(“%s”,str);      //
               typedef struct a aType;                   or the more curt one
               is encountered body of                            2) printf(str);
               struct a is not known. This               Answer & Explanation:
               is known as „incomplete                           Prefer the first one. If the
               types‟.                                           str contains any format
                                                                 characters like %d then it
167)     void main()                                             will result in a subtle bug.
         printf(“sizeof (void *) = %d \n“,      170)    void main()
sizeof( void *));                                       {
         printf(“sizeof (int *) = %d \n”,                      int i=10, j=2;
sizeof(int *));                                                int *ip= &i, *jp = &j;
         printf(“sizeof (double *) = %d                        int k = *ip/*jp;
\n”, sizeof(double *));                                        printf(“%d”,k);
         printf(“sizeof(struct unknown *) =             }
%d \n”, sizeof(struct unknown *));                      Answer:
         }                                                     Compiler              Error:
         Answer :                                       “Unexpected end of file in
                  sizeof (void *) = 2                   comment started in line 5”.
                  sizeof (int *) = 2                    Explanation:
                  sizeof (double *) = 2                                 The programmer
                  sizeof(struct unknown *)                              intended to divide
         = 2                                                            two integers, but
         Explanation:                                                   by the “maximum
                  The pointer to any type is                            munch” rule, the
         of same size.                                                  compiler     treats
                                                                        the        operator
168)     char inputString[100] = {0};                                   sequence / and *
         To get string input from the                                   as     /*    which
keyboard which one of the following is                                  happens to be the
better?                                                                 starting         of
                 1) gets(inputString)                                   comment.        To
                 2)        fgets(inputString,                           force what is
sizeof(inputString), fp)                                                intended by the
         Answer & Explanation:                                          programmer,
                 The second one is better                                        int k =
                 because gets(inputString)                              *ip/ *jp;
                 doesn't know the size of
                 the string passed and so, if
                                //     give                       requires 6 bytes to get
                        space      explicity                      stored.
                        separating / and *
                                //or             174)    main()
                                int k =                  {
                        *ip/(*jp);                                char a[4]="HELL";
                                //       put                      printf("%s",a);
                        braces to force the              }
                        intention                        Answer:
                        will solve the
                 problem.                                HELL%@!~@!@???@~~!
171)    void main()                                             The character array has
        {                                                       the memory just enough
        char ch;                                                to hold the string “HELL”
        for(ch=0;ch<=127;ch++)                                  and doesnt have enough
        printf(“%c %d \n“, ch, ch);                             space to store the
        }                                                       terminating null character.
        Answer:                                                 So it prints the HELL
                 Implementaion dependent                        correctly and continues to
        Explanation:                                            print garbage values till it
                 The char type may be                                   accidentally
                 signed or unsigned by                          comes across a NULL
                 default. If it is signed then                  character.
                 ch++ is executed after ch
                 reaches 127 and rotates         175)    main()
                 back to -128. Thus ch is                {
                 always smaller than 127.                         int a=10,*j;
                                                                  void *k;
172)    Is this code legal?                                       j=k=&a;
        int *ptr;                                                 j++;
        ptr = (int *) 0x400;                                      k++;
        Answer:                                                   printf("\n %u %u ",j,k);
                  Yes                                   }
        Explanation:                                    Answer:
                  The pointer ptr will point                     Compiler error: Cannot
        at the integer in the memory             increment a void pointer
        location 0x400.                                 Explanation:
                                                                 Void pointers are generic
173)    main()                                                   pointers and they can be
        {                                                        used only when the type is
                 char a[4]="HELLO";                              not known and as an
                 printf("%s",a);                                 intermediate       address
         }                                                       storage type. No pointer
         Answer:                                                 arithmetic can be done on
                Compiler error: Too many                         it and you cannot apply
initializers                                                     indirection operator (*) on
         Explanation:                                            void pointers.
                The array a is of size 4 but
                the    string      constant      176)    main()
                          extern int i;           Answer:
                 {        int i=20;                         Garbage values.
                  {                               Explanation:
                   const volatile unsigned                  Both the functions suffer from the
i=30; printf("%d",i);                             problem of dangling pointers. In
                 }                                someFun1() temp is a character array and
                          printf("%d",i);         so the space for it is allocated in heap and
                }                                 is initialized with character string “string”.
                  printf("%d",i);                 This is created dynamically as the function
                }                                 is called, so is also deleted dynamically on
        int i;                                    exiting the function so the string data is
                                                  not available in the calling function main()
177) Printf can be implemented by                 leading to print some garbage values. The
using __________ list.                            function someFun2() also suffers from the
        Answer:                                   same problem but the problem can be
                 Variable length argument         easily identified in this case.
178) char *someFun()
        char *temp = “string constant";
        return temp;
        int main()
        string constant
        The program suffers no problem
and gives the output correctly because the
character constants are stored in code/data
area and not allocated in stack, so this
doesn‟t lead to dangling pointers.

179)       char *someFun1()
           char temp[ ] = “string";
           return temp;
           char *someFun2()
           char     temp[      ]    =     {„s‟,
           return temp;
           int main()

Shared By:
Description: All the Questions And Aptitude related to C & C that has high probability of being asked in Placement Drive......!!!!