IAE Academy

GATE 2020 CS Q.32: solution and answer

GATE 2020 · CSQ.32 · 2 marks · MCQ

Consider the following languages.


Which one of the following is TRUE?

  1. (A)

    is regular and is context-free.

  2. (B)

    is context-free but not regular and is context-free.

  3. (C)

    Neither nor is context-free.

  4. (D)

    is context-free but is not context-free.

See the question in the paper

Answer and solution

ANSWER

Option (A): is regular and is context-free.

Official answer: ASource: Official answer key, GATE 2020 CS (IIT Delhi)

  1. is regular. Every string of can be rewritten with a one-symbol . Write a member as with and let . Then the string is . The first and third blocks are nonempty, so it has the form with nonempty. Conversely, a string of that form is in by taking . So .

  2. A string of length is in exactly when its last symbol also occurs at some position with . Position leaves room for a nonempty , and leaves room for a nonempty before the final .

  3. Hence . This is a regular expression, so is regular, and therefore also context-free.

  4. is context-free. A string with is in exactly when for some position , so has even length . Take the grammar , , , . Here generates exactly the odd-length strings over whose middle symbol is , and those whose middle symbol is .

  5. Soundness: let produce a string of length and one of length . Then has length with . The is at position and the is at position . These are exactly apart, so the two halves differ at position . The rule is the same with the roles of and swapped.

  6. Completeness: if has length with and , split as followed by . The first part has odd length and middle symbol , so derives it. The second part has length and its middle symbol is at position , which is , so derives it. If instead and , the same split works with . So the grammar generates exactly , and is context-free.

  7. Therefore is regular and is context-free, which is option (A). Options (B) and (D) are false because is regular (not 'not regular'), and (D) also because is context-free. Option (C) is false because both languages are context-free.

KEY IDEA

The word with long collapses to with a single symbol , so is regular. The set of even-length strings whose halves differ is context-free: guess one mismatch pair, which sits exactly half the length apart, using two odd-length palindrome-style blocks with centres and .

WATCH OUT FOR

Treating as a 'copy language' that needs memory of and so is not regular, or thinking (with ) is not context-free because is not. The complement of is context-free because it only needs to remember one mismatch position.

From the GATE 2020 CS paper. Every question of this paper · Download the full paper · All 8 Context-free languages questions, with notes

More Context-free languages questions