nerdexam
Python_Institute

PCEP-30-02 · Question #111

What is the output of the following code? `` 1 x = "2" 2 y = 2 * x 3 print(y) ``

The correct answer is B. 22. In Python, multiplying a string by an integer repeats that string - so 2 "2" produces "22", a two-character string, not the number 4. Option A (2x) is wrong because Python doesn't perform symbolic math; it has no concept of algebraic expressions. Option C is wrong because this…

Question

What is the output of the following code?
1 x = "2"
2 y = 2 * x
3 print(y)

Options

  • A2x
  • B22
  • CThe program will cause an error.
  • D4

How the community answered

(15 responses)
  • A
    13% (2)
  • B
    73% (11)
  • C
    7% (1)
  • D
    7% (1)

Explanation

In Python, multiplying a string by an integer repeats that string - so 2 * "2" produces "22", a two-character string, not the number 4. Option A (2x) is wrong because Python doesn't perform symbolic math; it has no concept of algebraic expressions. Option C is wrong because this is valid Python - string multiplication is a supported operation. Option D (4) is the most tempting trap: it assumes Python treats "2" as a number and multiplies arithmetically, but the quotes make it a string, not an integer.

Memory tip: Think of string multiplication as copy-paste - 2 * "2" pastes the string "2" twice, giving "22". If you wanted the number 4, you'd need 2 * int("2") to convert first.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice