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

Pointers Questions and Answers

51.

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.

52.

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.

53.

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.

54.

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.

55.

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.

56.

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.

57.

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.

58.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 

Answer: A

Correct answer: ink

Work outward. ptr holds {s+3, s+2, s+1, s}, and p = ptr then ++p leaves p pointing at ptr[1], which is s+2.

So *p is s+2, and **p is s[2], the pointer to "pink". Finally **p+1 advances that char pointer one position, so printf starts from the second character and prints ink. The trap is applying the +1 at the wrong level - it binds to the char pointer, not to p.

Please log in or register to report this question.

59.

What is the output of this C code?

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

int *const *p means a pointer to a constant pointer to int, so *p is const and cannot be assigned to. The line *p = &j therefore violates the constraint and the compiler rejects it. Reading such declarations from the name outward is the reliable method: p, pointer to, const, pointer to, int.

Please log in or register to report this question.

60.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

 

Answer: D

Correct answer: JACK

Take the three prints in turn. ch is 74, the code for J, so the first prints J. Then j is 65 and *(int*)vp yields 65, which %c prints as A.

Finally vp is set to cp, and (char*)vp+2 advances two characters into "JACK", so %s prints CK. The three outputs concatenate to JACK - a coincidence engineered by the question rather than anything the code copies.

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