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

Pointers Questions and Answers

61.

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.

62.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 

Answer: A

Correct answer: ink

Work outward. ptr holds {s+3, s+2, s+1, s}, and p = ptr then ++p leaves p pointing at ptr[1], which is s+2.

So *p is s+2, and **p is s[2], the pointer to "pink". Finally **p+1 advances that char pointer one position, so printf starts from the second character and prints ink. The trap is applying the +1 at the wrong level - it binds to the char pointer, not to p.

Please log in or register to report this question.

63.

What is the output of this C code?

int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}

 

Answer: A

Correct answer: Compile time error

int *const *p means a pointer to a constant pointer to int, so *p is const and cannot be assigned to. The line *p = &j therefore violates the constraint and the compiler rejects it. Reading such declarations from the name outward is the reliable method: p, pointer to, const, pointer to, int.

Please log in or register to report this question.

64.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

 

Answer: D

Correct answer: JACK

Take the three prints in turn. ch is 74, the code for J, so the first prints J. Then j is 65 and *(int*)vp yields 65, which %c prints as A.

Finally vp is set to cp, and (char*)vp+2 advances two characters into "JACK", so %s prints CK. The three outputs concatenate to JACK - a coincidence engineered by the question rather than anything the code copies.

Please log in or register to report this question.

65.

What is the output of this C code?

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 is a value, not an object, so it cannot be incremented: the ++ operator needs a modifiable lvalue and the result of & is neither modifiable nor an lvalue. The next question shows the working version, in which a pointer variable holds the address and it is the variable that is incremented.

Please log in or register to report this question.

66.

Are the three declarations char **apple, char *apple[ ], and char apple[ ][ ] same?

Answer: B

Correct answer: False

The three declare different things. char **apple is a pointer to a pointer. char *apple[] is an array of pointers, which decays to char ** when passed as a parameter but is not the same object. And char apple[][] is not even legal, since all dimensions but the first must be given for the compiler to compute the element size.

Please log in or register to report this question.

67.

Is there any difference between the following two statements?

char *p=0;
char *t=NULL;

 

Answer: B

Correct answer: No

Both give a null pointer. A constant expression with value zero used in a pointer context is a null pointer constant, and NULL is defined to be exactly such a constant, so the two initialisations are equivalent. Using NULL is preferred only because it documents the intent, not because it produces anything different.

Please log in or register to report this question.

68.

What is the output of this C code?

void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int *r = &p;
printf("%d", (**r));
}

 

Answer: B

Correct answer: Compile time error

p is an int *, so &p has type int **, and initialising an int * from it is a constraint violation - the levels of indirection do not match. The declaration should read int **r = &p;, which is exactly what the following question does. Some compilers downgrade this to a warning, but it is an error in standard C.

Please log in or register to report this question.

69.

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.

70.

Which of the following are correct syntaxes to send an array as a parameter to function:

Answer: B

Correct answer: func(array);

An array name in an expression decays to a pointer to its first element, so passing the name is all that is required and the function receives that pointer. &array gives a pointer to the whole array, a different type; *array passes the first element rather than the array; and array[size] passes one element, and one that is out of bounds at that.

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