Machine-translated by Google. Questions are authored in English.
Top

Pointers Questions and Answers

41.

What is the output of this C code?

int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}

 

Answer: A

Correct answer: 10

The storage was obtained from malloc, so it lives on the heap and survives the return of the function that allocated it. The pointer therefore remains valid in main and dereferencing it gives 10. Compare the next question, where the same structure with an automatic variable is undefined - the difference is the storage duration, not the pointer.

Please log in or register to report this question.

42.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int i = 10;
        int *const p = &i;
        foo(&p);
        printf("%d\n", *p);
    }
    void foo(int **p)
    {
        int j = 11;
        *p = &j;
        printf("%d\n", **p);
    }

 

Answer: A

Correct answer: 11 11

In main, int *const p makes the pointer itself const, so &p has type int *const *. Passing that to a parameter of type int ** discards the qualifier, which is a constraint violation - but compilers generally warn rather than refuse, so the program runs.

foo then writes through the pointer, redirecting main's p to j, and both prints show 11. The exercise illustrates that const can be circumvented through a pointer conversion the compiler only warns about.

Please log in or register to report this question.

43.

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

 

Answer: C

Correct answer: char *p = (char*)malloc(100);

A declaration may carry an initialiser, so the declaration and the assignment combine directly: char *p = (char*)malloc(100);.

The other forms misplace the asterisks. (char)malloc(100) casts the returned pointer to a single character, and the last option is not valid syntax at all. It is worth adding that in C the cast is unnecessary - void * converts to any object pointer type implicitly, and omitting the cast can expose a missing stdlib.h that the cast would hide.

Please log in or register to report this question.

44.

What will be the output of the program if the size of pointer is 4-bytes?

#include <stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}

 

Answer: C

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Please log in or register to report this question.

45.

Which of the following statements correctly declare a function that receives a pointer to pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?

Answer: D

Correct answer: float ****fun(float***);

Read the declaration outward from the name. The parameter is a pointer to a pointer to a pointer to a float, so it is written float ***. The return type is one level deeper again, so it is float ****.

Count the asterisks against the words: three for the parameter, four for the return. The other options mismatch one or both. Where such declarations become unreadable, a typedef for the intermediate types is the usual remedy.

Please log in or register to report this question.

Answer: C

Correct answer: The value stored at the address held by p

The asterisk is the dereference operator, which follows the pointer to the object it refers to. The address of p itself is obtained with &p, and p alone gives the address it holds.

Please log in or register to report this question.

47.

What is the output of this C code?

int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}

 

Answer: D

Correct answer: 11,11

ptr holds the address of a, so *ptr += 1 increments a itself - there is only one object, reached by two names. Both print as 11. Note also the /n in the format string, which is a forward slash and the letter n rather than an escape, so it appears literally instead of starting a new line.

Please log in or register to report this question.

48.

What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?

#include <stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}

 

Answer: C

Correct answer: 1006, 2, 2

Take the three arguments in turn. a[0] is a pointer to the first row's first element, at 1002, and adding 1 advances it by sizeof(int), giving 1006.

*(a[0]+1) dereferences that, yielding a[0][1], which is 2. And *(*(a+0)+1) is the same expression written with explicit pointer arithmetic - a+0 is a, *(a+0) is a[0] - so it too gives 2.

Please log in or register to report this question.

49.

What will be the output of the following C code?

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10;
        foo((&i)++);
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

 

Answer: C

Correct answer: Compile time error

&i yields the address of i, but it is a value, not an object - there is no storage holding it that could be incremented.

++ requires a modifiable lvalue, so (&i)++ is a constraint violation and the compiler rejects it. The contrast with the next case is instructive: if the address is first stored in a pointer variable, that variable is an object and can be incremented.

Please log in or register to report this question.

50.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *p;
    p="hello";
    printf("%s\n", *&*&p);
    return 0;
}

 

Answer: B

Correct answer: hello

The operators * and & are inverses, so each adjacent pair cancels. Reading *&*&p from the right: &p gives the address of p, * takes us back to p, & takes the address again, and the final * returns to p.

The whole expression is therefore just p, and printf prints hello. Any even number of such pairs behaves the same way; the exercise exists to make the inverse relationship explicit.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming