C Language | Set 3


Following questions have been asked in GATE CS exam.

1.Assume the following C variable declaration

 int *A [10], B[10][10];  

Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program (GATE CS 2003)?

a) I, II, and IV only
b) II, III, and IV only
c) II and IV only
d) IV only

Answer (a)
See below program

int main()
  int *A[10], B[10][10];    
  int C[] = {12, 11, 13, 14};

  /* No problem with below statement as A[2] is a pointer 
     and we are assigning a value to pointer */
  A[2] = C; 

  /* No problem with below statement also as array style indexing 
      can be done with pointers*/
  A[2][3] = 15;

  /* Simple assignment to an element of a 2D array*/
  B[2][3]  = 15;

  printf("%d %d", A[2][0], A[2][3]);

2. Consider the following declaration of a ‘two-dimensional array in C:

 char a[100][100]; 

Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50] is (GATE CS 2002)
a) 4040
b) 4050
c) 5040
d) 5050


Address of a[40][50] = 
     Base address + 40*100*element_size + 50*element_size
     0 + 4000*1 + 50*1

3. The C language is. (GATE CS 2002)
a) A context free language
b) A context sensitive language
c) A regular language
d) Parsable fully only by a Turing machine

Answer (a)
Most programming languages including C, C++, Java and Pascal can be approximated by context-free grammar and compilers for them have been developed based on properties of context-free languages.


4 The most appropriate matching for the following pairs (GATE CS 2000)

X: m=malloc(5); m= NULL;        1: using dangling pointers
Y: free(n); n->value=5;         2: using uninitialized pointers
Z: char *p; *p = ’a’;           3. lost memory is:

(a) X—1 Y—3 Z-2
(b) X—2 Y—1 Z-3
(C) X—3 Y—2 Z-1
(d) X—3 Y—1 Z-2

Answer (d)
X -> A pointer is assigned to NULL without freeing memory so a clear example of memory leak
Y -> Trying to retrieve value after freeing it so dangling pointer.
Z -> Using uninitialized pointers

5. Consider the following C-program:

void foo(int n, int sum)
  int k = 0, j = 0;
  if (n == 0) return;
    k = n % 10; 
  j = n / 10;
  sum = sum + k;
  foo (j, sum);
  printf ("%d,", k);

int main ()
  int a = 2048, sum = 0;
  foo (a, sum);
  printf ("%d\n", sum);

What does the above program print? (GATE CS 2005)
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(C) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8, 0

Answer (d)
sum has no use in foo(), it is there just to confuse. Function foo() just prints all digits of a number. In main, there is one more printf statement after foo(), so one more 0 is printed after all digits of n.

Please write comments if you find any of the above answers/explanations incorrect or you want to add some more information about questions.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

2.6 Average Difficulty : 2.6/5.0
Based on 13 vote(s)