Q158: Regular expression for city name – Part 2

Question #157: Which of the following text represent the regular expression [A-Z][a-z]* ([ ][A-Z] [a-z]*)* [A-Z][A-Z]?

Options:

  1. Bangalore KR
  2. Palo Alto CA
  3. New Delhi ND
  4. All of the above

Solution:

This regular expression is a more generic form of the earlier regular expression (Question 37) in the sense that the first city name can have multiple words starting from capital letter and then followed by a word of two capital letters. The correct answer is option 4.

Q147: Regular expression of string having length at least 3 and third symbol as 0

Question 147: What is the regular expression of the language {w| w has length at least 3 and its third symbol is a 0}?

  1. ΣΣ0Σ
  2. ΣΣ0Σ*
  3. Σ* Σ* 0 Σ
  4. None of the above

Solution: The correct answer is option 2. The regular expression in option 1 will have exactly 4 length strings. Option 3 might have no symbols before 0 because * denotes 0 or more occurrences. Option 2 has exactly two symbols from its alphabet and third symbol is always 0, followed by any number of any symbol from alphabet.

Q37: Regular expression for city name

Question #37: Which of the following text represent the regular expression

[A-Z][a-z]*[ ][A-Z][A-Z]?

 

Options:

A)     Bangalore KR

B)      Palo Alto CA

C)      New Delhi

D)     NDLS

 

Solution:

The above mentioned regular expression says that first word should start with a capital letter, then it is followed by a space and then the last word only has two capital letters. Only A option satisfies these constraints. This regular expression is usually used to represent city name followed by state name. Hence, the correct answer is A.