C Language | Set 3 - GeeksforGeeks

09 Apr, 2018
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 expressionsI 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 onlyb) II, III, and IV onlyc) II and IV onlyd) 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]);  getchar();}  
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) 4040b) 4050c) 5040d) 5050
Answer(b)
Address of a[40][50] = 
     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 languageb) A context sensitive languagec) A regular languaged) Parsable fully only by a Turing machine
Answer (b)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.
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.
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 leakY -> 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);      getchar();}
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-2000
GATE-CS-2002
GATE-CS-2003
GATE-CS-2005
GATE-CS-C-Language
GATE CS
MCQ
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Differences between TCP and UDP
Data encryption standard (DES) | Set 1
Introduction of Operating System - Set 1
Types of Network Topology
Inter Process Communication (IPC)
Data Structures and Algorithms | Set 21
Practice questions on Height balanced/AVL Tree
Operating Systems | Set 1
Computer Networks | Set 1
Computer Networks | Set 2