Google Analytics

Friday 19 August 2011

FRP Questions for practice


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



2.      Is this code legal?
int *ptr;
ptr = (int *) 0x400;

3.      void main()
{
int i=10, j=2;
int *ip= &i, *jp = &j;
int k = *ip/*jp;
printf(“%d”,k);
}       


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


5. void main()
{
int *i = 0x400;  // i points to the address 400
*i = 0;                  // set the value of memory location pointed by i;
}


6. What is the subtle error in the following code segment?
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++<n)
                   p = &arr[i];
*p = 0;
}


7. #include <stdio.h>
main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
                  least = (*ptr<least ) ?*ptr :least;
printf("%d",least);
}


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


9. main()
{
                   int i = 258;
          int *iPtr = &i;
                   printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}       



10. main()
{
                   int i = 257;
          int *iPtr = &i;
                   printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}


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


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


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


          14. 1. const char *a;
2. char* const a;
3. char const *a;
-Differentiate the above declarations.


15.    main()
{
char *p = “ayqm”;
char c;
c = ++*p++;
printf(“%c”,c);
}


16. main()
{       
char *p = “ayqm”;
printf(“%c”,++*(p++));
}


17. What is the output for the following program

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


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


19. main()
{
          int a[10];
          printf("%d",*a+1-*a+3);
}


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


21. # include <stdio.h>
int one_d[]={1,2,3};
main()
{
 int *ptr;
 ptr=one_d;
 ptr+=3;
 printf("%d",*ptr);
}


22. main()
{
 char *p;
 int *q;
 long *r;
 p=q=r=0;
 p++;
 q++;
 r++;
 printf("%p...%p...%p",p,q,r);
}


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


24. main()
          {
              int *j;
              {
               int i=10;
               j=&i;
               }
               printf("%d",*j);
}


25. main()
          {
          char *cptr,c;
          void *vptr,v;
          c=10;  v=0;
          cptr=&c; vptr=&v;
          printf("%c%v",c,v);
          }


26. main()
{
 int  i, n;
 char *x = “girl”;
 n = strlen(x);
 *x = x[n];
 for(i=0; i<n; ++i)
   {
printf(“%s\n”,x);
x++;
   }
 }


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


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


29. main( )
{
 char  *q;
 int  j;
 for (j=0; j<3; j++) scanf(“%s” ,(q+j));
 for (j=0; j<3; j++) printf(“%c” ,*(q+j));
 for (j=0; j<3; j++) printf(“%s” ,(q+j));
}


30. main( )
{
 static int  a[ ]   = {0,1,2,3,4};
 int  *p[ ] = {a,a+1,a+2,a+3,a+4};
 int  **ptr =  p;
 ptr++;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 *ptr++;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 *++ptr;
 printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
 ++*ptr;
      printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
}


31. 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++;
      }
 }


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


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



34. 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;   }
}



35. void main()
{
          int  const * p=5;
          printf("%d",++(*p));
}




                             Answers
1. Answer:
                   Compiler error: Cannot increment a void pointer
Explanation:
Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.


2. Answer:
                    Yes
Explanation:
The pointer ptr will point at the integer in the memory location 0x400.

3. Answer:
Compiler Error: “Unexpected end of file in comment started in line 5”.
Explanation:
The programmer intended to divide two integers, but by the “maximum munch” rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer,
int k = *ip/ *jp; 
// give space explicity separating / and *
//or
int k = *ip/(*jp);
// put braces to force the intention 
will solve the problem. 


4. Answer  :
sizeof (void *) = 2
sizeof (int *)    = 2
sizeof (double *)  =  2
sizeof(struct unknown *) =  2
Explanation:
The pointer to any type is of same size.


5. Answer:
Undefined behavior
Explanation:
The second statement results in undefined behavior because it points to some location whose value may not be available for modification.  This type of pointer in which the non-availability of the implementation of the referenced location is known as 'incomplete type'.


6. Answer & Explanation:
If the body of the loop never executes p is assigned no address. So p remains NULL where *p =0 may result in problem (may rise to runtime error “NULL pointer assignment” and terminate the program).    


7. Answer:

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.


8. 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.


9. 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.  


10. 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.


11. 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].


12. 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.


13. 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.


14. Answer:
1. 'const' applies to char * rather than 'a' ( pointer to a constant char )
          *a='F'       : illegal
                             a="Hi"       : legal

2. 'const' applies to 'a'  rather than to the value of a (constant pointer to char )
          *a='F'       : legal
          a="Hi"       : illegal

3. Same as 1.


15. 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.



16. Answer:
b      



17. Answer
1
Explanation
This is due to the close relation between the arrays and pointers. N dimensional arrays are made up of (N-1) dimensional arrays.  
          arr2D is made up of a 3 single arrays that contains 3 integers each .


arr2D



arr2D[1]




arr2D[2]


arr2D[3]






The name arr2D refers to the beginning of all the 3 arrays. *arr2D refers to the start of the first 1D array (of 3 integers) that is the same address as arr2D. So the expression (arr2D == *arr2D) is true (1).
Similarly, *arr2D is nothing but *(arr2D + 0), adding a zero doesn’t change the value/meaning. Again arr2D[0] is the another way of telling *(arr2D + 0). So the expression (*(arr2D + 0) == arr2D[0]) is true (1).
Since both parts of the expression evaluates to true the result is true(1) and the same is printed. 



18. Answer
                   Definition.
          x is a pointer to array of(size 10) integers.

                   Apply clock-wise rule to find the meaning of this definition.


19. Answer:
Explanation:
          *a and -*a cancels out. The result is as simple as 1 + 3 = 4 !     


20. Answer:
Compiler Error. We cannot apply indirection on type void*.
Explanation:
Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for,
Passing generic pointers to functions and returning such pointers.
As a intermediate pointer type.
Used when the exact pointer type will be known at a later point of time.


21. Answer:
garbage value
Explanation:
ptr pointer is pointing to out of the array range of one_d.


22.  Answer:
0001...0002...0004
Explanation:
++ operator  when applied to pointers increments address according to their corresponding data-types.


23. Answer:
garbagevalue..1
Explanation:
p=&a[2][2][2]  you declare only two 2D arrays. but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. now q is pointing to starting address of a.if you print *q meAnswer:it will print first element of 3D array.


24. Answer:
10
Explanation:
The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i.


25. Answer:
Compiler error (at line number 4): size of v is Unknown.
Explanation:
You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error.



26. Answer:
(blank space)
irl
rl
l

Explanation:
Here a string (a pointer to char) is initialized with a value “girl”.  The strlen function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location (‘\0’) to the first location. Now the string becomes “\0irl” . Now the printf statement prints the string after each iteration it increments it starting position.  Loop starts from 0 to 4. The first time x[0] = ‘\0’ hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e “irl” and the third time it prints “rl” and the last time it prints “l” and the loop terminates.


27. Answer:
          ck
Explanation:
In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p =  s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 – 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ‘ck’.


28. Answer:
          g20fy
Explanation:
Since a void pointer is used it can be type casted to any  other type pointer. vp = &ch  stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is ‘g’. Similarly  the output from second printf is ‘20’. The third printf statement type casts it to print the string from the 4th value hence the output is ‘fy’.


29. Explanation:
Here we have only one pointer to type char and since we take input in the same pointer thus we keep writing over in the same location, each time shifting the pointer value by 1. Suppose the inputs are MOUSE,  TRACK and VIRTUAL. Then for the first input suppose the pointer starts at location 100 then the input one is stored as
M
O
U
S
E
\0
When the second input is given the pointer is incremented as j value becomes 1, so the input is filled in memory starting from 101.
M
T
R
A
C
K
\0
The third input  starts filling from the location 102
M
T
V
I
R
T
U
A
L
\0
This is the final value stored .
The first printf prints the values at the position q, q+1 and q+2  = M T V
The second printf prints three strings starting from locations q, q+1, q+2
 i.e  MTVIRTUAL, TVIRTUAL and VIRTUAL.


30. Answer:
          111
          222
          333
          344
Explanation:
Let us consider the array and the two pointers with some address
a  

1
2
3
4
   100      102      104      106      108
                                                p
100
102
104
106
108
                                1000    1002    1004    1006    1008
          ptr        
1000
2000
After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes. Now ptr – p is value in ptr – starting location of array p, (1002 – 1000) / (scaling factor) = 1,  *ptr – a = value at address pointed by ptr – starting value of array a, 1002 has a value 102  so the value is (102 – 100)/(scaling factor) = 1,  **ptr is the value stored in the location pointed by  the pointer of ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is  1, 1, 1.
After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the second printf are ptr – p = 2, *ptr – a = 2, **ptr = 2.
After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the third printf are ptr – p = 3, *ptr – a = 3, **ptr = 3.
After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the fourth printf are ptr – p = 1006 – 1000 = 3, *ptr – a = 108 – 100 = 4, **ptr = 4.


31. Answer:
Compiler error: lvalue required. 
                  
Explanation:
Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue.


32. Answer:
100, 100, 100, 2
114, 104, 102, 3
Explanation:
              The given array is a 3-D one. It can also be viewed as a 1-D array.

2
4
7
8
3
4
2
2
2
3
3
4
   100  102  104  106 108   110  112  114  116   118   120   122

thus, for the first printf statement a, *a, **a  give address of  first element . since the indirection ***a gives the value. Hence, the first line of the output.
for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence, the output.


33. Answer:
SomeGarbageValue---1
Explanation:
p=&a[2][2][2]  you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array.


34. Answer:
                   2 2 2 2 2 2 3 4 6 5
          Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.


35. Answer:
                   Compiler error: Cannot modify a constant value.
Explanation:   
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

No comments:

Post a Comment