OA. free
Free
Micron Core Computer Science Core Computer Science Medium

QUESTION 21 What is output of python test.py 5 27?

Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.

QUESTION 21

#test.py
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('nums', nargs=2)
args = parser.parse_args()

print(" Name: {}".format(args.nums))

What is output of python test.py 5 27?

Choose one option.
Show answer & explanation
Answer: B. Name: ['5', '27']

When nargs=2 is specified in argparse, it collects exactly 2 arguments into a list. The command python test.py 5 27 passes two string arguments ('5' and '27'), which are stored in args.nums as a list. The .format() method then converts this list to its string representation, which displays as ['5', '27'] with square brackets.

Step-by-step Derivation:

  1. parser.add_argument('nums', nargs=2) creates a positional argument that accepts exactly 2 values and stores them in a list.
  2. python test.py 5 27 provides the two arguments: '5' and '27'.
  3. args.nums becomes ['5', '27'] (a list of strings, since command-line arguments are strings by default).
  4. print("Name: {}".format(args.nums)) substitutes the list into the string, producing: Name: ['5', '27'].
  5. The list representation includes square brackets [] in the output.