{ "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": { "module": "The line import sys loads the sys Python module.", "argv": "The command-line arguments to the script are stored in sys.argv.", "first": "Due to the [1:], the script only prints out the first argument.", "print": "It is necessary to include the parentheses in the print function.", "arg": "The arg variable needs a $ in front of it just like in shell.", "shebang": "The shebang is not necessary if we want to run the script with ./echo.py." } }, "q2": { "type": "multiple", "question": "\nGiven 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(f'{key}={value}')\n\n\n
Which of the following statements are true (select all that apply)?
\n", "responses": { "dict": "The os.environ is a dictionary-like object.", "list": "The os.environ is a list-like object.", "svars": "This script prints all the variables inside the script.", "evars": "This script prints all environmental variables from the shell.", "format": "Instead of print(f'{key}={value}'), we could do print('{}={}'.format(key, value)).", "concat": "Instead of print(f'{key}={value}'), we could do print(key + '=' + value)." } }, "q3": { "type": "multiple", "question": "\nGiven the following Python script, pokemon.py:
\n\n\n#!/usr/bin/env python3\n\nPokemon = ['pikachu', 'charmander', 'bulbasaur', 'squirtle']\n\nfor pokemon in Pokemon:\n if pokemon.endswith('r'):\n print(pokemon.upper())\n else:\n print(pokemon.title())\n\n\n
Which of the following statements are true (select all that apply)?
\n", "responses": { "add": "To add 'meowth' to the list of Pokemon, we could do Pokemon.add('meowth').", "append": "To add 'meowth' to the list of Pokemon, we could do Pokemon.append('meowth').", "last_item": "To access the last item in the Pokemon list, we could do Pokemon[-1]", "endswith": "The pokemon.endswith('r') checks if the string contains the letter r.", "upper": "The pokemon.upper() permanently converts the current pokemon string into upper-case.", "output": "The output of this script is Pikachu CHARMANDER BULBASAUR Squirtle (where each word is on a separate line)." } }, "q4": { "type": "multiple", "question": "\nWhich of the following statements about lists and\ndictionaries are true (select all that apply)?
\n", "responses": { "index": "Like a list, a dictionary's key must be an integer.", "values": "Like a list, iterating through a dictionary provides the values in the data structure.", "length": "Like a list, the len() function can be used to get the number of elements in a dictionary.", "update": "Unlike a list, a dictionary cannot be updated after it has been declared.", "slice": "Unlike a list, a dictionary cannot be sliced.", "contains": "Unlike a list, a dictionary cannot be searched with the in operator." } }, "q5": { "type": "blank", "question": "\nGiven the following Python script, ruthless.py:
\n\n\n#!/usr/bin/env python3\n\nlyric = \"I finally know now what I should have known then That I could still be ruthless if you'll let me\"\ncounts = {}\n\nfor letter in lyric.lower():\n counts[letter] = counts.get(letter, 0) + 1\n\n\n
After executing the code in the ruthless.py script, what are\nthe following values?
\n\nGiven the following Python script, words.py:\n\n
\n#!/usr/bin/env python3\nwords = 'I made a life out of chasin a ghost'\n for letter in word:\n result.append(word)\n keep = True\nprint(' '.join(result))\nfor word in words.split():\n if letter in 'life':\n if keep:\n keep = False\nresult = []\n\n\n
The code is suppose to only keep sub-strings in the words\nstring that contain any of the letters in life. For instance,\nexecuting the script above should print out: \"made life of chasin\".\nUnfortunately, the lines of code are scrambled. Unscramble them below so\nthat the script produces the correct output.
\n", "responses": { "A": "words = 'I made a life out of chasin a ghost'", "B": " for letter in word:", "C": " result.append(word)", "D": " keep = True", "E": "print(' '.join(result))", "F": "for word in words.split():", "G": " if letter in 'life':", "H": " if keep:", "I": " keep = False", "J": "result = []" } } }