nerdexam
Python_Institute

PCEP-30-02 · Question #142

What is the expected output of the following code? x = ' '\nprint(len(x))

The correct answer is A. The code is erroneous. Option A is correct because the \n in x = ' '\nprint(len(x)) appears as literal characters (backslash + n) outside the string, not as a real newline that separates two statements. Python cannot parse \n as a valid statement separator on a single line - only a semicolon (;) or…

Question

What is the expected output of the following code? x = ' '\nprint(len(x))

Options

  • AThe code is erroneous.
  • B2
  • C3
  • D1

How the community answered

(48 responses)
  • A
    73% (35)
  • B
    15% (7)
  • C
    8% (4)
  • D
    4% (2)

Explanation

Option A is correct because the \n in x = ' '\nprint(len(x)) appears as literal characters (backslash + n) outside the string, not as a real newline that separates two statements. Python cannot parse \n as a valid statement separator on a single line - only a semicolon (;) or an actual newline in the source file can do that - so the interpreter raises a SyntaxError before anything executes.

Why the distractors are wrong:

  • D (1) is tempting because ' ' is a single space and len(' ') does equal 1 - but the code never runs successfully enough to produce that output.
  • B (2) would be correct if the string were ' \n' (space + newline inside the quotes, two characters), but the \n here is outside the string.
  • C (3) has no logical basis under any interpretation of this code.

Memory tip: When you see \n inside quotes it's a newline character (counts as 1 in len); when you see it outside quotes in source code, it's not a legal statement separator - only a real line break or ; can split statements.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice