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