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

Pointers Questions and Answers

Answer: C

Correct answer: A variable that stores address of other variable

A pointer is a variable whose value is the address of another object. It has a type - the type of what it points to - which determines both how the pointed-to bytes are interpreted and how pointer arithmetic is scaled.

It is not a keyword; there is no pointer keyword in C, the asterisk in a declaration doing the work. And while a function pointer holds the address of code, the general definition is in terms of objects, so the option naming instructions describes a special case rather than the concept.

Please log in or register to report this question.

82.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str1[] = "India";
    char str2[] = "BIX";
    char *s1 = str1, *s2=str2;
    while(*s1++ = *s2++)
        printf("%s", str1);

    printf("\n");
    return 0;
}

 

Answer: B

Correct answer: BndiaBIdiaBIXia

The loop copies str2 into str1 one character at a time, printing the whole of str1 after each copy, and stops when the assigned value is the terminating null.

After copying B, str1 reads Bndia; after I, BIdia; after X, BIXia. The three prints concatenate to BndiaBIdiaBIXia. Note that the assignment's value is what controls the loop, which is the standard C string-copy idiom.

Please log in or register to report this question.

Answer: B

Correct answer: k is a pointer to a pointer to a pointer to a pointer to a char

Each asterisk adds one level of indirection, so four asterisks give four levels: k is a pointer to a pointer to a pointer to a pointer to char.

Counting the asterisks is the whole technique for reading such declarations. In practice more than two or three levels are rare outside argument-vector manipulation and certain data structures, and where they arise a typedef for the intermediate levels usually makes the code readable.

Please log in or register to report this question.

84.

What is the output of this C code?

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

 

Answer: B

Correct answer: Compile time error

int *const ptr declares a constant pointer to a modifiable int: the pointer itself may not be changed after initialisation. So ptr++ is an attempt to modify a const object and the compiler rejects it. Read the declaration from the name outward - const binds to what is on its left, so here it qualifies the pointer rather than the pointee.

Please log in or register to report this question.

85.

What is the output of this C code?
 

void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}

 

Answer: D

Correct answer: Compile time error

The last argument is **p, but p is an int *, so *p is an int and applying * to an integer is not allowed. The number of dereferences must match the number of levels the pointer actually has. Writing **m instead, as in the previous question, is the correct form and gives 5.

Please log in or register to report this question.

86.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%s\n", str);
    return 0;
}

 

Answer: A

Correct answer: Mello

char *const p makes the pointer const, not what it points to. So p cannot be made to point elsewhere, but *p = 'M' is perfectly legal and writes through it.

str is a modifiable array, so the write succeeds and the string becomes Mello. Compare const char *p, where the qualifier applies to the pointed-to characters and the assignment would be rejected - the position of const relative to the asterisk decides which is protected.

Please log in or register to report this question.

87.

What will be the output of the following C code?

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

 

Answer: B

Correct answer: 11 11 Undefined-value

int **const p makes p itself const but leaves *p modifiable, so *p = &j is legal and main's pointer is redirected to j.

The first two prints show 11. But j is local to foo, and its lifetime ends when foo returns, so the pointer left in main is dangling. The second print in main reads that dead stack slot, which may still hold 11 or may not - it is undefined behaviour, and the intervening call to printf is exactly the sort of thing that overwrites it.

Please log in or register to report this question.

Answer: A

Correct answer: You cannot change the value pointed by ptr

The const applies to what ptr points at, not to ptr itself, so *ptr = 5 is rejected while ptr = &other is allowed.

Read such declarations from the identifier outwards, or apply the rule that const qualifies whatever lies to its left unless there is nothing there, in which case it qualifies what lies to its right. int * const ptr reverses the meaning - a constant pointer to a modifiable int - and const int * const ptr fixes both.

Please log in or register to report this question.

89.

What is the output of this C code?

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

 

Answer: A

Correct answer: 2 97

The parameter p inside foo is a local copy of the pointer, so p = &j redirects only that copy. It prints 2. Back in main the original pointer is untouched and still points at i, so the second print gives 97. C passes everything by value, pointers included - which is exactly why the next question passes the address of the pointer instead.

Please log in or register to report this question.

90.

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.

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