Following questions have been asked in GATE CS exam.
1.Assume the following C variable declaration Of the following expressions
int *A , B;
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
Of the following expressions
See below program
2. Consider the following declaration of a ‘two-dimensional array in C:
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a is (GATE CS 2002)
Address of a = Base address + 40*100*element_size + 50*element_size 0 + 4000*1 + 50*1 4050
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
C and C++ are context-sensitive languages.
There are several reasons:
- 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).
- 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”).
- 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.
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
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:
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
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.