Blog Archive

Saturday, May 15, 2010

Pointers And Array Test1 Solutions

http://rahulbudholiya.blogspot.com
Balkans
Session 1:
Candidate Name: Anonymous
Test 1: Pointers And Array Part1 (Solutions)

Q1. What will be the result of following code.

Void main()
{
int a[5][2]={{0,5},{1,7},{2,6},{3,8},{4,9}};
int (*p)[3];
p=a;
clrscr();
printf("%d",*p[0]);
printf("%d",*p[1]);
printf("%d",*p[2]);
}

Ans. 073
In above program p is declared as pointer to a block whose size is equal to 3 ‘int’ blocks.
Whenever we increment its value, for a single increment address in p is incremented by size of 3 int’s that is 3*2(6 bytes). So in first printf we incremented its size by 0 so address in p is remained same as before and 0 is printed, because 0 is stored in the first block of array, and then in next printf 6 bytes incremented and 7 is printed because it 6 bytes above from first block, and similarly in last printf 3 is printed because it is 2*6 bytes above from the first block.

Q2. Tell the difference between following pointer declarations.
a) int const *p;
b) const int *p;
Ans. both declarations are same, both are declaring a pointer to a constant variable.
In above declarations p is not constant so we can change its value at the time of
execution. In above declarations p is pointer to a constant int variable, so we can
change the value of variable pointed by p.
*p=10; //this will generate an error because p is a pointer to a constant variable
int k = *p;// this statement is valid , wecan read the value of variable pointed by p

Q3. Rewrite following statement in postfix increment operator;
++*ptr //ptr can be any pointer
Ans. (*ptr)++;
Se operator precedence table for details.

Q4. Write the equivalent pointer instruction to access following array element.
int a[10][10][10][10];
a[i][j][k][l]; // write the statement in the form of pointers
Ans. *(*(*(*(a+i)+j)+k)+l)
See let us c for more details.

Q5. If size of array is 4 bytes then what will be the output of following code.
#include<stdio.h>
void main()
{
int array[] = {12,13,14,15,16};
printf(“%d%d%d”,sizeof(array),sizeof(*array),sizeof(array[0]));
return 0;
}
Ans. 2044
First sizeof operator calculates size of all elements in array that is 20 bytes.
Second and third sizeof operator calculates size of element in array(more specifically size of first element of array) that is 4 bytes.
array is theoretically the pointer of first block of Array if it’s size is 4 bytes then size of int is also 4 bytes. Because generally size of pointers and int’s are same in many c compilers.

Q6. Will following code compile ? give reason.
void main()
{
char name[2] =”Garima.”;
printf(“%d”,name);
}
Ans. This is a compiler dependent question. In Turbo c answer is No. Following code will Not compile in turbo c. Because we are trying to assign 7 bytes data in a two byte array.
If statement was char name[] = “Garima”; then this code could compile in turbo c.
And same in vc++ this code will not compile in vc++.
But in gcc(GNU Cpp compiler, Dev C++ use this compiler) this code will compile without any error. But as we expect in the last block of array ‘\0’ must be stored, but in the above code in the last block of array ‘h’ will store, this will create a dangerous string
because there is no terminating character in this string.

Q7. What will be the output of following code.
void main()
{
printf(“%c\n”,3[“Garima.”]);
}

Ans. i
In above code by the statement “Garima” a array of 7 char get created, and chars ‘G’,’a’,’r’,’i’,’m’,’a’,’\0’
and in statement above address of first block is returned at the place of “Garima”.
So above statement is same as following code.
Char name[7]={‘G’,’a’,’r’,’i’,’m’,’a’.’\0’};
Printf(“%c”,3[name]);
We know that because of pointer notation of array name[3] is same as 3[name].





Q8. What will be the output of following code. If answer in garbage then give reason.
Void main()
{
char *p;
p = “rahul”;
printf(“%s”,*&*&p);
}
Ans. rahul.
In Above code finally value of p(address of first block of char array in which “rahul” is stored) is getting passed to pritf.
&p means address of pointer variable p, *&p means value at the address of p which is the value of p, &*&p meanse address of value which is stored at the address of p(means the address of p), and *&*&p means the value at the address of value which is stored at the
address if p.
See the operator precedence table for evaluation order of above operators & and *.

Q9. Write the output of following program.
Void main ()
{
int a[10]= {11,32,4,65,12,90,26,83,100,401};
int *p;
int *q;
p = &a[1];
q= &a[3];
prinrf(“%d %d”,*q-*p,q-p);
}
Ans. 33 2
Because difference between second and third block of array is 2, result will come in the form of array blocks not in bytes, in above array every block of array is two bytes in size, so the result of q-p is 2 block which means 4 bytes because one block is 2 bytes long.

Q10. Write the output of following program.
Void main ()
{
int a[10]= {11,32,4,65,12,90,26,83,100,401};
int *p;
int *q;
p = &a[1];
q= &a[3];
prinrf(“%d %d”,*q+*p,q+p);
}
Ans. Compile time error addition of two pointers are not allowed.

Q11. Write the output of following program.
Void main ()
{
int a[10]= {11,32,4,65,12,90,26,83,100,401};
int *p;
int *q;
p = &a[1];
q= &a[3];
prinrf(“%d %d”,*q+2,q+2);

}

Ans. 67 In second %d address of 6th element will print, as we no we should use %u for printing address , but we here used %d which is used of signed numbers and address is always a positive entity. So if address is out of range of %d then a negative number will print.

Q12. Write the output of following program.
Void main ()
{
int a[10]= {11,32,4,65,12,90,26,83,100,401};
int *p;
int *q;
p = &a[1];
q= &a[3];
prinrf(“%d %d”,*q*2,q*2);

}
Ans. Compile time error , multiplication with pointer is not allowed.
Q13. Write a program to transform a given string’s every second element capital.
Example:-
If string in shreya then output should be sHrEyA
Ans. This only one possible solution for this problem, there can be many solutions for this problem. Every program which produce same output is a solution of this problem. So try to create your own solution.

#include<stdio.h>
#include<conio.h>
int main()
{
char name[100] = “shreya”;
int counter=0;
for(;name[counter]!=’\0’;counter++)
{
if(counter%2==0)
{
name[counter] =name[counter] - 32;
}
}
return 0;
}
ASCII code of a is 97 and ASCII code of A is 65 we have to subtract 32 from a letter to make it CAPITAL.

Q14. Write a function which accepts following array as parameter and print all the elements of the array.
int a[10][2][3]={//any initialization here};

Ans. Here is the function definition.
void print(int a[][][],int size1,int size2, int size3)
{
int i,j,k;
for(i=0; i<size1; i++)
{
for(j=0; j<size2; j++)
{
for(k=0; k<size3; k++)
{
printf(“%d”,a[i][j][k]);
}
}
}
}

Ans. And Here is the function call
print(a,10,2,3);
There may be many more solutions for this problem, try your own solution.
Q15. What will be the value of a, after execution of following code.
int a=0;
int *pointer = &a;
pointer=10;
Ans. the value of a will remain 0. Because in above code we are modifying value of pointer, but not the value of variable pointed by the pointer.

Q16. What printf will print on screen, in following code?
char *p = “RahulShreyaDeepanshuAditi”;
printf(“%d”,sizeof(p));
Ans. 4 //in dev c++ or vc++
2 //in turbo c++
This is again a compiler dependent question, in above code size of pointer will get printed, in case of array all size of all the elements of array would be printed , but p is not declared as array. So the size of c will get printed, size of p can be vary compiler to compiler in 32 bit compiler size of pointers is 4 bytes and in 16 bit environment size of pointers is 2 bytes.

Q17. What printf will print on screen, in following code?
int a[4] = {0,1,2};
printf(“%d%d%d%d”,a[0],a[1],a[2],a[3]);
Ans. 0120
Because when we initialize a array partially, all remaining elements of array will initialize by 0.

Author - rahul

No comments:

Post a Comment