10 MCQs related to Lexical Analysis

10 Multiple Choice Questions (MCQs) related to Lexical Analysis, suitable for UGC NET, SET, and GATE exams:


1. What is the main function of the lexical analyzer in a compiler?

A) Syntax checking
B) Semantic checking
C) Token generation
D) Code optimization

Explanation:
The Lexical Analyzer scans the input program and converts it into tokens, which are the basic building blocks for syntax analysis.

Answer: C) Token generation


2. Which of the following is NOT performed during lexical analysis?

A) Removing white spaces
B) Removing comments
C) Checking for syntax errors
D) Identifying keywords and identifiers

Explanation:
Syntax error checking is done in the Syntax Analysis phase, not in Lexical Analysis.

Answer: C) Checking for syntax errors


3. What type of errors are detected in the lexical analysis phase?

A) Syntax errors
B) Lexical errors
C) Semantic errors
D) Logical errors

Explanation:
Lexical errors include invalid tokens, unrecognized characters, and illegal identifier names.

Answer: B) Lexical errors


4. Which data structure is commonly used by a lexical analyzer?

A) Stack
B) Queue
C) Symbol Table
D) Linked List

Explanation:
The Symbol Table stores information about tokens, such as identifiers and keywords.

Answer: C) Symbol Table


5. What is a token in lexical analysis?

A) A sequence of characters representing a unit of meaning
B) A parsed tree representation of the source code
C) A machine code equivalent of a program
D) An error message in lexical analysis

Explanation:
A token is the smallest unit of a program that has meaning, such as keywords, identifiers, operators, and literals.

Answer: A) A sequence of characters representing a unit of meaning


6. What is the role of a finite automaton in lexical analysis?

A) It parses syntax trees
B) It helps in recognizing tokens
C) It optimizes the generated code
D) It performs semantic checking

Explanation:
A Finite Automaton (FA) is used by a Lexical Analyzer to identify tokens based on regular expressions.

Answer: B) It helps in recognizing tokens


7. Which of the following tools is commonly used to implement lexical analysis?

A) YACC
B) Bison
C) Lex
D) Turbo C++

Explanation:
Lex is a lexical analyzer generator used for building token recognizers.

Answer: C) Lex


8. Which type of grammar is used to define the lexical structure of a programming language?

A) Regular Grammar
B) Context-Free Grammar
C) Context-Sensitive Grammar
D) Unrestricted Grammar

Explanation:
Lexical analysis uses regular expressions, which can be described by Regular Grammar.

Answer: A) Regular Grammar


9. In lexical analysis, keywords are classified as which type of token?

A) Identifier
B) Operator
C) Reserved Word
D) Literal

Explanation:
Keywords are reserved words with predefined meanings and cannot be used as identifiers.

Answer: C) Reserved Word


10. The output of a lexical analyzer is given to which phase of the compiler?

A) Syntax Analysis
B) Code Optimization
C) Code Generation
D) Semantic Analysis

Explanation:
The Lexical Analyzer sends tokens to the Syntax Analyzer, which checks grammatical structure.

Answer: A) Syntax Analysis

Comments