1 November 2012

C Technical Questions Part 4



C Technical Questions

1.)main()
    {
       int a=10,*j;
       void *k;
       j=k=&a;
       j++; 
       k++;
       printf("\n %u %u ",j,k);
   }

Ans: 
type of k is unknown hence ERROR
Explanation:
void cannot be used for declaring variables. We can declare void pointers, but it can't be used in an expression.
2.) Is this code legal?
     int *ptr;
     ptr = (int *) 0x400;
Ans:  
    Legal
Explanation:
it is legal to assign an address to a pointer variable. The only condition to be followed is that it has to be casted to the type of pointer variable.
3.) void main()
{
int i=10, j=2;
int *ip= &i, *jp = &j;
int k = *ip/*jp;//ya but *ip/(*jp) is valid since /* is used for    comment
printf(“%d”,k);
}         

Ans : 
error
Explanation:
The symbol /* will make the statements after it as comment. 

4.)
 void main()
{
            printf(“sizeof (void *) = %d \n“, sizeof( void *));
            printf(“sizeof (int *)    = %d \n”, sizeof(int *));
            printf(“sizeof (double *)  = %d \n”, sizeof(double *));
            printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *));
            }

Ans:
2 2 2 2
Explanation:
Tthe size of all pointer variables is 2, independent of type.
5.) void main()
{
int *i = 0x400;  // i points to the address 400
*i = 0;              // set the value of memory location pointed by i;
}

Ans :
Legal
Explanation:
it is legal to assign an address to a pointer variable. The only condition to be followed is that it has to be casted to the type of pointer variable.
6.) What is the subtle error in the following code segment?
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(1)
                        p = &arr[i];
*p = 0;
}

Ans: 
Error
Explanation:
statement *p=0 will be never executed.

7.) #include <stdio.h>
main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
                  least = (*ptr<least ) ?*ptr :least;
printf("%d",least);
}
Ans: 
104
Explanation:
ptr contains address of the first element of array which is h. the ascii value of h is 104 which is less than 107. So least is asssigned value 104 and value is printed.

8.) main()
{
            int i=300;
            char *ptr = &i;
            *++ptr=2;
            printf("%d",i);
}

Ans:
300
Explanation:
++ ptr first increment the address stored in ptr and value at that address is modified. Hence the value at i is left unaltered.
9.) main()
{
            int i = 258;
            int *iPtr = &i;
            printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}         
         
Ans:
2 1
Explanation:
Since iptr is casted to character pointer only first byte is considered.
10.) main()
{
            int i = 257;//
            int *iPtr = &i;
            printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}

Ans: 1 1


Explanation:
Since iptr is casted to character pointer only first byte is considered.

11.)
 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));
                        }
}

Ans :    
              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]
*(*(j+p)+i) is equivalent to p[j][i]
*(*(i+p)+j) is equivalent to p[i][j]
*(*(p+j)+i)) is equivalent to p[j][i]
12.)  main()
      {
        char *p="GOOD";
        char a[ ]="GOOD";
        printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d\n",      sizeof(p), sizeof(*p), strlen(p));
        printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));
}

Ans: 
2 1 4
5 4
Explanation:
sizeof(p) will return the value 2 since size of all pointer variables is 2.
sizeof(*p) will return the value one since *p is the character 'G' and size of a character is 1
strlen(p) will return the number of characters in the string pointed by pointer p and returns 4
sizeof(a) will return 5 because a is an array and sizeof() will return the number of elements of array. The size of array a will be 5. ie 4 character plus one null character attached at the end.
strlen(a) will return the number of characters in the string stored in array and returns 4

13.) main()
     {
     a=2,*f1,*f2;//a=2,f1=&a,f2=&a//
     f1=f2=&a;
     *f2+=*f2+=a+=2.5;
     printf("\n%d %d %d",a,*f1,*f2);
}

Ans: 

16 16 16
Explanation:
the expression  *f2+=*f2+=a+=2.5 is equal to *f2=*f2+(*f2=*f2+(a=a+2.5)). 
If you still have any doubt please post a comment. 
     
14. 1. const char *a;
      2. char* const a;
      3. char const *a;
      -Differentiate the above declarations.

Ans:   
  pointer to a constant character
constant Pointer to a character
pointer to a constant character

Explanation:
const char *a; defines a as the pointer to a constant character, so it is not possible to change the value at the address stored in a but it is possible to change the address value stored in a. ie the assignment *a=*a+1 is not possible but a=a+1 is allowed
char* const a; defines a as the constant pointer to a character, so it is not possible to change the address value stored in a but its possible to change the value at the address stored in a,
char const *a; is same as  const char *a;


15.)       main()
{
char *p = “ayqm”;
char c;
c = ++*p++;
printf(“%c”,c);
}
Ans:
 b
Explanation:
here *p contains the value 'a', and the expression ++*p++ work as follows.
first the value of *p is incremented and
16. main()
{         
char *p = “ayqm”;
printf(“%c”,++*(p++));
}

Ans:
b
Explanation:
Same as question 15 
17. What is the output for the following program

            main()
                            {
      int arr2D[3][3];
       printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) );
               }
Ans: 
1

Explanation: 
Two dimensional array is internally stored as a one dimensional array. The only difference is the way of access to elements. Hence arr2D, *arr2D, arr2D[0] are all address of first element of that large one dimensional error

18.) Is the following statement a declaration/definition. Find what does it mean?
int (*x)[10];

Ans:  

pointer to an array of 10 integer data.

19.) main()
{
            int a[10];
            printf("%d",*a+1-*a+3);
}
Ans:
 4
Explanation:
The indirection operator * has more precedence than +, suppose first element of a is 2 then the expression is
2+1-2+3. Hence the result.

20. void main()
{
            void *v;
            int integer=2;
            int *i=&integer;
            v=i;
            printf("%d",(int*)*v);
}

Ans :
2
Explanation:
void can be used as generic type. Before using variables declared as void it has to be casted to a valid type.

No comments:

Post a Comment

Followers