Q1. Consider the following three C functions:
rn[PI]
rnint * g (void)
rn{
rnint x = 10;
rnreturn (&x);
rn}
rn[P2]
rnint * g (void)
rn{
rnint * px;
rn*px = 10;
rnreturn px;
rn}
rn[P3]
rnint *g (void)
rn{
rnint *px;
rnpx = (int *) malloc (sizeof(int));
rn*px = 10;
rnreturn px;
rn}
rnWhich 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
rnAnswer: (c)
rnExplanation: 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.
rnIn P2, pointer variable px is being assigned a value without allocating memory to it.
rnP3 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.
rnQ2. The value of j at the end of the execution of the following C program.
rnint incr (int i)
rn{
rnstatic int count = 0;
rncount = count + i;
rnreturn (count);
rn}
rnmain ()
rn{
rnint i,j;
rnfor (i = 0; i <=4; i++)
rnj = incr(i);
rn}
rnrn
(a) 10
rn(b) 4
rn(c) 6
rn(d) 7
rnAnswer (a)
rnExplanation: 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.
rnCount will become 0 after the call incr(0)
rnCount will become 1 after the call incr(1)
rnCount will become 3 after the call incr(2)
rnCount will become 6 after the call incr(3)
rnCount will become 10 after the call incr(4)
rnQ3. Consider the following C declaration
rnstruct {
rnshort s [5]
rnunion {
rnfloat y;
rnlong z;
rn}u;
rn} t;
rnAssume 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
rnConsiderations, is (GATE CS 2000)
rn(a) 22 bytes
rn(b) 14 bytes
rn(c) 18 bytes
rn(d) 10 bytes
rnAnswer: (c)
rnExplanation: 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).
rnQ4. The number of tokens in the following C statement.
rnprintf(“i = %d, &i = %x”, i, &i);is
rn(a) 3
rn(b) 26
rn(c) 10
rn(d) 21
rnAnswer (c)
rnExplanation: 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.
rnThere 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.
rnQ5. The following C declarations
rnstruct node
rn{
rnint i;
rnfloat j;
rn};
rnstruct node *s[10] ;
rnrn
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.
rnAnswer: (a)