Sample questions
Question 1Easy
- A syntax error
- A compiled file
- A meaningful unit such as a keyword or identifier
- A memory address
Answer: C. A meaningful unit such as a keyword or identifier
Explanation
The lexer recognises tokens using patterns usually written as regular expressions.
Step-by-step solution
- The lexer recognises tokens using patterns usually written as regular expressions.
- So the answer is: A meaningful unit such as a keyword or identifier.
Why the other options are wrong
- A. Errors are reported separately.
- B. A file is a larger unit.
- D. Addresses are used at run time.
Question 2Easy
Which phase of a compiler reads the source code and produces tokens?
- Syntax analysis phase
- Code generation phase
- Optimisation
- Lexical analysis
Answer: D. Lexical analysis
Explanation
The lexer groups characters into tokens such as keywords, identifiers and operators.
Step-by-step solution
- The lexer groups characters into tokens such as keywords, identifiers and operators.
- So the answer is: Lexical analysis.
Why the other options are wrong
- A. Syntax analysis builds the parse tree.
- B. Code generation outputs target code.
- C. Optimisation improves code.
Question 3Medium
The output of the lexical analyser is
- A parse tree
- Machine code
- A stream of tokens
- An executable file
Answer: C. A stream of tokens
Explanation
The lexer converts characters into tokens for the parser.
Step-by-step solution
- The lexer converts characters into tokens for the parser.
- So the answer is: A stream of tokens.
Why the other options are wrong
- A. The parse tree comes from the parser.
- B. Machine code comes from code generation.
- D. An executable comes from linking.
Question 4Medium
Error recovery in which the parser skips input until a synchronising token is found is called
- Phrase-level
- Global correction
- Error productions
- Panic mode
Answer: D. Panic mode
Explanation
Panic mode discards tokens until a safe point such as a semicolon.
Step-by-step solution
- Panic mode discards tokens until a safe point such as a semicolon.
- So the answer is: Panic mode.
Why the other options are wrong
- A. Phrase-level makes local fixes.
- B. Global correction finds a minimal change.
- C. Error productions add rules for errors.
Question 5Medium
A three-address code for a = b + c * d is
- t1 = b + c; t2 = t1 * d; a = t2
- a = b; a = a + c; a = a * d
- t1 = d * b; a = t1 + c
- t1 = c * d; t2 = b + t1; a = t2
Answer: D. t1 = c * d; t2 = b + t1; a = t2
Explanation
Multiplication binds tighter than addition, so c * d is computed first.
Step-by-step solution
- Multiplication binds tighter than addition, so c * d is computed first.
- So the answer is: t1 = c * d; t2 = b + t1; a = t2.
Why the other options are wrong
- A. This follows the wrong precedence.
- B. This also ignores precedence.
- C. This computes the wrong expression.
Question 6Medium
- Top-down parsers
- Bottom-up parsers
- Lexical analysers
- Assemblers
Answer: B. Bottom-up parsers
Explanation
LR parsers build a rightmost derivation in reverse by shifting and reducing.
Step-by-step solution
- LR parsers build a rightmost derivation in reverse by shifting and reducing.
- So the answer is: Bottom-up parsers.
Why the other options are wrong
- A. Top-down parsers are LL.
- C. Lexers produce tokens.
- D. Assemblers translate assembly.
Question 7Medium
Peephole optimisation works on
- A small window of generated instructions
- The whole source file
- The lexer rules
- Only comments
Answer: A. A small window of generated instructions
Explanation
It looks at short instruction sequences and replaces them with better ones.
Step-by-step solution
- It looks at short instruction sequences and replaces them with better ones.
- So the answer is: A small window of generated instructions.
Why the other options are wrong
- B. Peephole is local, not whole-program.
- C. It is not about the lexer.
- D. It does not work on comments.
Question 8Medium
Which tool is commonly used to generate lexical analysers?
- Yacc
- Lex or Flex
- GDB
- Make
Answer: B. Lex or Flex
Explanation
Lex builds a scanner from regular expression patterns.
Step-by-step solution
- Lex builds a scanner from regular expression patterns.
- So the answer is: Lex or Flex.
Why the other options are wrong
- A. Yacc generates parsers.
- C. GDB is a debugger.
- D. Make is a build tool.
Question 9Hard
An activation record (stack frame) typically stores
- The source code
- Local variables, parameters and return address
- The symbol table
- The token list
Answer: B. Local variables, parameters and return address
Explanation
Each function call pushes an activation record onto the run-time stack.
Step-by-step solution
- Each function call pushes an activation record onto the run-time stack.
- So the answer is: Local variables, parameters and return address.
Why the other options are wrong
- A. Source code is compile-time.
- C. The symbol table is compile-time.
- D. Tokens are compile-time.
Tip: An activation record (stack frame) holds local variables, parameters and the return address, and is pushed on each function call.
Question 10Hard
Replacing x * 2 by x + x or a shift is an example of
- Constant folding
- Loop invariant motion
- Strength reduction
- Common subexpression elimination
Answer: C. Strength reduction
Explanation
Strength reduction replaces expensive operations with cheaper ones.
Step-by-step solution
- Strength reduction replaces expensive operations with cheaper ones.
- So the answer is: Strength reduction.
Why the other options are wrong
- A. Constant folding works on constants.
- B. Loop invariant motion moves code out of loops.
- D. CSE reuses computed values.
Tip: Strength reduction swaps an expensive operation for a cheaper one, like x * 2 becoming x + x or a shift. Spot cheaper equivalents.
Practise all 25 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →