Pointers

Question 1

Faster access to non-local variables is achieved using an array of pointers to activation records called a

A
stack
B
heap
C
display
D
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.
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;
}
A
1, 10, 11
B
1, 10, 14
C
10, 14, 11
D
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?

A
Only P3
B
Only P1 and P3
C
Only P1 and P2
D
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.
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?
A
14
B
30
C
24
D
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
A
0
B
99999999
C
1
D
unpredictable
Question 5 Explanation: 
Question 6
What is the output of this C code?
A
5 5 5
B
5 5 junk
C
5 junk junk
D
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
Question 7
What will be the output of the following program? Assume that you are running this program in little-endian processor.
A
1
B
320
C
64
D
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.
There are 7 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access