Monday, January 7, 2013

C apptitude



1.  void main()
    {
     int  const * p=5;
     printf("%d",++(*p));
}
a)Compiler error: Cannot modify a constant value.   B) 5           c)6
2.  main()
{
     char s[ ]="man";
     int i;
     for(i=0;s[ i ];i++)
     printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
a)mmmm aaaa nnnn                b)mmm aaa nnn           c)mm aa nn   d)man
3.  main()
{
     float me = 1.1;
     double you = 1.1;
     if(me==you)
printf("welcome");
else
            printf("sorry");
}
a)sorry             b)welcome                   c)error
4.  main()
     {
     static int var = 5;
     printf("%d ",var--);
     if(var)
            main();
     }
a)5 4 3 2 1                   b) 4 3 2 1 0                  c) 4 3 2 1
5.  main()
{
      int c[ ]={2.8,3.4,4,6.7,5};
      int j,*p=c,*q=c;
      for(j=0;j<5;j++) {
            printf(" %d ",*c);
             ++q;      }
      for(j=0;j<5;j++){
printf(" %d ",*p);
++p;   }
}
a)2 2 2 2 2 2 3 4 6 5            b)2 2 2 2  3 4 6 5                  c) 2  2 2 2 2 3 4 5 6
6.  main()
{
     extern int i;
     i=20;
printf("%d",i);
}
a) compiler error          b)Linker Error : Undefined symbol '_i'  c) 20
7.  main()
{
     int i=-1,j=-1,k=0,l=2,m;
     m=i++&&j++&&k++||l++;
     printf("%d %d %d %d %d",i,j,k,l,m);
}
a)0 0 1 3 1                   b) 0 0  0 1 3 1              c) 0 0 1 3 1 1
8.  main()
{
     char *p;
     printf("%d %d ",sizeof(*p),sizeof(p));
}
a)2 1                b)1 2                c)  01               d)  1 0
9.  main()
{
     int i=3;
     switch(i)
      {
         default:printf("zero");
         case 1: printf("one");
               break;
        case 2:printf("two");
              break;
       case 3: printf("three");
              break;
       }  
}
a) three            b) two              c) zero
10.main()
{
       printf("%x",-1<<4);
}
a)    eee0                      b)fff0              c) ggg0
11.main()
{
     char string[]="Hello World";
     display(string);
}
void display(char *string)
{
     printf("%s",string);
}
a)Compiler Error : Type mismatch in redeclaration of function display   b) Hello World   c)  Hello worl
12.main()
 
{
int c=- -2;
 
printf("c=%d",c);
 
}
a)c=2                           b)c=-2                          c)error
13.#define int char
 
main()
{
     int i=65;
     printf("sizeof(i)=%d",sizeof(i));
}
a) sizeof(i)=0               b)sizeof(i)=1                            c)sizeof(i)=2
14.main()
{
int i=10;
i=!i>14;
printf("i=%d",i);
}
a)i=0                                       b)i=10                                    c)i=13
15.#include‹stdio.h›
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
a) 76                b)78                 c)77
16.#include‹stdio.h›
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8}  };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
a) 10                                       b)5                                          c)SomeGarbageValue---1
17.#include‹stdio.h›
main()
{
struct xx
{
      int x=3;
      char name[]="hello";
 };
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}
a)Linker error                       b)compiler error                   c) hello
18.#include‹stdio.h›
main()
{
struct xx
{
int x;
struct yy
{
char s;
     struct xx *p;
};
struct yy *q;
};
}
a)Compiler Error                                                 b) invalid structure nesting                                c) arithmetic error
19.main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
a) ab si ha                    b) b I a             c)hai

20.main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
a)45554                                   b)54445                       c)45545
21.#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}
a)64                             b) 4                  c) arithmetic exception
22.main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s   %s",p,p1);
}
a)hai friends                b) hai               c)ibj!gsjfoet
23.#include‹stdio.h›
#define a 10
main()
{
#define a 50
printf("%d",a);
}
 
a) 10            b)50           c) 40
 
 
24.#define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}
a)loop occurs       b) 76          c)100

  
  
25.main()
{
printf("%p",main);
}
a) some string will be printed  b) Some address will be printed.  c) compiler error
26.main()
{
clrscr();
}
clrscr();
a)No output/error                                b)  76                                       c) system hangs
27.enum colors {BLACK,BLUE,GREEN}
 main()
{
  
 printf("%d..%d..%d",BLACK,BLUE,GREEN);
   
 return(1);
}
a)  BLACK, BLUE, GREEN                b)0.,1,.2                                 c) 0.0,  1.0, 2.0
28.void main()
{
 char far *farther,*farthest;
  
 printf("%d..%d",sizeof(farther),sizeof(farthest));
   
 }
a) 2, 4                          b)1, 4                           c)4..2
29.main()
{
 int i=400,j=300;
 printf("%d..%d");
}
a)400..300                   b)300...400                  c) 300              d) 400
30.main()
{
 char *p;
 p="Hello";
 printf("%c\n",*&*p);
}
a)Hello                                   b)H                                         c)Hell
31.main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
       goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}
a)Compiler error: Undefined label 'here' in function main                   b) Runtime error         c)Linker error
32.main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t;     
    for (i=0;i<=4;i++)
     printf("%s",names[i]);
}
a)Compiler error: Lvalue required in function main       b) pascal                  c)pascal,ada, cobol,fortran,perl
33.void main()
{
     int i=5;
     printf("%d",i++ + ++i);
}
a)  12                                                      b)11                        c)Output Cannot be predicted exactly.
34.void main()
{
     int i=5;
     printf("%d",i+++++i);
}
a)6                          b)8                                          c)14                        Compiler Error
35.#include‹stdio.h›
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
         break;
 case j:  printf("BAD");
        break;
 }
}
a)Compiler Error: Constant expression required in function main.
b)  GOOD                   c)BAD
36.main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}
a)1                                                          b)some address value                       c)10
37.#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
   }
a)12                                                        b)200                                     c)100
38.main()
{
int i=0;
 
for(;i++;printf("%d",i)) ;
printf("%d",i);
}
a)0                                                          b)1                                                          c)2
39.#include‹stdio.h›
main()
{
  char s[]={'a','b','c','\n','c','\0'};
  char *p,*str,*str1;
  p=&s[3];
  str=p;
  str1=s;
  printf("%d",++*p + ++*str1-32);
}
a)M                                                         b)b                          c) a                         d)0
40. main()
{
printf("%d", out);
}
int out=100;
a)Compiler error: undefined symbol out in function main.   b)100     c) 0
41.main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
a)undefined symbol out                     b)100                     c) 0
42.main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}
a)Compiler error:.        B)I am the greatest  c) Infinity times executes
43.main( )
{
  int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
  printf("%u %u %u %d \n",a,*a,**a,***a);
     printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
    }
a)100, 100, 100, 2  114, 104, 102, 3         b)  linker error        c) 2 4 8 8 3 4 2 4
44.main( )
{
  int a[ ] = {10,20,30,40,50},j,*p;
  for(j=0; j<5; j++)
    {
printf("%d" ,*a); 
a++;
    }
    p = a;
  for(j=0; j<5; j++) 
      {
printf("%d " ,*p); 
p++;
      }
 }
a)Compiler error: lvalue required.     B) compiler error: undefined symbol
c)compiler error : type mismatch
.
45.main( )
{
 void *vp;
 char ch = 'g', *cp = "goofy";
 int j = 20;
 vp = &ch;
 printf("%c", *(char *)vp);
 vp = &j;
 printf("%d",*(int *)vp);
 vp = cp;
 printf("%s",(char *)vp + 3);
}
a) gf720                                   b)g20fy                       c)goofy
46.main ( )
{
 static char *s[ ]  = {"black", "white", "yellow", "violet"};
 char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
 p = ptr;
 **++p;
 printf("%s",*--*++p + 3);
}
a)bk                        b)  ye                      c) vi                         d)ck
47.main()
{
 int  i, n;
 char *x = "girl";
 n = strlen(x);
 *x = x[n];
 for(i=0; i‹n; ++i)
   {
      printf("%s\n",x);
      x++;
   }
 }
a)(blank space)                  b) girl                           c) 4
irl
rl
l          



48.int i,j;
     for(i=0;i<=10;i++)
     {
     j+=5;
     assert(i<5);
     }
a)Runtime error:                                  b)compile time error           c) linker error
49.main()
     {
     int i=-1;
     +i;
     printf("i = %d, +i = %d \n",i,+i);
     }
a)  i = -1, +i = -1                                    b) i = 1, +i = -1                      c) i = -1, +i = 1
50. .main()
     {
     main();
     }
a)Runtime error : Stack overflow.   B)infinity times        c) compiler error
51.main()
     {
     char *cptr,c;
     void *vptr,v;
     c=10;  v=0;
     cptr=&c; vptr=&v;
     printf("%c%v",c,v);
     }
a) 0                                         b) garbage value                 c)Compiler error (at line number 4): size of v is Unknown.
52.main()
     {
     char *str1="abcd";
     char str2[]="abcd";
     printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
     }
a)2 5 5             b)   5  2  5                    c) 2 2 5            d) 5 5 2
53.main()
     {
     char not;
     not=!2;
     printf("%d",not);
     }
a) not                                      b) 1                         c)0
54.#define FALSE -1
     #define TRUE   1
     #define NULL   0
     main() {
        if(NULL)
           puts("NULL");
      else if(FALSE)
           puts("TRUE");
      else
           puts("FALSE");
      }
a)TRUE                                                  b)False                                  c)Null
55. main(){
         if(0)
                 puts("NULL");
  else if(-1)
                 puts("TRUE");
  else
                 puts("FALSE");
         }
a)True           b)False        c)Null
56.main()
   {
  int k=1;
  printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");
  }
a) 1==1 is True                                     b) 1==1 is False                   c) True:False
57.main()
     {
     int y;
   scanf("%d",&y); // input given is 2000
   if( (y%4==0 && y%100 != 0) || y%100 == 0 )
        printf("%d is a leap year");
  else
       printf("%d is not a leap year");
  }
a) 2000 is not a leap year                  b) 2000 is a leap year                         c) compiler error
58.#define max 5
  #define int arr1[max]
  main()
  {
  typedef char arr2[max];
arr1 list={0,1,2,3,4};
  arr2 name="name";
  printf("%d %s",list[0],name);
  }
a)Compiler error (in the line arr1 list = {0,1,2,3,4})          b)compiler error in the first line
c)No error/output
59.int i=10;
  main()
  {
   extern int i;
                   {
       int i=20;
         {
          const volatile unsigned i=30;
          printf("%d",i);
         }
        printf("%d",i);
     }
  printf("%d",i);
  }
            a)30,20,10                   b) 20,30,10                  c)10,20,30
60.main()
  {
      int *j;
      {
       int i=10;
       j=&i;
       }
     printf("%d",*j);
}
Answer:
a)10                                        b)some address                                  c)0
61. #include‹stdio.h›
main()
 {
   const int i=4;
   float j;
   j = ++i;
   printf("%d  %f", i,++j);
 }
a)Compiler error                      b)4, 5                           c)5,4
62.#include‹stdio.h›
main()
{
  int a[2][2][2] = { {10,2,3,4}, {5,6,7,8}  };
  int *p,*q;
  p=&a[2][2][2];
  *q=***a;
  printf("%d..%d",*p,*q);
}
a)garbagevalue..1                    b)1…..garbage value               c)0…1
63.#include‹stdio.h›
main()
  {
    register i=5;
    char j[]= "hello";                     
     printf("%s  %d",j,i);
}
a)5  hello                     b) hello                        c)hello 5
64.main()
{
       int i=5,j=6,z;
       printf("%d",i+++j);
      }
a)10                                                        b)11                                                        c)12
65.struct aaa{
struct aaa *prev;
int i;
struct aaa *next;
};
main()
{
 struct aaa abc,def,ghi,jkl;
 int x=100;
 abc.i=0;abc.prev=&jkl;
 abc.next=&def;
 def.i=1;def.prev=&abc;def.next=&ghi;
 ghi.i=2;ghi.prev=&def;
 ghi.next=&jkl;
 jkl.i=3;jkl.prev=&ghi;jkl.next=&abc;
 x=abc.next->next->prev->next->i;
 printf("%d",x);
}
a)error                                                    b)2                                                          c)1
66.main()
{
 int i=_l_abc(10);
      printf("%d\n",--i);
}
int _l_abc(int i)
{
 return(i++);
}
a)10                                        b)9                                          c)19
67.main()
{
 char *p;
 int *q;
 long *r;
 p=q=r=0;
 p++;
 q++;
 r++;
 printf("%p...%p...%p",p,q,r);
}
a)0001...0002...0004               b)0002….0001….0004  c)0001…0004….0002
68.main()
{
 char c=' ',x,convert(z);
 getc(c);
 if((c>='a') && (c<='z'))
 x=convert(c);
 printf("%c",x);
}
convert(z)
{
 return z-32;
}
a)Compiler error                      b)c                   c)d
69.main(int argc, char **argv)
{
 printf("enter the character");
 getchar();
 sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
 return num1+num2;
}
a) ascii value                        b)garbage value                  c)compiler error
70.# include ‹stdio.h›
int one_d[]={1,2,3};
main()
{
 int *ptr; 
 ptr=one_d;
 ptr+=3;
 printf("%d",*ptr);
}
a) 4                         b)0                          c)1                          d)garbage value
garbage value
71.# include‹stdio.h›
aaa() {
  printf("hi");
 }
bbb(){
 printf("hello");
 }
ccc(){
 printf("bye");
 }
main()
{
  int (*ptr[3])();
  ptr[0]=aaa;
  ptr[1]=bbb;
  ptr[2]=ccc;
  ptr[2]();
}
a) bye                                     b) hello                                  c)hi
72.#include‹stdio.h›
main()
{
FILE *ptr;
char i;
ptr=fopen("zzz.c","r");
while((i=fgetch(ptr))!=EOF)
printf("%c",i);
}
a)contents of zzz.c followed by an infinite loop   b) i            c)  null
73.main()
{
 int i =0;j=0;
 if(i && j++)
     printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
a)0..0                           b) 1..0                          c) 0….1
74.main()
{
 int i;
 i = abc();
 printf("%d",i);
}
abc()
{
 _AX = 1000;
}
a)10001                                 b)1000                   c)999
1000
75.int i;
            main(){
int t;
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))
                    printf("%d--",t--);
             }
     // If the inputs are 0,1,2,3 find the o/p
a) 4--0    3--1    2—2       b)  4….1    3….0     2…2                          c)4…2   3…1   2….1
76.main(){
  int a= 0;int b = 20;char x =1;char y =10;
  if(a,b,x,y)
        printf("hello");
 }
a)hello                                     b)error                         C)null
77.   In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'.
main(){
  int * j;
  void fun(int **);
  fun(&j);
 }
 void fun(int **k) {
  int a =0;
  /* add a stmt here*/
 }
a)*k = &a                    b)*k=a                         c)*k=0
78.main()
{
char *p;
p="%d\n";
            p++;
            p++;
            printf(p-2,300);
}
a)300                                                   b)302                               c)400



79 .void main()
{
     static int i=5;
     if(--i){
            main();
            printf("%d ",i);
     }
}
a) 0 0 1 0                     b) 0 0 01                      c)0 0 0 0
80.void main()
{
     int k=ret(sizeof(float));
     printf("\n here value is %d",++k);
}
int ret(int ret)
{
     ret += 2.5;
     return(ret);
}
a)Here value is 5.0      b)Here value is 7      c)Here value is 3.0
81.void main()
{
     char a[]="12345\0";
     int i=strlen(a);
     printf("here in 3 %d\n",++i);
}
a)here in 3 6        b) compiler error                  c) here in 3  7
82.void main()
{
     unsigned giveit=-1;
     int gotit;
     printf("%u ",++giveit);
     printf("%u \n",gotit=--giveit);
}
a)0 65535                    b)0  65534                   c)0  65536
83.void main()
{
     int i;
     char a[]="\0";
     if(printf("%s\n",a))
            printf("Ok here \n");
     else
            printf("Forget it\n");
}
a)Forget                        b)Ok here                   c) Ok
84.void main()
{
     void *v;
     int integer=2;
     int *i=&integer;
     v=i;
     printf("%d",(int*)*v);
}
a) compiler error                  b)  2                        c) 4
85.void main()
{
     int i=i++,j=j++,k=k++;
printf("%d%d%d",i,j,k);
}
a) 0                                         b) 1                         c)Garbage values
86.void main()
{
     static int i=i++, j=j++, k=k++;
printf("i = %d j = %d k = %d", i, j, k);
}
a) i = 0 j = 1 k = 1                   b) i = 1 j = 1 k = 1       c) i = 1 j = 0 k = 1
87.void main()
{
     while(1){
            if(printf("%d",printf("%d")))
                    break;
            else
                    continue;
     }
}
a)Garbage values                               b) 1                                         c)0
88.main()
{
     unsigned int i=10;
     while(i-->=0)
            printf("%u ",i);
 
}
a)10 9 8 7 6 5 4 3 2 1 0 65535 65534...    b) compiler error      c) 10
89.#include‹conio.h›
main()
{
     int x,y=2,z,a;
     if(x=y%2) z=2;
     a=2;
     printf("%d %d ",z,x);
}
a)Garbage-value 0                   b) 1                  c)2
90.main()
{
     int a[10];
     printf("%d",*a+1-*a+3);
}
a)3                                                          b)5                                          c)4
91.#define prod(a,b) a*b
main() 
{
     int x=3,y=4;
     printf("%d",prod(x+2,y-1));
}
a)5                          b)3                          c)0                          d)10
    92.        main()
{
     unsigned int i=65000;
     while(i++!=0);
     printf("%d",i);
}
a) 1                         b) 0                         c) 65000
93.main()
{
     int i=0;
     while(+(+i--)!=0)
            i-=i++;
     printf("%d",i);
}
a)1                          b)0                          c)-1
94. main()
{
  int i=10;
  void pascal f(int,int,int);
f(i++,i++,i++);
  printf(" %d",i);
}
void pascal f(integer :i,integer:j,integer :k)
{ 
write(i,j,k); 
}
a)compiler error                   b)No output/error                                 c) Linker error
95.   What is the output of the program given below
main()
    {
       signed char i=0;
       for(;i>=0;i++) ;
       printf("%d\n",i);
    }
a)-128                          b)128                           c) ‘\0’
96.main()
    {
       unsigned char i=0;
       for(;i>=0;i++) ;
       printf("%d\n",i);
    }
a)infinite loop                          b) compiler error                      c)No op/error
97.   What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
     main()
    {
        error g1;
        g1=1; 
        printf("%d",g1);
     }
a)compiler error.                                  B) 1                         c) 0        
).
98.typedef struct error{int warning, error, exception;}error;
     main()
    {
        error g1;
        g1.error =1; 
        printf("%d",g1.error);
     }
a)1                                          b) 2                                         c)0                          d)error
99.#ifdef something
int some=0;
#endif
 
main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
a)Compiler error                      b)0                   c) null
100.           #if something == 0
int some=0;
#endif
 
main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
a)0 1                b)0                   c0 0














101.           void main()
         {
if(~0 == (unsigned int)-1)
printf("You can answer this if you know how values are represented in memory");
         }
Answer
You can answer this if you know how values are represented in memory
Explanation
~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. -1 is represented in unsigned value as all 1's and so both are equal.
102.           int swap(int *a,int *b)
{
 *a=*a+*b;*b=*a-*b;*a=*a-*b;
}
main()
{
         int x=10,y=20;
  swap(&x,&y);
         printf("x= %d y = %d\n",x,y);
}
Answer:
x = 20 y = 10
Explanation
This is one way of swapping two values. Simple checking will help understand this.
121. main()
{    
char *p = "ayqm";
printf("%c",++*(p++));
}
Answer:
b
122. main()
     {
      int i=5;
      printf("%d",++i++);
} 
Answer:
Compiler error: Lvalue required in function main
Explanation:
++i yields an rvalue. For postfix ++ to operate an lvalue is required.
123. main()
{
char *p = "ayqm";
char c;
c = ++*p++;
printf("%c",c);
}
Answer:
b
Explanation:
There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated.
124. int aaa() {printf("Hi");}
int bbb(){printf("hello");}
iny ccc(){printf("bye");}
 
main()
{
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:
bye
Explanation:
int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized with the address of the function aaa. Similarly, the other two array elements also get initialized with the addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to the function ptr[2]() is same as calling ccc(). So it results in printing "bye".
125. main()
{
int i=5;
printf("%d",i=++i ==6);
}
Answer:
1
Explanation:
The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.
126. main()
{
            char p[ ]="%d\n";
p[1] = 'c';
printf(p,65);
}
Answer:
A
Explanation:
Due to the assignment p[1] = 'c' the string becomes, "%c\n". Since this string becomes the format string for printf and ASCII value of 65 is 'A', the same gets printed.
127. void ( * abc( int, void ( *def) () ) ) ();
Answer:
abc is a ptr to a function which takes 2 parameters (a) an integer variable (b)a ptrto a funtion which returns void. the return type of the function is void.
Explanation:
Apply the clock-wise rule to find the result.
128. main()
{
while (strcmp("some","some\0")) 
printf("Strings are not equal\n");
     }
Answer:
No output
Explanation:
Ending the string constant with \0 explicitly makes no difference. So "some" and "some\0" are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop.
129. main()
{
char str1[] = {'s','o','m','e'};
char str2[] = {'s','o','m','e','\0'};
while (strcmp(str1,str2)) 
printf("Strings are not equal\n");
}
Answer:
"Strings are not equal"
"Strings are not equal" ...
Explanation:
If a string constant is initialized explicitly with characters, '\0' is not appended automatically to the string. Since str1 doesn't have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a '\0'. So str1 and str2 are not the same, hence the result.
130. main()
{
int i = 3;
for (;i++=0;) printf("%d",i);
}
Answer:
Compiler Error: Lvalue required.
Explanation:
As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation.
131. void main()
{
int *mptr, *cptr;
mptr = (int*)malloc(sizeof(int));
printf("%d",*mptr);
int *cptr = (int*)calloc(sizeof(int),1);
printf("%d",*cptr);
}
Answer:
garbage-value 0
Explanation:
The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros.
132. void main()
{
static int i;
while(i<=10)
(i>2)?i++:i--;
     printf("%d", i);
}
Answer:
32767
Explanation:
Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value.
133. main()
{
            int i=10,j=20;
     j = i, j?(i,j)?i:j:j;
            printf("%d %d",i,j);
}
Answer:
10 10
Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
               {
if(i,j)
              j = i;
         else
             j = j;                     
         }
     else
         j = j;
134. 
1.     const char *a;
2.     char* const a;
3.     char const *a;

-Differentiate the above declarations.
Answer:
4.     'const' applies to char * rather than 'a' ( pointer to a constant char )
*a='F' : illegal
a="Hi" : legal
5.     'const' applies to 'a' rather than to the value of a (constant pointer to char )
*a='F' : legal
a="Hi" : illegal
6.     Same as 1.
135. main()
{
            int i=5,j=10;
     i=i&=j&&10;
            printf("%d %d",i,j);
}
Answer:
1 10
Explanation:
The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.




136.      main()
{
            int i=4,j=7;
     j = j || i++ && printf("YOU CAN");
            printf("%d %d", i, j);
}
Answer:
4 1
Explanation:
The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expression's truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become false, the whole expression's truth value becomes false and hence the remaining expression will not be evaluated.
false && (anything) => false where (anything) will not be evaluated.
137.      main()
{
            register int a=2;
     printf("Address of a = %d",&a);
            printf("Value of a   = %d",a);
}
Answer:
Compier Error: '&' on register variable
Rule to Remember:
& (address of ) operator cannot be applied on register variables.
138.      main()
{
            float i=1.5;
     switch(i)
            {
            case 1: printf("1");
                    case 2: printf("2");
                    default : printf("0");
   }
}
Answer: Compiler Error: switch expression not integral
Explanation: Switch statements can be applied only to integral types.
139.      main()
{  
           extern i;
   printf("%d\n",i);
           {
                  int i=20;
           printf("%d\n",i);
           }
}
Answer:
Linker Error : Unresolved external symbol i
Explanation:
The identifier i is available in the inner block and so using extern has no use in resolving it.
140.      main()
{
           int a=2,*f1,*f2;
   f1=f2=&a;
           *f2+=*f2+=a+=2.5;
        printf("\n%d %d %d",a,*f1,*f2);
}
Answer:
16 16 16
Explanation:
f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a.
141.      main()
{
            char *p="GOOD";
     char a[ ]="GOOD";
printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p));
     printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));
}
Answer:
sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
Explanation:
sizeof(p) => sizeof(char*) => 2
sizeof(*p) => sizeof(char) => 1
Similarly,
sizeof(a) => size of the character array => 5
When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account.
142.      #define DIM( array, type) sizeof(array)/sizeof(type)
main()
{
int arr[10];
printf("The dimension of the array is %d", DIM(arr, int));    
}
Answer:
10
Explanation:
The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10.
143.      int DIM(int array[]) 
{
return sizeof(array)/sizeof(int );
}
main()
{
int arr[10];
printf("The dimension of the array is %d", DIM(arr));    
}
Answer:
1
Explanation:
Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array (this is one of the very few places where [] and * usage are equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in this case.
144.      main()
{
  static int a[3][3]={1,2,3,4,5,6,7,8,9};
  int i,j;
  static *p[]={a,a+1,a+2};
         for(i=0;i<3;i++)
  {
                 for(j=0;j<3;j++)
                 printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j),
                 *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
  }
}
Answer:
                 1       1       1       1
                 2       4       2       4
                 3       7       3       7
                 4       2       4       2
                 5       5       5       5
                 6       8       6       8
                 7       3       7       3
                 8       6       8       6
                 9       9       9       9
Explanation:
*(*(p+i)+j) is equivalent to p[i][j].
145.      main()
{
         void swap();
  int x=10,y=8;     
         swap(&x,&y);
  printf("x=%d y=%d",x,y);
}
void swap(int *a, int *b)
{
   *a ^= *b,  *b ^= *a, *a ^= *b; 
}
Answer:
x=10 y=8
Explanation:
Using ^ like this is a way to swap two variables without using a temporary variable and that too in a single statement.
Inside main(), void swap(); means that swap is a function that may take any number of arguments (not no arguments) and returns nothing. So this doesn't issue a compiler error by the call swap(&x,&y); that has two arguments.
This convention is historically due to pre-ANSI style (referred to as Kernighan and Ritchie style) style of function declaration. In that style, the swap function will be defined as follows, void swap()
int *a, int *b
{
   *a ^= *b,  *b ^= *a, *a ^= *b; 
}
where the arguments follow the (). So naturally the declaration for swap will look like, void swap() which means the swap can take any number of arguments.
146.           main()
{
            int i = 257;
     int *iPtr = &i;
            printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}
Answer:
1 1
Explanation:
The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed.
147.           main()
{
            int i = 258;
     int *iPtr = &i;
            printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}
Answer:
2 1
Explanation:
The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are 'small-endian' machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010.
148.           main()
{
            int i=300;
     char *ptr = &i;
            *++ptr=2;
     printf("%d",i);
}
Answer:
556
Explanation:
The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556.
149.           #include ‹stdio.h›
main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
       least = (*ptr < least ) ?*ptr :least;
printf("%d",least);
}
Answer:
0
Explanation:
After 'ptr' reaches the end of the string the value pointed by 'str' is '\0'. So the value of 'str' is less than that of 'least'. So the value of 'least' finally is 0.
150.           main()
{
struct student 
{
char name[30];
struct date dob;
}stud;
struct date
        {  
         int day,month,year;
         };
     scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month,      &student.dob.year);
}
Answer:
Compiler Error: Undefined structure date
Explanation:
Inside the struct definition of 'student' the member of type struct date is given. The compiler doesn't have the definition of date structure (forward reference is not allowed in C in this case) so it issues an error.