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

Pointers Questions and Answers

81.

If the size of integer is 4bytes, What will be the output of the program?

#include <stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}

 

Answer: B

Correct answer: 20, 4, 4

sizeof(arr) gives the size of the whole array - five elements of four bytes, so 20. It is one of the few contexts in which an array name does not decay to a pointer.

*arr is the first element, an int, so its size is 4; and arr[0] is the same object written differently, so it too is 4. The pair is worth noting because sizeof(arr)/sizeof(arr[0]) is the idiomatic way to count an array's elements - and it fails silently once the array has decayed to a pointer.

Please log in or register to report this question.

82.

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.

83.

Which of the following changes can correct the program so that it prints “Geeks Quiz”?

#include <stdio.h> 
  
void myStrcat(char *a, char *b) 
{ 
    int m = strlen(a); 
    int n = strlen(b); 
    int i; 
    for (i = 0; i <= n; i++) 
       a[m+i]  = b[i]; 
} 
  
int main() 
{ 
    char *str1 = "Geeks "; 
    char *str2 = "Quiz"; 
    myStrcat(str1, str2); 
    printf("%s ", str1); 
    return 0; 
} 

 

Answer: A

Correct answer: char *str1 = “Geeks “; can be changed to char str1[100] = “Geeks “;

The fault is that str1 points at a string literal, which must not be modified, and there is no room after it in any case. Making it an array of 100 characters gives writable storage with space to spare. No terminator line is needed, because the copy loop runs to i <= n and so copies b’s own null byte along with its characters.

Please log in or register to report this question.

84.

What is the output of this C code?

void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}

 

Answer: D

Correct answer: 0

ptr holds the address of x, so *ptr retrieves what is stored there, which is 0. There is nothing indeterminate about it - x was explicitly initialised, and the pointer was set from its address, so the dereference is entirely defined. Printing the address rather than the value would require %p and the pointer itself.

Please log in or register to report this question.

85.

How many bytes are occupied by near, far and huge pointers (DOS)?

Answer: A

Correct answer: near=2 far=4 huge=4

These are the pointer types of the segmented 16-bit DOS memory model. A near pointer holds a 16-bit offset within the current segment and occupies 2 bytes. A far pointer holds both segment and offset and occupies 4.

A huge pointer is also 4 bytes but is normalised on every arithmetic operation, so it can address across segment boundaries correctly where a far pointer wraps within its segment. The distinction disappeared with flat 32-bit addressing, and the keywords are of historical interest only.

Please log in or register to report this question.

86.

Which of the following is the correct syntax to send an array as a parameter to function?

Answer: A

Correct answer: func(&array);

Of the four forms offered this is the only valid C that passes the array's address. &array yields a pointer to the whole array, which the function can receive as int (*)[N].

It is worth noting that the idiomatic form, func(array), is not among the options: an array name in an expression decays to a pointer to its first element, so writing the bare name already passes the address. The other options are not valid syntax at all, or pass a single element rather than the array.

Please log in or register to report this question.

87.

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.

88.

What is the output of this C code?

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

 

Answer: A

Correct answer: 10

The postfix ++ yields the value the pointer had before the increment, so foo receives the original address of i and prints 10. The increment happens to p in main, which afterwards points one int past i - harmless here since it is never dereferenced. Prefix ++p would have passed the advanced pointer and the behaviour would then have been undefined.

Please log in or register to report this question.

89.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    printf("%c\n", 7["IndiaBIX"]);
    return 0;
}

 

Answer: C

Correct answer: print "X" of IndiaBIX

Subscripting is defined as x[y] meaning *(x+y), and addition is commutative, so 7["IndiaBIX"] means *(7 + "IndiaBIX") - exactly the same as "IndiaBIX"[7].

Counting from zero, position 7 of IndiaBIX is X. The reversed form is legal but deliberately perverse, and it is worth knowing chiefly as evidence that array subscripting in C really is just pointer arithmetic with syntactic sugar.

Please log in or register to report this question.

90.

If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

Answer: D

Correct answer: ->

The arrow operator accesses a member through a pointer to a structure: p->member is by definition equivalent to (*p).member.

The parentheses in that expansion are essential, which is why the shorthand exists - . binds more tightly than *, so *p.member would be parsed as *(p.member) and rejected. The dot operator is for a structure object itself, not for a pointer to one.

Please log in or register to report this question.

Loading…
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