Computer Science 111 Final Exam
The comparison operators are A. ==, <, >, <=, >=, and != B. and, or, and not C. +, -, *, and /
A. ==, <, >, <=, >=, and !=
A finite sequence of computable steps that halts with a solution to a problem is called a(n) ____
Algorithm
Consider the code segment name = "Ken Lambert" To access the letter "t" in name, you would use the expression ____ A. name[len(name)] B. name[len(name) - 1]
B. name[len(name) - 1]
The {while} loop stops iterating when its condition becomes ____
False
The / operator returns a value of type ____
Float
The code segment "3" + "5" evaluates to the value ____
"35" - got wrong on quiz
To convert the string "Computer" to uppercase, you would use the expression ____
"Computer".upper()
The method call "ken lambert".upper() returns the string ____
"KEN LAMBERT"
Consider the code segment name = "Ken Lambert" The expression name[0:3] evaluates to the value ____
"Ken"
To open a file for output, where you intend to overwrite an existing file, you run the open function with the argument
'w'
The expression "%6d" % 43 returns a string containing ____ spaces to the right of the digits in "43"
0
The statement for x in range(1, 5, 2): print(x) outputs the numbers
1 and 3
The type of statement that gives a value to a variable is called an ____
Assignment Statement
To loop through the integers from 1 through 10, you use the loop header A. for x in range(10): B. for x in range(1, 10): C. for x in range(1, 11):
C. for x in range(1,11)
A while loop whose condition never becomes False and which contains no break statements is said to be A. definite B. indefinite C. infinite
C. infinite
Jack is modeling a process that will repeat an unpredictable number of times. The control statement that he should use in his code for this is a(n) ____ A. if/else statement B. for loop C. while loop
C. while loop
The program that translates a Python program into byte code is called the ____
Compiler
A program consists of a sequence of instructions that manipulate ____
Data
The tripple-quoted text that appears at the beginning of a Python program is called a(n) ____
Docstring
Let A be 10 and B be 2. Then, the value of the expression A <= B is ____
False
Let A be True and B be False. Then the value of the expression not A or B is ____
False
Let A be True and B be False. Then, the value of the expression A and B is ____
False
The data type that represents numbers containing a decimal point is ____
Float
The type of statement that makes the code resources of another Python module available to a program is called an ____
Import Statement
The // operator returns a value of type ____
Int
Jack wants to process lines of text from an input file. Should he use the read method to input the text, or a for loop over each line? Justify your answer.
Jack should use a for loop, because it reads each line in the file, rather than the entire text.
The expression "%0.2f" % 3.146 returns the string ____ (remember that strings are enclosed in double or single quotes).
"3.15"
Consider the following code segment: name = "Ken Lambert" substring = "" for ch in name: if ch != " ": substring += ch The value of substring when the loop finishes is ____
"KenLambert" - got wrong on quiz
Consider the code segment name = "Ken Lambert" The expression name[4] evaluates to the value ____
"L"
The expression "Python rules!"[2:4] returns the value ____
"th"
To open a file for input, you would run the open function with the argument
'r'
The character at the first position in a non-empty string is located at the index position ____
0
The expression 5 % 2 produces the value ____
1
The expression int(3.75) produces the value ____
3
Let A be -5. Then the value of A after the statement if A < 0: A = -A is run would be ____
5
To access the letter 'h' in the string "Python", you would use the subscript ____
[3]
A set of keys associated with values.
a dictionary
A mutable sequence of items of any type.
a list
The lifetime of a parameter or a temporary variable is the duration of
a particular activation of a function or method
The Python method read inputs
a single string containing the entire text of a file
An immutable sequence of characters.
a string
A sequence of characters on a secondary storage medium.
a text file
An immutable sequence of items of any type.
a tuple
A list can contain
anything at all, including different types of things at different positions
The method that allows you to add an item to the end of a list, increasing its length by 1.
append
The scope of a temporary variable is the
area of text below where it is introduced within the body of a function definition
The values True and False belong to the data type ____
bool
The statement that exits a loop within that loop's body is called ____
break
Assume that the function choice is not defined in the random module, but that you have imported randint from that module. Define a new Python function, named choice, that expects a nonempty list as an argument and returns an item chosen from a random position in that list. The function's header is def choice(lyst):
def choice(lyst): ...........for items in lyst: ..........................return lyst[random.randint(0,len(lyst)-1)]
The literal structure {"name":"Ken", age:67, "hair color":"gray"} is an instance of a
dictionary
The Python method write
does not automatically output a newline to a file
When at least one of its two operands is of type float, an arithmetic operator produces a value of type ____
float
Assume that inputFile refers to a file opened for input and outputFile refers to a file opened for output. Write a code segment that would copy the contents of the input file to the output file. You should need only two or three lines of code (remembering to close the output file).
for lines in inputFile: outputFile.write(lines) outputfile.close()
An example of a control statement is the ____
for loop
When you attempt to open a file for input and the file does not exist, Python
halts the program by raising a runtime error
Consider the code segment total = 0 number = 1 while number <= 4: ..........total += number number += 1 print(total) This code ____
hangs in an infinite loop
Consider the code segment total = 0 number = 1 while number <= 4: ......total += number number += 1 print(total) This code ____
hangs in an infinite loop
To modify each item in a list, you must use a for loop over the list's
index positions (with a subscript)
The function that converts an input string of digits to an integer is called ____
int
The ____ method glues together the strings in a list to form one string
join
The method that glues together a list of words to form a string is called
join
A mutable data structure.
list
Jill needs to work with a set data values that are ordered by position. She does not know ahead of time how many values will be input. The appropriate data structure for organizing these values is a
list
The ____ module includes resources for performing numerical computations.
math
qualifiers = ["Why do you say that", "Please explain that"] def reply(sentence): return random.choice(qualifiers) + changePerson(sentence) In this code, qualifiers is the name of a
module variable
Unlike strings, lists are
mutable data structures
Set a variable named numbers to be a list of the numbers 1 through 100, inclusive. Hint: you may use the list and range functions.
numbers = list(range(1, 101))
Jill Is working on a program that outputs line numbers followed by the lines of text they number. She is using the variable lineNumber for the number and line for the text. She wants to output the numbered line to the terminal, with the line number right-justified in 4 columns and with two spaces between it and the line of text. The line of text does not contain a newline. Write a line of code that would print these data in this format to the terminal. (Hint: you use a format string.) format = "%4d " % lineNumber + line
print("%4d %s" % (lineNumber, line))
When you use the subscript operator [] with a key that is not present in a dictionary, Python responds by
raising a KeyError
A for loop through a dictionary accesses its keys in
random order
The method that reads a line of text from a file.
readline
In computer science, an abstraction is something that
simplifies a complex thing so we can deal with more easily
The method that returns a list of the words in a string is called
split
def getSum(low, high): .........total = 0 ..........for number in range(low, high + 1): ........................total += number return total In this code, total is the name of a
temporary variable
An immutable data structure.
string
The operator that allows you to access or modify an item at a given index position in a list.
subscript
Consider the code segment total = 0 number = 1 while number <= 4: ...........total += number ............number += 1 print(total) The output of this code is ____
10
Consider the code segment total = 0 number = 1 while number <= 4: .......total += number .......number += 1 print(total) The output of this code is ____
10
The expression 2 * 3 ** 2 produces the value ____
18
The expression 2 times 3 ** 2 produces the value ____
18
The expression 2* 3 ** 2 produces the value ____
18
The expression 5 / 2 produces the value ____
2.5
Consider the following code segment: number = 8 count = 0 while number > 0: ............count += 1 ............number = number // 2 print(count) The output of this code segment is ____
4
After the following code segment is run, the value of total is total = 0 for n in range(4): total += n
6
The function sum computes and returns the sum of a sequence of numbers, including a sequence returned by the range function. The value of the expression sum(range(4)) is ____
6
The Python operator that tests two values for equality is ____
==
When you attempt to open an input file that does not exist, what happens?
Python raises an exception and halts the program (red ink).
The ____ is a program that runs the byte code of a Python program.
Python virtual machine
Quiz 1
Quiz 1
Quiz 3
Quiz 3
The input function returns a(n) ____
String
What is the function os.path.exists used for?
This function is used to determine whether or not a file exists in the file system.
A while loop will continue execution as long as its condition is ____
True
If N is an even number, then the expression N % 2 == 0 evaluates to the value ____
True
In the code segment if b1: s1 else: s2 the statement s1 is run only if the value of the Boolean expression b1 is ____
True
Let A be True and B be False. Then, the value of the expression A or B is ____
True
The expression 'A' < 'a' returns the value ____
True
The value output by the code segment x = 10 print(x >= 0 and x <= 10) is
True