2 November 2012

C Technical Questions Part 5


C Technical Questions
1.) 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]);
}

Ans:
mmmm
aaaa
nnnn
Explanation:
Here the for loop will execute until a null character is reached. s[i], i[s], *(i+s), *(s+i) all indicates i th element of array s.
2. main()
{
            char *p;
            printf("%d %d ",sizeof(*p),sizeof(p));
}

Ans:
1 2
Explanation:
sizeof(*p) returns the size of the character pointed by p. sizeof(p) returns the size of pointer p. Note:// The size of normal pointers are always 2.
3.) main()
{
              printf("%x",-1<<4);
}

Ans:
0xfff0
Explanation:
The binary representation of -1  and corresponding hexa representation is.
11111111 11111111  
F       F       F      F
11111111 11111111   << 4 gives
1111 1111 1111 0000
F        F       F       0
Hence the result

4.)  main()
{
            int c=- -2;
            printf("c=%d",c);
}

Ans:
 2
Explanation:
Use of two unary operators minus operator cancel the effect each other.
Note:// the space between - - is necessary otherwise it will be treated as decrement operator.

5.) main()
{
 char *p;
 p="Hello";
 printf("%c\n",*&*p);
}
//here*&p==&*p
Ans:
H
Explanation :
*p specifies value at address p.
&*p  equal to &(*p) specifies address of value pointed by p, ie nothing other than address stored in p.
*&*p equal to *(&(*p)) specifies value at address of value pointed by p, ie nothing other than *p which is value ‘H’

6.) main()
            {
            char *str1="abcd";
            char str2[]="abcd";
            printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
            }

Ans:
2 5 5
Explanation:
sizeof(str1),sizeof(str2),sizeof("abcd")
sizeof(str1) returns the size of pointer variable str1 which is 2 (size of a normal pointer is always 2). sizeof(str2) returns the size of array str2 which is 5*sizeof(char) hence 5 (the fifth element of array is the null character. sizeof("abcd") returns size of string constant “abcd” which is again 5*sizeof(char).(there will always a null character appended at the end of the string constant).
7. #include<stdio.h>
main()
  {
    register i=5;
    char j[]= "hello";                    
    printf("%s  %d",j,i);
}

Ans:
hello 5
Explanation:
The keyword register specifies the value at i will be stored in memory register. register is a storage class, if data type is not mentioned after a storage class or qualifier by default it will be treated as int.

8.) void main()
{
            int i;
            char a[]="\0";
            if(printf("%s\n",a))
            printf("Ok here \n");
            else
            printf("Forget it\n");
}

Ans:
Ok here
Explanation:
  Printf returns the number of characters it successfully printed. Here printf prints one newline character hence the result.
9. What is the output of the program given below

main()
    {
       signed char i=0;
       for(;i>=0;i++) ;
       printf("%d\n",i);
    }

Ans :
-128
Explanation:
Signed characters will take values from -128 to +127 in cyclic manner. Here first the value 0 is assigned to i then the i is continuously incremented in the increment section of the loop. When the value of i reaches 127, when it again incremented i will take value -128 and the test condition fails and loop terminates. Hence the result
10.) main()
      {
       char i=0;
       for(;i>=0;i++) ;
       printf("%d\n",i);
}
Ans :
 -128
Explanation:
Works in the same manner as above. Normally when no qualifiers are used by default data types are taken as signed.

11.) void main()
      {
      if(~0 == (unsigned int)-1)
      printf(“You can answer this if you know how values are represented in memory”);
      }

      Ans :
      you can answer this if you know how values are represented in memory.
      Explanation:
      As I mentioned in the previous questions variables will take values within its range in a cyclic manner. Unsigned int will take values between 0 to65535 and signed between -32768 to +32767. The unsigned equivalent of -1 is 1. Hence the result.
         

12. main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}

Ans :
L value required error.
Explanation:
 i++ will return athe value 3 and is impossible to assign a value to a constant.
13. main()
{
 int i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
Ans :
 1 10
Explanation:
i=i&=j&&10; statement will be expanded as i=i=i&j&&10; hence the result.
14.)
#define DIM( array, type) sizeof(array)/sizeof(type)
main()
{
int arr[10];
printf(“The dimension of the array is %d”, DIM(arr, int));   
}

Ans:
10
Explanation:
Preprocessor will expand the macro as sizeof(arr)/sizeof(int) which is 20/2, hence the result.

15. int DIM(int array[])
{
return sizeof(array)/sizeof(int );
}
main()
{
int arr[10];
printf(“The dimension of the array is %d”, DIM(arr));   
}

Ans:
The  dimension of the array is 1
Explanation:
An array can be passed to a function only as a pointer and size of pointer is always 2 hence the result.
16. #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);
            }
Ans:
Error
Explanation:
Arr1 and arr2 is not defined anywhere.

No comments:

Post a Comment

Followers