Summary

This document is a question bank for a compiler course, providing practice questions on various compiler phases such as lexical analysis, syntax analysis, and semantic analysis. It's suitable for undergraduate computer science students.

Full Transcript

CT- LEC 2 QUESTION BANK CT- LEC 2 QUESTION BANK Questions 1. What is the first phase of a compiler? A) Syntax Analysis B) Code Generation C) Lexical Analysis D) Semantic Analysis 2. What does lexical analysis do in a compiler?...

CT- LEC 2 QUESTION BANK CT- LEC 2 QUESTION BANK Questions 1. What is the first phase of a compiler? A) Syntax Analysis B) Code Generation C) Lexical Analysis D) Semantic Analysis 2. What does lexical analysis do in a compiler? A) Generates intermediate code B) Scans the source code for tokens C) Optimizes the code D) Translates code into machine language 3. What is a lexeme in lexical analysis? A) A regular expression B) A specific token instance C) A type of error message D) A comment in the source code 4. What does the syntax analysis phase do? A) Generates machine code B) Converts the source code into tokens C) Creates a parse tree D) Optimizes the intermediate code 5. What does the syntax analyzer check during the syntax analysis phase? A) If the program follows semantic rules B) If the tokens are arranged correctly according to grammar C) If the program contains errors D) If the program uses correct identifiers 1 CT- LEC 2 QUESTION BANK 6. What is the output of semantic analysis? A) Optimized code B) A parse tree C) An annotated syntax tree D) Machine code 7. What is checked during semantic analysis? A) Whether tokens are valid B) Whether the parse tree follows the rules of the language C) Whether the code is optimized D) Whether the syntax tree is valid 8. What is the role of intermediate code generation? A) Translate code to machine language B) Generate a program for an abstract machine C) Optimize the code D) Parse the source code 9. What does the code optimization phase aim to do? A) Eliminate unnecessary lines of code B) Translate code into machine language C) Generate intermediate code D) Perform error checking 10. Which phase translates the intermediate code into machine code? A) Code Optimization B) Code Generation C) Syntax Analysis D) Semantic Analysis 2 CT- LEC 2 QUESTION BANK 11. What is a symbol table used for in a compiler? A) Storing intermediate code B) Storing program identifiers and their attributes C) Generating machine code D) Storing comments and whitespace 12. ______ is responsible for managing scope in a program? A) Semantic Analysis B) Code Generation C) Symbol Table D) Code Optimization 13. In which phase are comments and white-space eliminated? A) Code Generation B) Semantic Analysis C) Lexical Analysis D) Code Optimization 14. What does a lexical analyzer do if it finds an invalid token? A) Continues parsing the code B) Generates an error C) Optimizes the code D) Generates machine code 15. What is an example of a token in C? A) int value = 100; B) C) 100 D) value 3 CT- LEC 2 QUESTION BANK 16. What is a keyword in programming? A) A reserved word with a special meaning B) A user-defined variable name C) A type of punctuation D) An operator 17. Which of the following is an identifier? A) int B) = C) value D) 100 18. What type of token is used to perform operations in a program? A) Keywords B) Identifiers C) Operators D) Punctuations 19. What role do punctuation tokens play in a programming language? A) Perform operations B) Separate code elements C) Declare variables D) Define constants. 20. What is the purpose of a regular expression in lexical analysis? A) To define the grammar of the language B) To optimize the code C) To generate machine code D) To describe valid patterns for tokens 4 CT- LEC 2 QUESTION BANK 21. What does a regular expression define? A) The syntax tree of a program B) A pattern for valid strings or tokens C) The variables used in a program D) The structure of the symbol table 22. What does a semantic analyzer check for? A) Whether code follows the grammar B) Whether tokens are valid C) Whether intermediate code is optimized D) Whether identifiers are declared before use 23. What is an identifier in a programming language? A) A reserved word B) A variable or function name C) An operator D) A symbol used to separate code elements 24. What does the code generation phase do? A) Checks the grammar of the source code B) Translates intermediate code into machine code C) Performs semantic analysis D) Removes unnecessary code 25. Which of the following is a task of the lexical analyzer? A) Parse the code into a tree B) Translate intermediate code into machine code C) Identify tokens and pass them to the parser D) Generate an annotated syntax tree 5 CT- LEC 2 QUESTION BANK 26. What does the parser do with tokens? A) Generates machine code B) Creates a parse tree C) Optimizes the code D) Declares variables 27. What is the purpose of the code optimization phase? A) Remove errors in the code B) Improve program execution speed without wasting resources C) Parse the code into tokens D) Translate code into an abstract machine representation 28. What is the function of the lexical analyzer's "get next token" command? A) To generate machine code B) To read the next token from the source code C) To optimize the code D) To parse the tokens 29. Which phase of the compiler works closely with the lexical analyzer? A) Syntax Analysis B) Code Optimization C) Code Generation D) Semantic Analysis 30. What does the intermediate code represent? A) Machine code B) A high-level language program C) A program for an abstract machine D) A parse tree 6 CT- LEC 2 QUESTION BANK 31. What is a regular language in the context of lexical analysis? A) A language that requires context-free grammar B) A language that can be defined by regular expressions C) A language with no predefined rules D) A language that includes only machine code 32. Which phase checks the compatibility of data types in expressions? A) Lexical Analysis B) Semantic Analysis C) Syntax Analysis D) Code Optimization 33. Which phase of the compiler generates an annotated syntax tree? A) Code Generation B) Syntax Analysis C) Semantic Analysis D) Code Optimization 34. What is a pattern in the context of lexical analysis? A) A token B) A regular expression that defines a token C) A type of error D) A parse tree structure 7 CT- LEC 2 QUESTION BANK Answers Question Answer 1 C 2 B 3 B 4 C 5 B 6 C 7 B 8 B 9 A 10 B 11 B 12 C 13 C 14 B 15 B 16 A 17 C 18 C 19 B 20 D 21 B 22 D 23 B 24 B 25 C 26 B 27 B 28 B 8 CT- LEC 2 QUESTION BANK 29 A 30 C 31 B 32 B 33 C 34 B We Hope we could Help You Please leave us your feedback Here 9

Use Quizgecko on...
Browser
Browser