Deep Thinking Conceptual C Programming Questions

Deep Thinking Conceptual C Programming Questions

Q1. Consider the following three C functions:

rn

 [PI]

rn

int * g (void)

rn

{

rn

int x = 10;

rn

return (&x);

rn

}

rn

[P2]

rn

 int * g (void)

rn

{

rn

int * px;

rn

*px = 10;

rn

return px;

rn

}

rn

[P3]

rn

int *g (void)

rn

{

rn

int *px;

rn

px = (int *) malloc (sizeof(int));

rn

*px = 10;

rn

return px;

rn

}

rn

Which of the above three functions is likely to cause problems with pointers?

rn

(a) Only P3

rn

(b) Only P1 and P3

rn

(c) Only P1 and P2

rn

(d) P1, P2 and P3

rn

Answer: (c)

rn

Explanation: In P1, pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after g() has returned as x exists on stack. So, &x may become invalid.

rn

In P2, pointer variable px is being assigned a value without allocating memory to it.

rn

P3 works perfectly fine. Memory is allocated to pointer variable px using malloc(). So, px exists on heap, it’s existence will remain in memory even after return of g() as it is on heap.

rn

Q2. The value of j at the end of the execution of the following C program.

rn

int incr (int i)

rn

{

rn

static int count = 0;

rn

count = count + i;

rn

return (count);

rn

}

rn

main ()

rn

{

rn

int i,j;

rn

for (i = 0; i <=4; i++)

rn

j = incr(i);

rn

}

rn

 

rn

(a) 10

rn

(b) 4

rn

(c) 6

rn

(d) 7

rn

Answer (a)

rn

Explanation: count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first call. Other calls to this function will take the old values of count.

rn

Count will become 0 after the call incr(0)

rn

Count will become 1 after the call incr(1)

rn

Count will become 3 after the call incr(2)

rn

Count will become 6 after the call incr(3)

rn

Count will become 10 after the call incr(4)

rn

Q3. Consider the following C declaration

rn

struct {

rn

short s [5]

rn

union {

rn

float y;

rn

long z;

rn

}u;

rn

} t;

rn

Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment

rn

Considerations, is (GATE CS 2000)

rn

(a) 22 bytes

rn

(b) 14 bytes

rn

(c) 18 bytes

rn

(d) 10 bytes

rn

Answer: (c)

rn

Explanation: Short array s[5] will take 10 bytes as size of short is 2 bytes. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).

rn

Q4. The number of tokens in the following C statement.

rn

printf(“i = %d, &i = %x”, i, &i);is

rn

(a) 3

rn

(b) 26

rn

(c) 10

rn

(d) 21

rn

Answer (c)

rn

Explanation: In a C source program, the basic element recognized by the compiler is the “token.” A token is source-program text that the compiler does not break down into component elements.

rn

There are 6 types of C tokens: identifiers, keywords, constants, operators, string literals and other separators. There are total 10 tokens in the above printf statement.

rn

Q5. The following C declarations

rn

struct node

rn

{

rn

int i;

rn

float j;

rn

};

rn

struct node *s[10] ;

rn

 

rn

Defines to be:

rn

(a) An array, each element of which is a pointer to a structure of type node

rn

(b) A structure of 2 fields, each field being a pointer to an array of 10 elements

rn

(c) A structure of 3 fields: an integer, a float, and an array of 10 elements

rn

(d) An array, each element of which is a structure of type node.

rn

Answer: (a)

c