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

Pointers Questions and Answers

71.

Is this a correct way for NULL pointer assignment?

int i=0;
char *q=(char*)i;

 

Answer: B

Correct answer: No

This does not produce a null pointer reliably. The rule requires a null pointer constant - a constant expression evaluating to zero - and i is a variable, so what is converted is a run-time integer value. The compiler is obliged to reinterpret it as an address rather than substitute the machine’s null representation, which need not be all-zero bits.

Please log in or register to report this question.

72.

Is the NULL pointer same as an uninitialised pointer?

Answer: B

Correct answer: No

A null pointer holds a defined value that compares unequal to any valid address, so it can be tested with if (p) and used as a sentinel. An uninitialised pointer holds whatever happened to be in memory, so it cannot be tested meaningfully and dereferencing it is undefined. Confusing the two is why uninitialised pointers should always be set to NULL explicitly.

Please log in or register to report this question.

73.

Will the program compile in Turbo C?

#include <stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u\n", j, k);
    return 0;
}

 

Answer: B

Correct answer: No

Arithmetic on a void * is not allowed, because the compiler would have to know the size of the pointed-to type to advance the pointer, and void has none. So k++ is the offending line. The j++ above it is perfectly legal and advances by the size of an int. GCC permits void arithmetic as an extension, treating the size as one, but it is not standard C.

Please log in or register to report this question.

74.

Will the following program give any warning on compilation in TurboC (under DOS)?

#include <stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}

 

Answer: B

Correct answer: No

All four assignments are legal without a cast. C converts implicitly in both directions between void * and any object pointer type, so assigning an int * to a void * and back again raises no diagnostic. It is C++ that requires an explicit cast when converting away from void *, and mixing up the two languages is what makes this question look harder than it is.

Please log in or register to report this question.

75.

What is the output of this C code?

int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}

 

Answer: A

Correct answer: nullp nullq

Both pointers are null - NULL and a literal zero in a pointer context mean the same thing - and a null pointer used in a condition tests false. So both if statements take their else branch, and the output is nullp nullq. The result does not depend on how NULL is defined, since the comparison is against the null pointer value rather than against the bit pattern.

Please log in or register to report this question.

76.

What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}

 

Answer: A

Correct answer: Compile time error

A void * cannot be dereferenced. void is an incomplete type with no size and no representation, so the compiler cannot know how many bytes to read or how to interpret them, and *p is rejected before the cast is even considered. The correct form casts the pointer first: *(int *)p.

Please log in or register to report this question.

77.

What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

 

Answer: D

Correct answer: 0.000000

The cast changes how the bytes are interpreted, not what they contain. The four bytes hold the integer 10, and read as an IEEE single-precision float that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \) - which %f prints, to six places, as 0.000000. The program compiles because the cast is explicit; it is the meaning rather than the syntax that is wrong.

Please log in or register to report this question.

78.

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.

79.

What is the output of this C code?

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

 

Answer: A

Correct answer: Undefined behaviour

j is an automatic variable whose lifetime ends when f returns, so the address returned is dangling and dereferencing it is undefined. It very often prints 10, because the stack frame has not yet been overwritten, but that is an accident rather than a guarantee - another call, a different optimisation level or a different compiler can produce anything at all. The preceding question shows the correct pattern, using malloc.

Please log in or register to report this question.

80.

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.

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