Wednesday 8 November 2017

Pointers Questions in C


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

--------------------------------------------------------------- 

1.

What is the output of following program?

# include  
void fun(int x)
 {
 x = 30;
 } 
int main()
 {
 int y = 20;
 fun(y); 
printf("%d", y); 
return 0;
 }

A. 30
B. 20
C. Compilation Error
D. Runtime Error

Answer: B

Explanation:

Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using the function fun(). So how do we modify the value of a local variable of a function inside another function. Pointer is the solution to such problems. Using pointers, we can modify a local variable of a function inside another function. See the next question. Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.


2.

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


Answer: D

----------------------------------------------------------------------------------------------------------------------

3.

Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y.

int main()
{
    int array[] = {3, 5, 1, 4, 6, 2};
    int done = 0;
    int i;

    while (done == 0)
    {
        done  = 1;
        for (i = 0; i <= 4; i++)
        {
            if (array[i] < array[i+1])
            {
                swap(&array[i], &array[i+1]);
                done = 0;
            }
        }
        for (i = 5; i >= 1; i--)
        {
            if (array[i] > array[i-1])
            {
                swap(&array[i], &array[i-1]);
                done = 0;
            }
        }
    }

    printf("%d", array[3]);
}

The output is:

A. 1
B. 2
C. 3
D. 4


Answer: 3

Explanation:

After the execution of the for loop, the content of array: First time: 5 3 4 6 2 1 Second time: 6 5 4 3 2 1 Third time: 6 5 4 3 2 1 Now, when while loop execute again, done = 1 and first and second for loop if condition will not be satisfied . Therefore, At the end, value of arr[3] = 3, Option C is correct.
----------------------------------------------------------------------------------------------------
4.

Suppose that in a C program snippet, followings statements are used.

i) sizeof(int);
ii) sizeof(int*);
iii) sizeof(int**);

Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the most correct answer from the given options.

A. Only i) would compile successfully and it would return size as 4.
B. i), ii) and iii) would compile successfully and size of each would be same i.e. 4
C. i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time.
D. ii) and iii) would result in compile error but i) would compile and result in size as 4.

Answer: B

Explanation:

Size of all pointer types is same. And whether it is a 'pointer to char' or 'pointer to int' or 'pointer to pointer to int', the size always remain same. That's why all i), ii) and iii) would compile successfully and would result in same size value of 4.
-------------------------------------------------------------

5.

Find the output.

#include

int main()
{
 int var;  /*Suppose address of var is 2000 */

 void *ptr = &var;
 *ptr = 5;
 printf("var=%d and *ptr=%d",var,*ptr);
             
 return 0;
}

A. It will print “var=5 and *ptr=2000”
B. It will print “var=5 and *ptr=5”
C. It will print “var=5 and *ptr=XYZ” where XYZ is some random address
D. Compile error

Answer: D
----------------------------------------------------------------------------------------------\
Previous                         Next                            Last
----------------------------------------------------------------------------------------------

More Imp Topics:






100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...