{ "q1": { "type": "multiple", "question": "\n

Given the following Python script, echo.py:

\n\n
\n#!/usr/bin/env python3\n\nimport sys\n\nfor arg in sys.argv[1:]:\n    print(arg)\n
\n\n

Which of the following statements are true (select all that apply)?

\n", "responses": { "print": "It is not necessary to include the parentheses in the print function.", "first": "Due to the [1:], the script only prints out the first argument.", "module": "The line import sys loads the sys Python module.", "argv": "The command-line arguments to the script are stored in sys.argv.", "arg": "The arg variable needs a $ in front of it just like in shell." } }, "q3": { "type": "multiple", "question": "\n

Given the following Python script, cat.py:

\n\n
\n#!/usr/bin/env python3\n\nimport os\nimport sys\n\n# Global Variables\n\nENDING = ''\n\n# Usage function\n\ndef usage(status=0):\n    print('''Usage: {} files...\n\n    -E  display $ at end of each line'''.format(os.path.basename(sys.argv[0])))\n    sys.exit(status)\n\n# Parse command line options\n\nargs = sys.argv[1:]\nwhile len(args) and args[0].startswith('-') and len(args[0]) > 1:\n    arg = args.pop(0)\n    if arg == '-E':\n        ENDING = '$'\n    elif arg == '-h':\n        usage(0)\n    else:\n        usage(1)\n\nif len(args) == 0:\n    args.append('-')\n\n# Main execution\n\nfor path in args:\n    if path == '-':\n        stream = sys.stdin\n    else:\n        stream = open(path)\n\n    for line in stream:\n        line = line.rstrip()\n        print(line + ENDING)\n\n    stream.close()\n
\n\n

Which of the following statements are true (select all that apply)?

\n", "responses": { "status": "If the usage function is called without an argument, that would result in an error.", "startswith": "The args[0].startswith('-') checks if the first argument begins with the character -.", "basename": "The os.path.basename function returns the directory of the specified path.", "stdin": "If no argument is passed to the script, then sys.stdin will be read.", "len": "The len function returns the number of items in the args list.", "rstrip": "The line.rstrip() is unnecessary for correct output.", "options": "The script parses command-line options by treating the arguments as a queue.", "append": "To add an item to the end of a list, we use the append method." } }, "q2": { "type": "multiple", "question": "\n

Given the following Python script, env.py:

\n\n
\n#!/usr/bin/env python3\n\nimport os\n\nfor key, value in sorted(os.environ.items()):\n    print('{}={}'.format(key, value))\n
\n\n

Which of the following statements are true (select all that apply)?

\n", "responses": { "evars": "This script prints all environmental variables from the shell.", "nofmt": "Instead of print('{}={}'.format(key, value)), we could do print(key + '=' + value).", "dict": "The os.environ is a dictionary object.", "svars": "This script prints all the variables inside the script.", "list": "The os.environ is a list object.", "oldfmt": "Instead of print('{}={}'.format(key, value)), we could do print('%s=%s' % (key, value))." } } }