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

Pointers Questions and Answers

101.

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.

102.

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.

104.

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.

105.

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.

106.

What will be the output of the following C code?

#include <stdio.h>
    void foo(float *);
    int main()
    {
        int i = 10, *p = &i;
        foo(&i);
    }
    void foo(float *p)
    {
        printf("%f\n", *p);
    }

 

Answer: B

Correct answer: 0.000000

foo expects a float * but is given an int *. The pointer types are incompatible, so a conforming compiler must diagnose this - but the code compiles with a warning on most implementations, and the resulting behaviour is what the question asks about.

Inside foo, the four bytes holding the integer 10 are reinterpreted as a float. The bit pattern 0x0000000A read as an IEEE single is a denormal of the order of \( 10^{-44} \), which prints as 0.000000 at the default precision. The value is an accident of representation, not a conversion.

Please log in or register to report this question.

107.

Will the program compile?

#include <stdio.h>
int main()
{
    char str[5] = "IndiaBIX";
    return 0;
}

 

Answer: A

C doesn't do array bounds checking at compile time, hence this compiles.

But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.

GCC would give you a warning.

Please log in or register to report this question.

108.

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.

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