1.
How many tokens are there in the following code snippet? Consider semicolon as a token.int f(int n){static int i=1;if(n>= 5) printf("Value = %d”, n);n=n+i;i++;return f(n);}
Correct Answer
A. 42
Explanation
The code snippet consists of multiple tokens including keywords (int, static, if, printf, return), identifiers (f, n, i), operators (=, >=, ++), literals (5, 1), punctuations ({, }, (, ), ;), and a format specifier (%d). Counting all these tokens gives a total of 42.
2.
Compute FIRST of ‘S’ for the following grammar:S → ABb | bAA → BSb | λB →ab | λ
Correct Answer
B. A,b
Explanation
The FIRST of 'S' for the given grammar is {a, b}. This is because 'S' can derive either 'ABb' or 'bAA'. The FIRST of 'ABb' is {a} and the FIRST of 'bAA' is {b}. Therefore, the FIRST of 'S' is the union of the FIRST of 'ABb' and the FIRST of 'bAA', which is {a, b}.
3.
Compute FOLLOW of ‘B’ for the following grammar:S → ABb | bAA → BSb | λB →ab | λ
Correct Answer
D. A,b
Explanation
The FOLLOW set of a non-terminal symbol in a grammar consists of all the terminals that can appear immediately to the right of that non-terminal in any derivation. In this case, the non-terminal 'B' can appear immediately to the right of 'a' and 'b'. Therefore, the correct answer is 'a,b'.
4.
A top down parser generates
Correct Answer
C. Left most derivation
Explanation
A top-down parser generates a left-most derivation. This means that it starts with the start symbol of the grammar and repeatedly replaces the leftmost non-terminal symbol with a production rule until it derives the input string. The left-most derivation is the process of deriving the input string from the start symbol by always replacing the leftmost non-terminal symbol. This is in contrast to a right-most derivation, where the rightmost non-terminal symbol is always replaced.
5.
Is the following grammar LL(1)? Give reason.S → Ab | aA → Sb | b
Correct Answer
B. No, because of left recursion
Explanation
The given grammar is not LL(1) because of left recursion. Left recursion occurs when a non-terminal symbol appears as the first symbol on the right-hand side of a production rule. In this case, the non-terminal symbol "A" appears as the first symbol in the production rule "A → Sb". This left recursion makes it impossible to determine which production rule to apply when parsing the grammar using LL(1) parsing technique, as it leads to an infinite loop.
6.
Which one of the following lexeme is said to be a token without looking at next character?
Correct Answer
D. None
Explanation
The answer "none" is correct because without looking at the next character, it is impossible to determine whether any of the given lexemes should be considered as a token. A token is a meaningful unit of code, and in order to determine whether a lexeme should be considered a token, it is necessary to consider the context and the surrounding characters. Without this information, it is not possible to definitively classify any of the given lexemes as tokens.
7.
A programmer, by mistake, writes an instruction to divide, instead of a multiply, such error can be detected by a/an
Correct Answer
D. None of these
Explanation
This error cannot be detected by a compiler, interpreter, or a compiler or interpreter test because dividing instead of multiplying is a logical error that does not violate the syntax rules of the programming language. The program will run without any errors, but the output will be incorrect. Therefore, none of the given options can detect this specific error.
8.
A compiler for a high level language that runs on one machine and produce code for different machine is called
Correct Answer
C. Cross compiler
Explanation
A compiler for a high-level language that runs on one machine and produces code for different machines is called a cross compiler. This type of compiler is designed to generate executable code for a target machine that is different from the machine on which the compiler is running. It allows developers to write code in a high-level language and then compile it into machine code that can be executed on a different architecture or platform. Cross compilers are commonly used in software development for creating programs that can run on multiple platforms or operating systems.
9.
Intermediate code generation phase gets input from
Correct Answer
C. Semantic analyzer
Explanation
The intermediate code generation phase receives input from the semantic analyzer. This is because the semantic analyzer is responsible for checking the meaning and correctness of the code, ensuring that it follows the rules and constraints of the programming language. Once the semantic analysis is complete, the intermediate code generation phase can then use this information to generate an intermediate representation of the code that can be further optimized and translated into machine code.
10.
LL(1) is
Correct Answer
B. Left to right, leftmost derivation
, 1 lookahead symbol
Explanation
LL(1) is a parsing method where the input is scanned from left to right, and the leftmost non-terminal symbol is always chosen for expansion. It also uses a lookahead of 1 symbol to determine which production rule to apply. This means that the next input symbol is examined to determine the correct production to use. Therefore, the correct answer is "Left to right, leftmost derivation, 1 lookahead symbol."