C Misc
Question 1 |
In the C language (GATE CS 2002)
At most one activation record exists between the current activation record and the activation record for the main | |
The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence. | |
The visibility of global variables depends on the actual function calling sequence. | |
Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive function can be called. |
Discuss it
Question 1 Explanation:
a) –> There is no such restriction in C language
b) –> True
c) –> False. In C, variables are statically scoped, not dynamically.
c) –> False. The activation records are stored on the same stack.
Question 2 |
The C language is. (GATE CS 2002)
A context free language | |
A context sensitive language | |
A regular language | |
Parsable fully only by a Turing machine |
Discuss it
Question 2 Explanation:
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.
Question 3 |
The number of tokens in the following C statement is
C
printf("i = %d, &i = %x", i, &i);
3 | |
26 | |
10 | |
21 |
Discuss it
Question 3 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. Below are tokens in above program.
printf ( "i = %d, &i = %x" , i , & i ) ;
Question 4 |
Which of the following best describes C language
C is a low level language | |
C is a high level language with features that support low level programming | |
C is a high level language | |
C is a very high level language |
Discuss it
Question 4 Explanation:
Question 5 |
Assume that the size of an integer is 4 bytes. Predict the output?
#include <stdio.h> int fun() { puts(" Hello "); return 10; } int main() { printf("%d", sizeof(fun())); return 0; }
4 | |
Hello 4 | |
4 Hello | |
Compiler Error |
Discuss it
Question 5 Explanation:
sizeof() is an operator, not a function. It looks like a function though.
The operands of operators need not to be evaluated. That is why fun() is not called.
Question 6 |
#include <stdio.h> int main() { int a[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = a; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %dn", (*ptr)[1], (*ptr)[2]); return 0; }
2 3 5 6 | |
2 3 4 5 | |
4 5 0 0 | |
none of the above |
Discuss it
Question 7 |
Consider line number 3 of the following C- program.
int main ( ) { /* Line 1 */ int I, N; /* Line 2 */ fro (I = 0, I < N, I++); /* Line 3 */ }Identify the compiler's response about this line while creating the object-module
No compilation error | |
Only a lexical error | |
Only syntactic errors | |
Both lexical and syntactic errors |
Discuss it
Question 7 Explanation:
Note that there is 'fro' instead of 'for'. This is not a lexical error as lexical analysis typically involves Tokenization.
Question 8 |
Consider a program P that consists of two source modules M1 and M2 contained in two different files. If M1 contains a reference to a function defined in M2, the reference will be resolved at
Edit-time | |
Compile-time | |
Link-time | |
Load-time |
Discuss it
Question 8 Explanation:
Note:
Static linking is done at link-time, dynamic linking or shared libraries are brought in only at runtime.
(A) Edit-time : Function references can never be given/determined at edit-time or code-writing time. Function references are different from function names. Function names are used at edit-time and function references are determined at linker-time for static libraries or at runtime for dynamic libraries.
(B) Compile-time : Compile time binding is done for functions present in the same file or module.
(C) Link-time : Link time binding is done in the linker stage, where functions present in separate files or modules are referenced in the executable.
(D) Load-time : Function referencing is not done at load-time.
Hence, correct answer would be (C).
This solution is contributed by Vineet Purswani.
Question 9 |
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 ?
I, II and IV only | |
II, III and IV only | |
II and IV only | |
IV only |
Discuss it
Question 9 Explanation:
See http://geeksquiz.com/c-arrays-question-6/ for explanation.
Question 10 |
The C language is:
A context free language | |
A context sensitive language | |
A regular language | |
Parsable fully only by a Turing machine |
Discuss it
Question 10 Explanation:
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.
There are 25 questions to complete.