Pointers
Question 1 |
Faster access to non-local variables is achieved using an array of pointers to activation records called a
stack | |
heap | |
display | |
activation tree |
Question 1 Explanation:
Properties of displays:
→ Use a pointer array to store the activation records along the static chain.
→ Fast access for non-local variables but may be complicated to maintain.
→ Use a pointer array to store the activation records along the static chain.
→ Fast access for non-local variables but may be complicated to maintain.
Question 2 |
What is printed by the following ANSI C program?
#include
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d\n", x, z[0], z[1]);
return 0;
}
#include
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d\n", x, z[0], z[1]);
return 0;
}
1, 10, 11 | |
1, 10, 14 | |
10, 14, 11 | |
10, 10, 14 |
Question 2 Explanation:

Question 3 |
Consider the following three C functions:
[PI] int*g(void)
{
int x = 10;
return(&x);
}
[P2] int*g(void)
{
int*px;
*px = 10;
return px;
}
[P3] int*g(void)
{
int*px;
px = (int *) malloc(sizeof(int));
*px = 10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?
Only P3 | |
Only P1 and P3 | |
Only P1 and P2 | |
P1, P2 and P3 |
Question 3 Explanation:
[P1] → May cause error because the function is returning the address of locally declared variable.
[P2] → It will cause problem because px is in int pointer that is not assigned with any address and we are doing dereferencing.
[P3] → It will work because memory will be stored in px that can be use further. Once function execution completes this will exist in Heap.
[P2] → It will cause problem because px is in int pointer that is not assigned with any address and we are doing dereferencing.
[P3] → It will work because memory will be stored in px that can be use further. Once function execution completes this will exist in Heap.
Question 4 |
Consider the following ANSI C program
#include<stdio.h>
int main() {
int arr[4][5];
int i, j;
for (i=0; i<4; i++){
for (j=0; j<5; j++){
arr[i][j] = 10*i + j;
}
}
printf (“%d”, *(arr[1] + 9));
return 0;
}
What is the output of the above program?
#include<stdio.h>
int main() {
int arr[4][5];
int i, j;
for (i=0; i<4; i++){
for (j=0; j<5; j++){
arr[i][j] = 10*i + j;
}
}
printf (“%d”, *(arr[1] + 9));
return 0;
}
What is the output of the above program?
14 | |
30 | |
24 | |
20 |
Question 4 Explanation:
arr[4][5]


Question 5 |
Consider the following program
Assuming required header files are included and if the machine in which this program is executed is little-endian, then the output will be
Assuming required header files are included and if the machine in which this program is executed is little-endian, then the output will be
0 | |
99999999 | |
1 | |
unpredictable |
Question 5 Explanation:

Question 6 |
What is the output of this C code?


5 5 5 | |
5 5 junk | |
5 junk junk | |
Compile time error |
Question 6 Explanation:
If we are given p instead of *p then it prints the address of k but we are printing the value of k.
The same rule applies in **m.
Output is 5 5 5
The same rule applies in **m.
Output is 5 5 5
Question 7 |
What will be the output of the following program? Assume that you are running this program in little-endian processor.


1 | |
320 | |
64 | |
Compilation error |
Question 7 Explanation:
Ptr = (char*) &a ; /* It is type casted to char* */
Printf (“%d”, *ptr) ; /* Here *ptr is referring a single character */
Printf (“%d”, *ptr) ;
Output: 64 /* @ ASCII value is 64 */
Note: Assume short a is 2 byte integer.
Printf (“%d”, *ptr) ; /* Here *ptr is referring a single character */
Printf (“%d”, *ptr) ;Output: 64 /* @ ASCII value is 64 */
Note: Assume short a is 2 byte integer.
There are 7 questions to complete.
