Tokens, Patterns, and Lexemes MCQs

  1. What is a token in the context of lexical analysis?
    a) A sequence of instructions
    b) A basic unit of syntax
    c) A sequence of characters that represents a meaningful element of the source code
    d) A pattern used to recognize keywordsAnswer: c) A sequence of characters that represents a meaningful element of the source code
  2. Which of the following is an example of a token?
    a) “int”
    b) x = 10
    c) 10 + 20
    d) Both a and bAnswer: d) Both a and b
  3. What is the relationship between tokens, patterns, and lexemes?
    a) A pattern is the definition, a lexeme is the instance, and a token is the category
    b) A lexeme is the definition, a pattern is the category, and a token is the instance
    c) A lexeme is the category, a pattern is the instance, and a token is the definition
    d) A token defines patterns and lexemesAnswer: a) A pattern is the definition, a lexeme is the instance, and a token is the category
  4. Which of the following is a lexeme for the token “integer”?
    a) int
    b) float
    c) integer
    d) numberAnswer: a) int
  5. What is a pattern in lexical analysis?
    a) A specific representation of a token in the source code
    b) A sequence of characters used to identify lexemes
    c) A rule for matching regular expressions
    d) A sequence of instructionsAnswer: b) A sequence of characters used to identify lexemes
  6. What is a lexeme in the context of programming language syntax?
    a) The syntax of a language
    b) A valid regular expression
    c) A sequence of characters that matches a pattern
    d) The structure of a tokenAnswer: c) A sequence of characters that matches a pattern
  7. In lexical analysis, how are tokens typically represented?
    a) As numbers
    b) As characters only
    c) As a data structure containing information like type and value
    d) As keywordsAnswer: c) As a data structure containing information like type and value
  8. Which of the following is NOT a valid example of a token?
    a) identifier
    b) keyword
    c) expression
    d) operatorAnswer: c) expression
  9. What does the lexical analyzer do with patterns during the analysis process?
    a) Matches the patterns to the lexemes in the source code
    b) Converts the patterns into machine code
    c) Passes patterns to the semantic analyzer
    d) Removes patterns from the source codeAnswer: a) Matches the patterns to the lexemes in the source code
  10. What type of regular expression would be used to identify a lexeme for an integer?
    a) [a-zA-Z]+
    b) [0-9]+
    c) [\w]+
    d) \d+Answer: b) [0-9]+

Leave a Comment

All copyrights Reserved by MCQsAnswers.com - Powered By T4Tutorials