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

Pointers Questions and Answers

81.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}

 

Answer: D

Correct answer: ce

The expression s++ +3 uses the original value of s, since ++ here is postfix, and adds 3 to it. s points at the start of "peace", so the argument to printf addresses position 3.

Counting from zero - p, e, a, c - position 3 is the c, so the output is ce. The increment of s itself happens too, but s is never used again. The spacing in s++ +3 is necessary: s+++3 would tokenise differently.

Please log in or register to report this question.

82.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int i = 97, *p = &i;
        foo(&p);
        printf("%d ", *p);
        return 0;
    }
    void foo(int **p)
    {
        int j = 2;
        *p = &j;
        printf("%d ", **p);
    }

 

Answer: A

Correct answer: 2 2

foo now takes a int ** and is given &p, so it can reach the caller's pointer itself. Assigning *p = &j changes main's p to point at j, and both prints show 2.

There is a hazard concealed in the output, though: j is local to foo and its lifetime ends when foo returns, so main's p is left dangling. The second print happens to find 2 still in that stack location, but reading it is undefined behaviour.

Please log in or register to report this question.

83.

How many number of pointer (*) does C have against a pointer variable declaration?

Answer: D

Correct answer: No limits

The standard sets no limit on the number of levels of indirection, and a conforming implementation must support at least twelve pointer, array and function declarators modifying a type. In practice more than two or three levels is a sign that the design would be better served by a structure or a typedef, since the declarations become almost unreadable.

Please log in or register to report this question.

84.

What is the output of the code below?

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

int *const p in main makes p a constant pointer, but the address of it is passed to a function taking a plain int **, which discards the qualifier. That is a constraint violation and a conforming compiler must diagnose it, though most treat it as a warning and compile. The code then modifies p through the unqualified pointer and both prints give 11 - which is precisely why the warning matters.

Please log in or register to report this question.

85.

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.

86.

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.

87.

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.

88.

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.

89.

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.

90.

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.

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