Q.1 What is the primary role of lexical analysis in a compiler?
Parsing syntax
Converting tokens to machine code
Breaking input into tokens
Optimizing code
Explanation - Lexical analysis scans the source code and breaks it into tokens like identifiers, keywords, and symbols.
Correct answer is: Breaking input into tokens
Q.2 Which of the following tools is commonly used for lexical analysis?
YACC
LEX
GCC
Bison
Explanation - LEX is a tool that generates lexical analyzers from regular expression specifications.
Correct answer is: LEX
Q.3 A lexical analyzer reads input characters and produces:
Intermediate code
Tokens
Machine instructions
Parse tree
Explanation - Lexical analyzer generates tokens that are passed to the parser for syntax analysis.
Correct answer is: Tokens
Q.4 Which finite automaton is used by lexical analyzers?
Deterministic Finite Automata (DFA)
Pushdown Automata
Turing Machine
Non-deterministic PDA
Explanation - Lexical analyzers implement DFAs for efficient token recognition.
Correct answer is: Deterministic Finite Automata (DFA)
Q.5 Regular expressions are used to define:
Context-free grammar
Syntax rules
Lexical tokens
Machine code
Explanation - Regular expressions describe the patterns of tokens recognized during lexical analysis.
Correct answer is: Lexical tokens
Q.6 What is the first phase of compilation?
Parsing
Lexical Analysis
Semantic Analysis
Code Generation
Explanation - Lexical analysis is the first step, where raw code is broken into tokens.
Correct answer is: Lexical Analysis
Q.7 Which data structure is commonly used in lexical analyzers?
Stack
Transition Table
Linked List
Queue
Explanation - Transition tables represent DFA state transitions for token recognition.
Correct answer is: Transition Table
Q.8 Which of the following cannot be represented by regular expressions?
Keywords
Identifiers
Nested parentheses
Operators
Explanation - Regular expressions cannot handle nested structures, which require context-free grammars.
Correct answer is: Nested parentheses
Q.9 Which of the following is an output of lexical analysis?
Syntax tree
Intermediate code
Symbol table
Token stream
Explanation - Lexical analysis produces a sequence of tokens known as the token stream.
Correct answer is: Token stream
Q.10 Finite automata are best suited for recognizing:
Context-free languages
Context-sensitive languages
Regular languages
Recursively enumerable languages
Explanation - Finite automata are used to recognize regular languages, which describe token patterns.
Correct answer is: Regular languages
Q.11 Which automaton is easier to implement directly in lexical analyzers?
NFA
DFA
PDA
Turing Machine
Explanation - DFAs are deterministic and easy to implement in code for token recognition.
Correct answer is: DFA
Q.12 Which of the following tokens is usually recognized by reserved keywords?
Operators
Identifiers
Keywords
Literals
Explanation - Lexical analyzers recognize keywords as specific reserved words in programming languages.
Correct answer is: Keywords
Q.13 The process of removing comments and whitespace in lexical analysis is called:
Lexeme simplification
Tokenization
Lexical filtering
Lexeme reduction
Explanation - Comments and whitespace are usually removed by the lexical analyzer via filtering.
Correct answer is: Lexical filtering
Q.14 A DFA has how many possible transitions for a given state and input symbol?
Zero or one
One or more
Exactly one
Unlimited
Explanation - In a DFA, every state has exactly one transition for each input symbol.
Correct answer is: Exactly one
Q.15 An NFA can be converted into:
Turing Machine
CFG
DFA
Stack Automata
Explanation - Every NFA has an equivalent DFA that recognizes the same language.
Correct answer is: DFA
Q.16 Which operation is NOT supported by regular expressions?
Union
Concatenation
Intersection
Kleene star
Explanation - Regular expressions allow union, concatenation, and Kleene star but not intersection directly.
Correct answer is: Intersection
Q.17 The smallest building block recognized by a lexical analyzer is:
Lexeme
Token
Pattern
Grammar
Explanation - A lexeme is the actual character sequence that matches a token pattern.
Correct answer is: Lexeme
Q.18 Which finite automaton allows epsilon (ε) transitions?
DFA
NFA
PDA
Turing Machine
Explanation - NFA can have epsilon transitions, while DFA cannot.
Correct answer is: NFA
Q.19 Which of these phases uses the symbol table heavily?
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation
Explanation - Lexical analyzers maintain symbol tables to record identifiers and keywords.
Correct answer is: Lexical Analysis
Q.20 Which among the following is an invalid token in C language?
int
while
3abc
char
Explanation - Identifiers cannot begin with digits, making '3abc' invalid.
Correct answer is: 3abc
Q.21 What is the difference between token and lexeme?
Token is pattern, lexeme is instance
Token is code, lexeme is rule
Token is keyword, lexeme is operator
No difference
Explanation - A token represents a category (pattern), while a lexeme is the actual text matched.
Correct answer is: Token is pattern, lexeme is instance
Q.22 Which machine is more powerful than finite automata?
PDA
DFA
NFA
Moore Machine
Explanation - Pushdown automata can recognize context-free languages, which finite automata cannot.
Correct answer is: PDA
Q.23 The set of keywords in a programming language is best described as:
Regular set
Context-free set
Recursive set
Non-regular set
Explanation - Keywords can be defined using regular expressions and hence form a regular set.
Correct answer is: Regular set
Q.24 Which of these is NOT a function of lexical analysis?
Error reporting
Token generation
Whitespace removal
Syntax tree construction
Explanation - Lexical analysis does not build syntax trees, which is part of syntax analysis.
Correct answer is: Syntax tree construction
Q.25 A finite automaton with output is known as:
Turing Machine
Moore/Mealy Machine
PDA
CFG
Explanation - Finite automata with outputs are categorized as Moore or Mealy machines.
Correct answer is: Moore/Mealy Machine
