Regular expressions and finite automata(MCQs)

What is a regular expression? a) A pattern that describes a set of strings b) A type of syntax error c) A machine code instruction d) A sequence of tokens Answer: a) A pattern that describes a set of strings Which symbol in regular expressions represents “zero or more occurrences”? a) * b) + c) ? d) | Answer: a) * In regular expressions, what does the + symbol represent? a) Zero or more occurrences b) One or more occurrences c) Optional occurrence d) A specific number of occurrences Answer: b) One or more occurrences What does the regular expression a? match? a) Zero or one occurrence of a b) One or more occurrences of a c) Zero or more occurrences of a d) Exactly one occurrence of a Answer: a) Zero or one occurrence of a Which symbol is used in regular expressions to denote a character class? a) () b) [] c) {} d) | Answer: b) [] What does the regular expression [a-z] represent? a) Any single lowercase letter b) Any single uppercase letter c) Any digit d) Any single character Answer: a) Any single lowercase letter Which of the following regular expressions matches exactly three consecutive digits? a) \d{3} b) \d+ c) [0-9]{3} d) Both a and c Answer: d) Both a and c In regular expressions, what does the | symbol represent? a) Alternation (OR) b) Concatenation c) Zero or more occurrences d) A character class Answer: a) Alternation (OR) What does the regular expression (abc)* match? a) Zero or more occurrences of the sequence abc b) One or more occurrences of the sequence abc c) Exactly three occurrences of the sequence abc d) The string abc followed by any characters Answer: a) Zero or more occurrences of the sequence abc Which of the following regular expressions matches an email address? a) \w+@\w+\.\w+ b) [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} c) [^@]+@[^@]+\.[a-zA-Z]+ d) \w+@\w+ Answer: b) [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} In finite automata, what does a state transition represent? a) The change from one state to another based on input b) The acceptance of a string c) The rejection of a string d) The initialization of the automaton Answer: a) The change from one state to another based on input What is the primary difference between deterministic and nondeterministic finite automata? a) Deterministic automata have a single possible transition for each input symbol, while nondeterministic automata can have multiple possible transitions. b) Nondeterministic automata are more efficient than deterministic automata. c) Deterministic automata can handle more complex languages than nondeterministic automata. d) There is no significant difference between them. Answer: a) Deterministic automata have a single possible transition for each input symbol, while nondeterministic automata can have multiple possible transitions. Which type of finite automaton is more powerful in terms of the languages it can recognize? a) Deterministic finite automaton (DFA) b) Nondeterministic finite automaton (NFA) c) Both DFAs and NFAs are equally powerful d) Neither DFA nor NFA Answer: c) Both DFAs and NFAs are equally powerful What is the purpose of a transition table in finite automata? a) To define the state transitions based on input symbols b) To list all possible states c) To store the accepted strings d) To provide the initial state of the automaton Answer: a) To define the state transitions based on input symbols In a deterministic finite automaton (DFA), what must be true about the transitions? a) There must be exactly one transition for each input symbol from every state b) There can be multiple transitions for the same input symbol from a single state c) The transitions can be random d) Transitions are not needed in a DFA Answer: a) There must be exactly one transition for each input symbol from every state Which of the following is NOT a component of a finite automaton? a) States b) Input symbols c) Transition function d) Syntax tree Answer: d) Syntax tree In an NFA, what is possible regarding state transitions? a) There can be multiple possible transitions for the same input symbol from a single state b) Every transition must be unique c) There are no transitions in an NFA d) State transitions are not used in NFAs Answer: a) There can be multiple possible transitions for the same input symbol from a single state What does ε-transition (epsilon transition) mean in the context of NFAs? a) A transition that occurs without consuming any input symbol b) A transition that occurs with a specific input symbol c) A transition that always results in a loop d) A transition that is not allowed in NFAs Answer: a) A transition that occurs without consuming any input symbol How can you convert an NFA to a DFA? a) By constructing the DFA’s transition table from the NFA’s transition table b) By removing all ε-transitions from the NFA c) By combining all states of the NFA into a single DFA state d) By creating a new NFA from the DFA Answer: a) By constructing the DFA’s transition table from the NFA’s transition table What is the purpose of the initial state in a finite automaton? a) It is the state where the automaton starts processing input b) It is the final state of the automaton c) It is the state where the automaton stops processing input d) It is an intermediate state in the automaton Answer: a) It is the state where the automaton starts processing input In the context of finite automata, what is a “final state”? a) A state where the automaton accepts the input string b) A state where the automaton starts processing input c) A state where the automaton transitions to other states d) A state with no transitions Answer: a) A state where the automaton accepts the input string Which of the following regular expressions represents a string that starts with a and ends with b? a) a.*b b) a+b c) ab* d) a?b Answer: a) a.*b Which of the following regular expressions matches any string containing at least one a? a) a+ b) .*a.* c) a* d) a? Answer: b) .*a.* Which regular expression matches exactly four occurrences of ab? a) (ab){4} b) ab{4} c) a{4}b{4} d) ab* Answer: a) (ab){4} What is the role of a finite automaton’s transition function? a) It defines the state transitions based on input symbols b) It specifies the initial state c) It lists all final states d) It initializes the automaton Answer: a) It defines the state transitions based on input symbols Which of the following regular expressions represents a string with exactly two as separated by any number of bs? a) ab*b b) a(b*)a c) a+b d) a.*a Answer: b) a(b*)a What is the purpose of a regular expression engine? a) To match regular expressions against strings b) To generate machine code c) To perform semantic analysis d) To optimize code Answer: a) To match regular expressions against strings Which of the following regular expressions matches a string that contains exactly three characters, where each character is either a or b? a) [ab]{3} b) a{3}|b{3} c) a+b+ d) [ab]*3 Answer: a) [ab]{3} In a finite automaton, what is the result of processing an input string that leads to a state not marked as final? a) The string is rejected b) The string is accepted c) The automaton enters an error state d) The automaton halts without result Answer: a) The string is rejected What does the notation a|b in a regular expression denote? a) Alternation (either a or b) b) Concatenation of a and b c) Zero or more occurrences of a followed by b d) One or more occurrences of a and b Answer: a) Alternation (either a or b) Which regular expression matches a string that contains at least one a, followed by zero or more bs? a) a+b* b) a*b+ c) a(b*) d) a(b|c)* Answer: a) a+b* What is the primary use of finite automata in the context of text processing? a) Pattern matching b) Code generation c) Data encryption d) Memory management Answer: a) Pattern matching Which of the following regular expressions matches a string with exactly two bs, where the bs are not necessarily adjacent? a) b.*b b) b{2} c) b+b+ d) b?b? Answer: a) b.*b What is an ε-transition in an NFA? a) A transition that does not consume any input symbols b) A transition that consumes multiple input symbols c) A transition that occurs only at the end of the string d) A transition that is not allowed in NFAs Answer: a) A transition that does not consume any input symbols Which regular expression denotes a string that starts with a and ends with b, with any number of characters in between? a) a.*b b) a+b+ c) a?b? d) a.b Answer: a) a.*b In the context of regular expressions, what does the . symbol represent? a) Any single character except a newline b) A literal period character c) Zero or more occurrences of any character d) A specific sequence of characters Answer: a) Any single character except a newline What does the regular expression \d match? a) Any digit b) Any letter c) Any non-digit character d) Any whitespace character Answer: a) Any digit Which of the following regular expressions matches a string of exactly five characters where each character is a or b? a) [ab]{5} b) a{5}|b{5} c) a|b{5} d) (a|b){5} Answer: d) (a|b){5} What does the regular expression [0-9]{4,6} match? a) A string of 4 to 6 digits b) A string of exactly 4 to 6 characters c) Any string with 4 to 6 alphanumeric characters d) A string with 4 or 6 digits Answer: a) A string of 4 to 6 digits What is a “character class” in regular expressions? a) A set of characters enclosed in square brackets b) A class of regular expressions c) A predefined set of symbols d) A character that matches any symbol Answer: a) A set of characters enclosed in square brackets What does the regular expression [^a-z] match? a) Any character except lowercase letters b) Any lowercase letter c) Any digit d) Any non-alphanumeric character Answer: a) Any character except lowercase letters Which regular expression matches a string containing exactly three occurrences of ab in any order? a) (ab){3} b) a(b.*b){3} c) abab|abab|baba d) a(b{2}a){3} Answer: a) (ab){3} In the context of finite automata, what does it mean for an automaton to be “deterministic”? a) For every state and input symbol, there is exactly one possible next state b) For every state, there can be multiple possible next states c) The automaton can handle multiple input symbols simultaneously d) The automaton can transition to any state from any state Answer: a) For every state and input symbol, there is exactly one possible next state What does the regular expression a{2,4} match? a) A string with 2 to 4 consecutive as b) A string with exactly 2 or 4 as c) A string with up to 4 as d) A string with 2 to 4 as separated by other characters Answer: a) A string with 2 to 4 consecutive as Which of the following regular expressions matches a string with an even number of as? a) (aa)* b) a{2,} c) (a|b)* d) a{2} Answer: a) (aa)* Which of the following symbols denotes a “non-greedy” quantifier in regular expressions? a) *? b) +? c) ?? d) | Answer: a) *? Which regular expression matches a string of zero or more as followed by exactly one b? a) a*b b) a+b c) a?b d) ab* Answer: a) a*b What does the regular expression \s match? a) Any whitespace character b) Any non-whitespace character c) Any digit d) Any letter Answer: a) Any whitespace character Which of the following regular expressions matches a string with exactly one a followed by zero or more bs and ending with c? a) a*b*c b) a+bc c) a*b+ d) ab*|c Answer: a) a*b*c In a finite automaton, what is a “transition function”? a) A function that maps a state and input symbol to the next state b) A function that determines the acceptance of a string c) A function that initializes the automaton d) A function that generates machine code Answer: a) A function that maps a state and input symbol to the next state
All Copyrights Reserved 2025 Reserved by T4Tutorials