nerdexam
Python_Institute

PCEP-30-02 · Question #139

You have the following le. index.py: from sys import argv\nprint(argv[1] + argv[2]) You run the le by executing the following command in the terminal. python index.py 42 3 What is the expected output?

The correct answer is C. 423. sys.argv stores command-line arguments as strings, not numbers, so argv[1] + argv[2] performs string concatenation - joining "42" and "3" into "423", making C correct. Why the distractors are wrong: A (45) assumes Python converts the arguments to integers automatically - it…

Question

You have the following le. index.py: from sys import argv\nprint(argv[1] + argv[2]) You run the le by executing the following command in the terminal. python index.py 42 3 What is the expected output?

Options

  • A45
  • B4242
  • C423
  • D126

How the community answered

(27 responses)
  • A
    11% (3)
  • B
    4% (1)
  • C
    81% (22)
  • D
    4% (1)

Explanation

sys.argv stores command-line arguments as strings, not numbers, so argv[1] + argv[2] performs string concatenation - joining "42" and "3" into "423", making C correct.

Why the distractors are wrong:

  • A (45) assumes Python converts the arguments to integers automatically - it doesn't; sys.argv is always a list of strings.
  • B (4242) would require repeating argv[1] twice (e.g., argv[1] * 2), not adding two separate arguments.
  • D (126) would result from actual integer multiplication (42 * 3), which would also require explicit type conversion.

Memory tip: Think of argv as a list of text typed at the terminal - to do math, you must wrap values in int() or float() first (e.g., int(argv[1]) + int(argv[2]) gives 45). The + operator on strings always concatenates, never adds.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice