Compiler Design Questions with Answers

25 practice questions on Compiler Design (Computer Fundamentals) with answers and step-by-step solutions. Try 10 samples below, then practise all 25 free in the AptiNest app.

Sample questions

Question 1Easy

A token is

  1. A syntax error
  2. A compiled file
  3. A meaningful unit such as a keyword or identifier
  4. 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

  1. The lexer recognises tokens using patterns usually written as regular expressions.
  2. 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?

  1. Syntax analysis phase
  2. Code generation phase
  3. Optimisation
  4. Lexical analysis

Answer: D. Lexical analysis

Explanation

The lexer groups characters into tokens such as keywords, identifiers and operators.

Step-by-step solution

  1. The lexer groups characters into tokens such as keywords, identifiers and operators.
  2. 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

  1. A parse tree
  2. Machine code
  3. A stream of tokens
  4. An executable file

Answer: C. A stream of tokens

Explanation

The lexer converts characters into tokens for the parser.

Step-by-step solution

  1. The lexer converts characters into tokens for the parser.
  2. 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

  1. Phrase-level
  2. Global correction
  3. Error productions
  4. Panic mode

Answer: D. Panic mode

Explanation

Panic mode discards tokens until a safe point such as a semicolon.

Step-by-step solution

  1. Panic mode discards tokens until a safe point such as a semicolon.
  2. 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

  1. t1 = b + c; t2 = t1 * d; a = t2
  2. a = b; a = a + c; a = a * d
  3. t1 = d * b; a = t1 + c
  4. 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

  1. Multiplication binds tighter than addition, so c * d is computed first.
  2. 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

LR parsers are

  1. Top-down parsers
  2. Bottom-up parsers
  3. Lexical analysers
  4. Assemblers

Answer: B. Bottom-up parsers

Explanation

LR parsers build a rightmost derivation in reverse by shifting and reducing.

Step-by-step solution

  1. LR parsers build a rightmost derivation in reverse by shifting and reducing.
  2. 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

  1. A small window of generated instructions
  2. The whole source file
  3. The lexer rules
  4. 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

  1. It looks at short instruction sequences and replaces them with better ones.
  2. 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?

  1. Yacc
  2. Lex or Flex
  3. GDB
  4. Make

Answer: B. Lex or Flex

Explanation

Lex builds a scanner from regular expression patterns.

Step-by-step solution

  1. Lex builds a scanner from regular expression patterns.
  2. 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

  1. The source code
  2. Local variables, parameters and return address
  3. The symbol table
  4. 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

  1. Each function call pushes an activation record onto the run-time stack.
  2. 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

  1. Constant folding
  2. Loop invariant motion
  3. Strength reduction
  4. Common subexpression elimination

Answer: C. Strength reduction

Explanation

Strength reduction replaces expensive operations with cheaper ones.

Step-by-step solution

  1. Strength reduction replaces expensive operations with cheaper ones.
  2. 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 →

Related topics

See all Computer Fundamentals topics · All topics