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

Pointers Questions and Answers

51.

What is the output of this C code?

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

The four bytes holding the integer 10 are read as an IEEE single-precision float, and that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \), which %f prints as 0.000000. The pointer types are incompatible, so a conforming compiler must issue a diagnostic; older compilers treat it as a warning and produce this output, which is what the question is asking for.

Please log in or register to report this question.

52.

What is the output of this C code?

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

 

Answer: A

Correct answer: 0xbfd605e8 0xbfd605ec

Pointer arithmetic is in units of the pointed-to type, so incrementing an int * advances the address by sizeof(int), four bytes on a typical implementation. The second address is therefore four higher than the first, which the option with the last digit changing from 8 to c reflects. The particular addresses vary from run to run; the difference of four does not.

Please log in or register to report this question.

53.

What will be the output of the following C code?

#include <stdio.h>
    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 pointer is passed by value, so foo receives a copy. Assigning p = &j inside foo changes only that copy, and the print there shows 2.

Back in main, the original p still points to i, so the second print shows 97. This is the fundamental point about pointer arguments: passing a pointer lets a function modify what it points to, but not the caller's pointer itself. Changing the caller's pointer requires a pointer to pointer.

Please log in or register to report this question.

54.

What will be the output of the program ?

#include <stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

 

Answer: B

Correct answer: 25

The expression b = **ptr***ptr; tokenises as **ptr * **ptr - the lexer taking the longest valid token at each point, so the run of asterisks splits into two dereferences, a multiplication, and two more dereferences.

ptr is a pointer to aa, which points to a, so **ptr is 5 and the product is 25. The code relies on the old implicit int return type, which was removed in C99, so it would not compile cleanly under a modern standard.

Please log in or register to report this question.

55.

What will be the output of the following C code?

#include <stdio.h>
    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

Read the declaration int *const *p from the inside out: p is a pointer to a const pointer to int. So *p is const-qualified.

The statement *p = &j attempts to assign to that const-qualified object, which the compiler rejects. Compare int **const p, where the const applies to p itself and *p remains modifiable - the position of the qualifier relative to the asterisks decides which level is protected.

Please log in or register to report this question.

56.

Which is an indirection operator among the following?

Answer: B

Correct answer: *

The unary * is the indirection or dereference operator: applied to a pointer it yields the object addressed.

& is its inverse, the address-of operator, producing a pointer from an object. The other two select members - . from a structure and -> from a pointer to one - and -> is in fact indirection and selection combined, p->m being shorthand for (*p).m.

Please log in or register to report this question.

57.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *str;
    str = "%d\n";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}

 

Answer: D

Correct answer: 300

str initially points at the format "%d\n". Two increments advance it two characters, and str-2 then brings it back to where it started.

So printf receives the original format string and prints 300 followed by a newline. The exercise is about pointer arithmetic being exactly reversible: advancing and retreating by the same amount returns the identical address, and the intervening increments leave no trace.

Please log in or register to report this question.

58.

What substitution should be made to //-Ref such that ptr1 points to variable C?

int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a;
int **sptr = &ptr1;
//-Ref
}

 

Answer: A

Correct answer: *sptr = &c;

*sptr is another name for ptr1, so assigning the address of c to it makes ptr1 point at c. The alternative **sptr = &c; would assign a pointer to the int that ptr1 currently points at, and *ptr1 = &c; does the same thing - both are type errors and both write to a rather than redirecting the pointer.

Please log in or register to report this question.

59.

Which of the following declaration throw run-time error?

Answer: D

Correct answer: None of the mentioned

None of the three reaches run time. Each is a type error caught at compilation: &c in the first has type int ***, the second is the same after the & and * cancel, and the third dereferences c twice in its own initialiser. A run-time error would require a program that compiles first.

Please log in or register to report this question.

60.

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.

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