MIS304 Final Exam
direct access file
When you work with a _____ _____ ______ (which is also known as a random access file), you can jump directly to any piece of data in the file without reading the data that comes before it.
string concatenation
using the '+' character b/w two strings is called ______ ________ and it prints the two strings together.
input file
what does the program read data from?
\
what is the line continuation character?
\n
what is the newline escape character?
print("Hello")
what should you code if you want to display the word "Hello"?
float
when a real number (aka a decimal) is stored in memory, it is classified as...
int
when an integer is stored in memory it is classified as ...
float
when an operation is preformed on 2 float values, the result will be...
int
when an operation is preformed on 2 int values, the result will be...
float
when an operation is preformed on an int value and a float value, the int value will be temporarily transformed into a ____ value and the result will also be a _____ value
dance.write("ballet")
write "ballet" to the file 'danceclasses.txt' (its already opened under the 'dance' variable)
age = 25
write an assignment statement setting age to 25
dance.write(class_time)
write the string under the variable 'class_time' to the file 'danceclasses.txt' (assume its already opened under the 'dance' variable)
expressions
you set variables equal to _____________
text file
A _____ ______ contains data that has been encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened and viewed in a text editor such as Notepad
function
A ______ is a group of statements that exist within a program for the purpose of performing a specific task.
flag
A ______ is a variable that signals when some condition exists in the program.
condition-controlled (the while loop)
A ______-________ loop uses a true/false condition to control the number of times that it repeats.
count-controlled (the for loop)
A _______-______ loop repeats a specific number of times.
relational operator
A ________ _______ determines whether a specific relationship exists between two values. for example: greater than, less than, or equal to
binary file
A ________ ________ contains data that has not been converted to text. The data that is stored in a ______ ____ is intended only for a program to read. As a consequence, you cannot view the contents of a ____ _____ with a text editor.
control structure
A ________ __________ is a logical design that controls the order in which a set of statements execute.
sentinel
A ________ is a special value that marks the end of a sequence of items. When a program reads the _____ value, it knows it has reached the end of the sequence, so the loop terminates.
sequence structure
A ___________ ____________ is a set of statements that execute in the order that they appear.
total_sales = input("enter projected amount of total sales: ") profit = .23 * total_sales print(profit)
A company has determined that its annual profit is typically 23 percent of total sales. Write a program that asks the user to enter the projected amount of total sales, and then displays the profit that will be made from that amount. Hint: Use the value 0.23 to represent 23 percent.
price1 = input("Enter the price of the first item: ") price2 = " price3 = " price4 = " price5 = " subtotal = price1 + price2 + price3 + price4 + price5 tax = subtotal * .07 total = subtotal + tax print(subtotal'\n', tax'\n', total)
A customer in a store is purchasing five items. Write a program that asks for the price of each item, and then displays the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 7 percent.
format(number, ',.1f')
Assume the following statement has been executed: number = 1234567.456 Write a Python statement that displays the value referenced by the number variable formatted as '1,234,567.5'
format(value, '.2f')
Assume the variable sales references a float value. Write a statement that displays the value rounded to two decimal points.
a. 12, b. 4, c. 2, d. 6, e. 2
Assume the variables result, w, x, y, and z are all integers, and that w = 5, x = 4, y = 8, and z = 2. What value will be stored in result after each of the following statements execute? a. result = x + y b. result = z * 2 c. result = y / x d. result = y - z e. result = w // z
FALSE
Different functions can't have local variables with the same names because the functions can see each other's local variables. TRUE/FALSE?
random.uniform(range)
How can i get a random value as a decimal in a certain range?
random.random()
How can i get a random value that has a decimal?
random.randint(range)
How do we get a random integer with an inclusive range?
random.randrange(10, 110, 10)
How do we get a random value from 10 to 100 by tens? In other words, a random value from this list: 10, 20, 30, 40... 100
file_variable = open(filename, mode)
How do you open a file (filename) and assign it to the file_variable variable?
FALSE
In a for loop, the ending range number is inclusive. That is, "for num in range(5)" runs the loop 6 times - 0,1,2,3,4,5. TRUE/FALSE?
mixed-type expression
an expression that uses operands of different data types is called...
except ValueError as err: print(err)
What should the except and print statements say if you want the program to display the original message it would have to display if you didn't catch it? use ValueError and attribute it to 'err'
except IOError:
What should the except clause say if it can't read the file or there is some sort of reading error?
except ValueError:
What should the except clause say if the input is the wrong type (like it's 'forty' and it should be '40')?
except:
What should the except clause say if you want to catch any error?
George@John@Paul@Ringo
What will the following statement display? print('George', 'John', 'Paul', 'Ringo', sep='@')
11
What would the following display? a = 5 b = 2 c = 3 result = a + b * c print(result)
5
What would the following display? num = 99 num = 5 print(num)
\n
When you are writing information to a file, what should you add at the end to make it easier to read later?
format(0.5, '.0%')
format '0.5' as a percentage with no decimal places:
value-returning
When you call a _____-______ function, it executes the statements that it contains, and then it returns a value back to the statement that called it.
void
When you call a ______ function, it simply executes the statements it contains and then terminates.
TRUE
numbers must be converted to strings before they can be written into a file. TRUE/FALSE?
dance = open('danceclasses.txt', 'a')
open file 'danceclasses.txt' for editing. The file already exists, do not erase what's already on there! Attribute it to the variable 'dance'
dance.close()
please close the 'danceclasses.txt' file that is currently under the 'dance' variable
infile = open('danceclasses.txt', 'r')
please open the danceclasses.txt file for reading under the variable 'infile'
terminal symbols
represented by the ovals in the flowchart, they mark the starting point and ending point of the flowchart
input/output symbols
represented by the parallelograms in the flowchart, they mark steps in which the program reads input or displays output
processing symbols
represented by the squares in the flowchart, they mark steps in which the program performs some process on the data, like mathematical equations
end = ' '
specifies that the print function should print a space instead of a newline character at the end of its output.
class1 = class1.rstrip('\n')
strip the class1 line from the newline character and set that equal to the 'class1' variable
import os os.rename('danceclasses.txt', 'classes.txt')
the dance studio will begin teaching other classes aside from dance, how do i change the danceclasses.txt file name to just classes.txt so that we can add the new classes to the file?
FALSE
In the try/except block, the 'else' is executed regardless of what happens with the except block. TRUE/FALSE?
total_sqft = input("Enter total number of square feet: ") acres = total_sqft / 43560
One acre of land is equivalent to 43,560 square feet. Write a program that asks the user to enter the total square feet in a tract of land and calculates the number of acres in the tract. Hint: Divide the amount entered by 43,560 to get the number of acres.
or
The ____ operator connects two Boolean expressions into one compound expression. One or both subexpressions must be true for the compound expression to be true. It is only necessary for one of the subexpressions to be true, and it does not matter which.
and
The _____ operator connects two Boolean expressions into one compound expression. Both subexpressions must be true for the compound expression to be true.
not
The _____ operator is a unary operator, meaning it works with only one operand. The operand must be a Boolean expression. The not operator reverses the truth of its operand.
conditionally executed
The action is because it is performed only when a certain condition is true.
priming read
The first input operation—just before the loop—is called a ______ ______, and its purpose is to get the first input value that will be tested by the validation loop. If that value is invalid, the loop will perform subsequent input operations.
TRUE
The variable that is receiving the assignment must appear on the left side of the operator, otherwise a syntax error occurs. TRUE/FALSE?
end = ''
This specifies that the print function should print nothing at the end of its output.
sep = '&'
This specifies that the print function should print the '&' sign instead of a space to separate the arguments in the function. The '&' can be any character or a space.
TRUE
Variable names may not contain symbols such as #,$,%, etc. TRUE/FALSE?
FALSE
Variables names can begin with letters, underscores, and digits. TRUE/FALSE?
output file
What do you write data into?
random.seed(number)
What if i want the same set of random number in every session?
sequential access file
When you work with a ________ _______ _______, you access data from the beginning of the file to the end of the file. If you want to read a piece of data that is stored at the very end of the file, you have to read all of the data that comes before it—you cannot jump directly to the desired data.
color = input("Enter your favorite color: ")
Write Python code that prompts the user to enter his or her favorite color and assigns the user's input to a variable named color.
height = input("Enter your height: ")
Write Python code that prompts the user to enter his or her height and assigns the user's input to a variable named height.
total = 10 +14
Write a Python statement that assigns the sum of 10 and 14 to the variable total.
total = subtotal * 0.15
Write a Python statement that multiplies the variable subtotal by 0.15 and assigns the result to the variable total.
due = total - down_payment
Write a Python statement that subtracts the variable down_payment from the variable total and assigns the result to the variable due
b = 2 + a, a = b * 4, b = a / 3.14, a = b - 8
Write assignment statements that perform the following operations with the variables a, b, and c. a. Adds 2 to a and assigns the result to b b. Multiplies b times 4 and assigns the result to a c. Divides a by 3.14 and assigns the result to b d. Subtracts 8 from b and assigns the result to a
function
a piece of written code that performs an operation. for example: *print*
string
a sequence of characters used as data
triple quotes
can be used to hold both single quotes and double quotes as part of a string and also for multi-line strings
print("Favorite ice cream: ", ice_cream)
code to display the argument "Favorite ice cream:" and the variable ice_cream using one function
\"
escape character causes a double quotation mark to be printed
\'
escape character causes a single quotation mark to be printed
\\
escape character that causes a backlash to be printed
\t
escape character that causes the output to skip over to the next horizontal tab position
decision structure
execute a set of statements only under certain circumstances. This can be accomplished with a _______ __________.
r
file mode that opens the file for reading only
a
file mode that opens the file for writing. If the file doesn't exist, it creates it. If the file does exist, it appends the new stuff to whatever's already written on there
w
file mode that opens the file for writing. If the file doesn't exist, it creates it. If the file does exist, it erases all of its contents and starts over.
format(1.2345, '.2f')
how do I format the number '1.2345' to show only 2 decimal places?
import os os.remove('danceclasses.txt')
how do i delete the danceclasses.txt file?
format(12345.123, ',.2f')
how do i format the number '12345.123' to show only 2 decimal places and use comma separators?
type(number)
how i determine the data type of a value called 'number'?
print(age)
how would you code to display the value of a variable named age?
class1 = infile.readline()
make the program read only the first line of danceclasses.txt file and attribute it to the 'class1' variable
classes = infile.read()
make the program read the entire danceclasses.txt file and attribute it to the 'classes' variable
camelCase
naming convention where the first word's first letter in lowercase but the second word's first letter is upper case