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

Pointers Questions and Answers

31.

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

 

Answer: C

Correct answer: char *p = (char*)malloc(100);

A declaration may carry an initialiser, so the declaration and the assignment combine directly: char *p = (char*)malloc(100);.

The other forms misplace the asterisks. (char)malloc(100) casts the returned pointer to a single character, and the last option is not valid syntax at all. It is worth adding that in C the cast is unnecessary - void * converts to any object pointer type implicitly, and omitting the cast can expose a missing stdlib.h that the cast would hide.

Please log in or register to report this question.

32.

In which header file is the NULL macro defined?

Answer: C

Correct answer: stdio.h and stddef.h

NULL is defined in several headers, among them stdio.h and stddef.h, and also stdlib.h, string.h, time.h and others.

The duplication is deliberate. A programmer using any of those libraries is likely to need NULL, and the standard permits a macro to be defined identically in more than one header, so each defines it rather than forcing an extra include. stddef.h is the canonical home, holding it alongside size_t and ptrdiff_t.

Please log in or register to report this question.

33.

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.

34.

If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

Answer: D

Correct answer: ->

The arrow operator accesses a member through a pointer to a structure: p->member is by definition equivalent to (*p).member.

The parentheses in that expansion are essential, which is why the shorthand exists - . binds more tightly than *, so *p.member would be parsed as *(p.member) and rejected. The dot operator is for a structure object itself, not for a pointer to one.

Please log in or register to report this question.

35.

What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

Answer: B

Correct answer: *(*(*(*(a+i)+j)+k)+l)

Each subscript is defined as a dereference of a sum: x[y] means *(x+y). Applying that rule four times, from the outside in, gives one dereference for every subscript.

\[ a[i][j][k][l] \equiv *(*(*(*(a+i)+j)+k)+l) \]

The options without the asterisks compute addresses but never read through them. Note that the pointer arithmetic is scaled by the size of the sub-array at each level, which is why the array's dimensions must be known to the compiler.

Please log in or register to report this question.

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.

37.

The operator used to get value at address stored in a pointer variable is

Answer: A

Correct answer: *

The unary * is the indirection, or dereference, operator: it yields the object a pointer points to. Its counterpart & is the address-of operator, going the other way.

The asterisk plays two distinct roles that are easy to conflate. In a declaration it forms a pointer type; in an expression it dereferences. The && and || operators are logical AND and OR and have nothing to do with pointers.

Please log in or register to report this question.

38.

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.

39.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int i=3, *j, k;
    j = &i;
    printf("%d\n", i**j*i+*j);
    return 0;
}

 

Answer: A

Correct answer: 30

The expression i**j*i+*j looks alarming but tokenises unambiguously as i * (*j) * i + (*j): the lexer takes the longest valid token at each point, so ** becomes * followed by unary *.

With i = 3 and *j = 3:

\[ 3 \times 3 \times 3 + 3 = 27 + 3 = 30 \]

Multiplication binds tighter than addition, so no parentheses are needed to get this grouping.

Please log in or register to report this question.

40.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}

 

Answer: D

Correct answer: x=31, y=504, z=504

*y++ = *z++ assigns *z to *y - writing 30 back over itself - and then increments both pointers, since ++ in the postfix position binds to the pointer rather than to the dereferenced value.

Pointer arithmetic is scaled by the pointed-to type, so each advances by sizeof(int), that is 4 bytes: from 500 to 504. x++ then makes x 31. Both pointers now point one past x and must not be dereferenced.

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