Open In App

C Language | Set 3

Last Updated : 09 Apr, 2018
Like Article

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 (b)
C and C++ are context-sensitive languages.

There are several reasons:

  1. To parse C and C++, you start by using a very powerful preprocessor. These preprocessors are inevitably written by hand (they are not based on a theoretic foundation like regular expressions or context-free grammars).
  2. The grammar is ambiguous: it has LR conflicts, such as the if-then-else conflict. Parsers typically resolve this using context (an “else” matches the closest “if”).
  3. C and C++ lexers require lexical feedback to differentiate between typedef names and identifiers. That is, the context-sensitive lexer needs help from the “context-free” parser to distinguish between an identifier “foo” and a typedef name “foo”. In this snippet,
    int foo;
    typedef int foo;
    foo x;

    the first “foo” is an identifier while the second and third are typedef names. You need to parse typedef declarations to figure this out (and since types have nested parentheses, this is definitely at least as hard as parsing a context-free language).
    This means that the parser and lexer are mutually recursive, so it doesn’t make sense to say that the parser is context free while the lexer is context sensitive.

Ref: C and C++ are not context free

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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads