nerdexam
Python_Institute

PCEP-30-02 · Question #260

If you want to build a string that reads: Peter's sister's name's "Anna" Which of the following literals would you use? (Choose two.)

Looking at the target string - Peter's sister's name's "Anna" - the challenge is that it contains both apostrophes/single quotes and double quotes, so the delimiter choice determines what needs escaping. A is correct ('Peter\'s sister\'s name\'s "Anna"'): It uses single quotes…

Question

If you want to build a string that reads: Peter's sister's name's "Anna" Which of the following literals would you use? (Choose two.)

Options

  • A'Peter's sister's name's "Anna"'
  • B"Peter's\ sister's\ name's\ "Anna\""
  • C"Peter's sister's name's "Anna""
  • D"Peter's sister's name's "Anna""

Explanation

Looking at the target string - Peter's sister's name's "Anna" - the challenge is that it contains both apostrophes/single quotes and double quotes, so the delimiter choice determines what needs escaping.

A is correct ('Peter\'s sister\'s name\'s "Anna"'): It uses single quotes as the string delimiter. Because the delimiter is ', the apostrophes in Peter's, sister's, and name's must be escaped as \'. Double quotes don't need escaping inside a single-quoted string, so "Anna" is written as-is. This produces the exact target string.

C is correct ("Peter's sister's name's \"Anna\""): It uses double quotes as the delimiter. Inside a double-quoted string, apostrophes are plain characters and need no escaping. The double quotes around Anna must be escaped as \". This also produces the exact target string.

B is wrong because \\ produces a literal backslash character in the output (e.g., Peter\\ sister\\...), and the unescaped " before Anna would prematurely terminate the string in most languages, causing a syntax error.

D appears identical to C here - if D differs subtly in the actual exam (e.g., using \' unnecessarily or misquoting), it would be a distractor based on plausible-but-wrong escape combinations.

Memory tip: Match your escaping to your delimiter - single-quoted strings escape apostrophes, double-quoted strings escape double quotes. Whatever wraps the outside must be escaped on the inside; the other quote character rides free.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice