Python for Everyone Final Unit 4-7
What is returned when the readline method reaches the end of the file?
""
What is the last output line of the code snippet given below? for i in range(3) : for j in range(5) : if i % 2 == j % 2 : print("*", end="") else : print(" ", end="") print()
***
How many copies of the letter B are printed by the following loop? i = 0 while i == 5 : print("B") i = i + 1
0
If a Byte consists of 8 Bits, what is the min and max values of one Byte?
0 - 255
What is the output of the code below? num = 1 for val in range(0, 4) : sum = val for x in range(0, val, num) : sum = sum + x print(sum , end = " ")
0 1 3 6
Given the list below, what are the upper and lower bounds? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
0, 25
How many times does the while loop execute? s = "abcdEfghI" found = False count = 0 while found == False : if s[count].isupper() : print(letter) found = True count = count + 1
5
A stub function is
A function that acts as a placeholder and returns a simple value so another function can be tested
The tool that allows you to follow the execution of a program and helps you locate errors is known as a(n):
Debugger
What term is used to describe a loop where the number of times that the loop will execute is known before the body of the loop executes for the first time?
Definite
How many return statements can be included in a function?
Exactly one
Parameter variables should not be changed within the body of a function because
It is confusing because it mixes the concept of a parameter with that of a variable
What is the last line of output produced by the code snippet below? i = 0 total = 0 while total < 0 : i = i + 1 total = total - i print(i, total)
No output
The term Black Box is used with functions because
Only the specification matters; the implementation is not important.
Which statement corrects the off-by-one error in the following code: # This code prints the first 10 numbers starting with zero i = 0 while i <= 10 : print(i) i = i + 1
Replace while i <= 10 with while i < 10
The following function is supposed to add 1 to every element in a list of integers. def addOne(values) : for i in range(len(values)) : values[i] = values[i] + 1 What is wrong with the following function?
The statement return values must be added to the end of the function.
What is output by the following code segment? values = ["Q", "W", "E", "R", "T", "Y"] print(values[2 : 4])
["E", "R"]
What list is stored in x after this code segment has run? x = [] for i in range(3) : x.append([]) for j in range(i) : x[j].append(0)
[[0, 0], [0], []]
The readline method reads text until an end of line symbol is encountered, how is an end of line character represented?
\n
When reading a file in Python, you must specify two items:
a file name and mode
Consider a function named avg, which accepts four numbers and returns their average. Which of the following is a correct call to the function avg?
average = avg(2, 3.14, 4, 5)
The location where the debugger stops executing your program so that you can inspect the values of variables is called a(n):
breakpoint
Which statement creates a new, empty list?
data = []
Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline() words = line.split() print(words[len(words)]) _____________________ print("Error.")
except IndexError :
Consider a program that wants to read a file from the absolute path c:\users\user1\states.dat. What statement allows you to read this file?
file = open("c:\\users\\user1\\states.dat", "r")
What are the values of i and j after the following code fragment runs? i = 60 j = 50 count = 0 while count < 5 : i = i + i i = i + 1 j = j - 1 j = j - j count = count + 1 print("i =", i, ", j =", j)
i = 1951, j = 0
The following program opens test.txt and displays its contents. If nothing is placed in the blank, then the contents of the file is displayed double spaced. What should be placed on the blank so that the contents of the file is displayed without the extra blank lines? infile = open("test.txt", "r") line = infile.readline() while line != "" : ____________________ print(line) line = infile.readline() infile.close()
line = line.rstrip()
What type of search inspects every element sequentially?
linear search
One advantage of designing functions as black boxes is that
many programmers can work on the same project without knowing the internal implementation details of functions.
Which statement gets the length of the list values?
numValues = len(values)
Before accessing a file, the program must:
open the file
Which statement causes the following function to exit immediately? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2)
return result
What library is used to read and write sound files?
scipy.io.wavfile
What term is used to describe the portion of a program in which a variable can be accessed?
scope
What term is used to refer to a collection of functions and classes organized into one or more user-defined modules?
software toolkit
The purpose of a function that does not return a value is
to package a repeated task as a function even though the task does not yield a value
What is the sentinel value in the following code segment? value = 15 x = int(input("Enter an integer: ")) while x != 0 : value = value * 2 print(value + 3) x = int(input("Enter an integer: "))
0
What RGB values represent green?
0, 255, 0
How many times does the code snippet below display "Hello"? i = 0 while i != 15 : print("Hello") i = i + 1
15
How many times does the following loop run? i = 0 j = 1 while j >= 1 : print(i , ";" , j) i = i + 1 if i % 3 == 0 : j = j - 1
3
Given the following list, what value is at index 5? values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6
How can you read from a file starting at a designated position in it?
Move the file marker prior to a read or write operation.
Consider the following code segment: print("W", end="") try : inFile = open("test.txt", "r") line = inFile.readline() value = int(line) print("X", end="") except IOError : print("Y", end="") except ValueError : print("Z", end="") What output is generated when this program runs if test.txt is not opened successfully?
WY
For a program that reads three letter grades and calculates an average of those grades, which of the following would be a good design based on stepwise refinement?
Write one function that reads a letter grade and returns the number equivalent, and one function that computes the average of three numbers.
In the code snippet below, if the file contains the following words: apple, pear, and banana stored one per line, what would be the output? infile = open("input.txt", "r") for word in infile : word = word.rstrip() print(word)
apple pear banana
What is supplied to a function when it is called?
arguments
Given a text file quote.txt that contains this sentence: Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog. ~Doug Larson What is the result of this code snippet: inputFile = open("quote.txt", "r") char = inputFile.read(1) while char != "" : print(char) char = inputFile.read(1) inputFile.close()
each character of the quote is printed on a separate line
Which of the following for loops is illegal?
for i in range( , ) :
Which of the following loops executes exactly 10 times?
for i in range(1, 11) : i = 1
Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers: 1 4 9 16 25 36 49 64 81 100
for i in range(10) : values[i] = (i + 1) * (i + 1)
Consider the following code segment: for i in range(4) : ____________________ print("*", end="") print() It is supposed to generate the following output: *** *** *** *** Which line of code should be placed in the blank to achieve this goal?
for j in range(3) :
A ___________________________ is a sequence of instructions with a name.
function
Assume that outfile is a file object that has been opened for writing. Which of the following code segments stores Hello World in the file?
outfile.write("Hello\nWorld\n")
Suppose an input file contains a grocery list. Each line contains the item name followed by its cost. The item name and cost are separated by a comma. Which statements extract this information correctly?
record = inputFile.readline() data = record.split(",") groceryItem = data[0].rstrip() cost = float(data[1])
Assume that a line has just been read from a file and stored in a variable named line. The line contains several words, each of which is separated by one or more spaces. Which of the following statements will store a list of all the words in wordList?
wordList = line.split()