Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

C Language | Set 1

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Following questions have been asked in GATE CS exam.

1. Consider the following three C functions :

[PI] int * g (void
  int x = 10; 
  return (&x); 
[P2] int * g (void
  int * px; 
  *px = 10; 
  return px; 
[P3] int *g (void
  int *px; 
  px = (int *) malloc (sizeof(int)); 
  *px = 10;
  return px; 

Which of the above three functions are likely to cause problems with pointers? (GATE 2001)
(a) Only P3
(b) Only P1 and P3
(c) Only P1 and P2
(d) P1, P2 and P3

Answer: (c)
Eplaination: 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.
In P2, pointer variable px is being assigned a value without allocating memory to it.
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.

2. The value of j at the end of the execution of the following C program. (GATE CS 2000)

int incr (int i)
   static int count = 0;
   count = count + i;
   return (count);
main ()
   int i,j;
   for (i = 0; i <=4; i++)
      j = incr(i);

(a) 10
(b) 4
(c) 6
(d) 7

Answer (a)
Eplaination: 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.
Count will become 0 after the call incr(0)
Count will become 1 after the call incr(1)
Count will become 3 after the call incr(2)
Count will become 6 after the call incr(3)
Count will become 10 after the call incr(4)

3. Consider the following C declaration

    short s [5];
         float y; 
         long z; 
} t; 

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
considerations, is (GATE CS 2000)

(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes

Answer: (c)
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).

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

printf("i = %d, &i = %x", i, &i);

is (GATE 2000)
(a) 3
(b) 26
(c) 10
(d) 21

Answer (c)
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.
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.

5. The following C declarations

struct node 
   int i; 
   float j; 
struct node *s[10] ; 

define s to be (GATE CS 2000)

(a) An array, each element of which is a pointer to a structure of type node
(b) A structure of 2 fields, each field being a pointer to an array of 10 elements
(c) A structure of 3 fields: an integer, a float, and an array of 10 elements
(d) An array, each element of which is a structure of type node.

Answer: (a)

My Personal Notes arrow_drop_up
Last Updated : 07 Oct, 2021
Like Article
Save Article
Similar Reads