The lexical analyzer is the first phase of a compiler, and GATE tests it more than its size in the syllabus would suggest. The questions cluster around a few ideas: what counts as a token, why a scanner only needs the power of a finite automaton, how keywords are recognised, and where the front end ends and the rest of the compiler begins. Because token patterns are regular expressions, lexical analysis is really an application of regular languages, so pair this set with the Finite Automata MCQs once you finish. Below are 10 solved MCQs from KnowledgeGate's published question bank, every one a previous-year GATE problem with the year marked. Attempt each before reading the explanation, and follow the inline link to its solved page inside the Compiler Design learn module.
Tokens, lexemes and counting them
Q1. In a compiler, keywords of a language are recognised during (GATE 2011, see the solved page)
(a) parsing of the program
(b) the code generation
(c) the lexical analysis of the program
(d) dataflow analysis
Answer: (c) the lexical analysis of the program.
The scanner groups source characters into lexemes and classifies each against the language's token patterns, including its reserved-word list. So keywords are separated from ordinary identifiers here, and the parser then receives a clean token stream in which a keyword already carries its own token type.
Q2. The number of tokens in the C statement printf("i = %d, &i = %x", i, &i); is (GATE 2000, see the solved page)
(a) 3
(b) 26
(c) 10
(d) 21
Answer: (c) 10.
The whole string literal "i = %d, &i = %x" is a single token, not one per character. Counting the units gives printf, (, the string, comma, i, comma, &, i, ), and the semicolon, which is exactly ten tokens. The trap is to count inside the quotes.
Q3. The number of tokens in the Fortran statement DO 10 I = 1.25 is (GATE 1999, see the solved page)
(a) 3
(b) 4
(c) 5
(d) None of the above
Answer: (a) 3.
Classic Fortran ignores blanks inside statements, so DO 10 I = 1.25 reads as DO10I = 1.25. Because the right-hand side is 1.25 (a real constant, not 1,25), this is an assignment to a variable named DO10I, giving the three tokens DO10I, =, and 1.25. This is the famous example of why a lexer needs more than local context.
Q4. For line 3 of a C program, fro (I = 0, I < N, I++);, the compiler's response while creating the object module is (GATE 2005, see the solved page)
(a) Both lexical and syntactic errors
(b) Only a lexical error
(c) Only syntactic errors
(d) No compilation error
Answer: (d) No compilation error.
Every piece of the line tokenises cleanly: fro is a valid identifier, and the parentheses, commas and ++ are all legal symbols, so the scanner reports nothing. It also parses in C89: because fro is an ordinary identifier followed by a parenthesised, comma-separated list, the line is read as a call to an implicitly declared function fro, which is syntactically valid. Nothing is wrong until link time, when fro resolves to no definition, so the object module is created without any compilation error.
Regular expressions and the finite-automaton scanner
Q5. Lexical analysis for a modern language such as Java needs the power of which machine model, in a necessary-and-sufficient sense? (GATE 2011, see the solved page)
(a) Finite state automata
(b) Deterministic pushdown automata
(c) Non-deterministic pushdown automata
(d) Turing machine
Answer: (a) Finite state automata.
Token definitions are written as regular expressions, and the languages a regular expression describes are exactly the ones a finite automaton recognises. Any token pattern can be compiled into an NFA and then a DFA, and nothing stronger is required, which is why scanners are built on finite automata rather than pushdown machines.
Q6. A scanner uses patterns T1: a?(b|c)*a, T2: b?(a|c)*b, T3: c?(b|a)*c over {a, b, c}, and always emits the token matching the longest prefix. For the input bbaacabc, the token sequence is (GATE 2018, see the solved page)
(a) T1 T2 T3
(b) T1 T1 T3
(c) T2 T1 T3
(d) T3 T3
Answer: (d) T3 T3.
Applying the longest-match rule from the start, T3 = c?(b|a)*c matches bbaac (length 5), beating T1's bba and T2's bb, so the first token is T3. The remaining input abc is again matched longest by T3 = c?(b|a)*c as abc, so the scanner emits T3 a second time. This is a clean drill on both the maximal-munch rule and reading a regular expression.
Compiler phases and the front end
Q7. For int main(){ Integer x; return 0; }, which phase of a seven-phase C compiler throws an error? (GATE 2021, see the solved page)
(a) Lexical analyzer
(b) Syntax analyzer
(c) Semantic analyzer
(d) Machine-dependent optimizer
Answer: (c) Semantic analyzer.
The scanner happily tokenises Integer as an identifier, and the parser accepts Integer x; because it is a grammatically valid declaration form. The error appears only when the semantic analyzer does a symbol-table lookup and type check and finds that Integer names no known type. This question is a good map of which phase catches which kind of mistake.
Q8. Which one of the following is NOT performed during compilation? (GATE 2014, see the solved page)
(a) Dynamic memory allocation
(b) Type checking
(c) Symbol table management
(d) Inline expansion
Answer: (a) Dynamic memory allocation.
Type checking, symbol-table management and inline expansion are all compile-time activities. Dynamic memory allocation happens at run time, when the running program actually requests memory, so it is not part of compilation at all.
Q9. Which data structure in a compiler manages information about variables and their attributes? (GATE 2010, see the solved page)
(a) Abstract syntax tree
(b) Symbol table
(c) Semantic stack
(d) Parse table
Answer: (b) Symbol table.
The symbol table records each identifier's name, type, scope, and memory offset, and every later phase consults it. It is usually built on a hash table for fast lookup, with a stack of scopes to handle nested blocks, which is why it is the compiler's central bookkeeping structure.
Q10. Consider S1: the front end includes phases independent of the target hardware; S2: the back end includes phases specific to the target hardware; S3: the back end includes phases specific to the source language. Which is CORRECT? (GATE 2023, see the solved page)
(a) Only S1 is true
(b) Only S1 and S2 are true
(c) S1, S2 and S3 are all true
(d) Only S1 and S3 are true
Answer: (b) Only S1 and S2 are true.
The front end handles the language-specific, hardware-independent phases such as scanning, parsing and semantic checks, which makes S1 true. The back end handles target-specific work such as instruction selection and register allocation, which makes S2 true. S3 is false because the language-specific phases live in the front end, not the back end.
Where these 10 fit in your preparation
The set follows the front end from left to right: what a token is and how the scanner counts it (Q1-Q4), why a finite automaton is exactly the right machine (Q5-Q6), and how lexical analysis hands off to the later phases (Q7-Q10). If the token-counting questions tripped you up, the fix is to treat each string literal and each operator as one unit and to remember that whitespace usually separates but does not itself become a token.
Rebuild the theory in the Compiler Design learn module, then move to the next front-end phase with the Parsing MCQs. The complete GATE compiler sequence sits inside GATE Guidance by Sanchit Sir, and the GATE CS category page shows where compiler design fits in the syllabus. Solve, review the misses, and return to this set a week later.


