comp sci 108 midterm
Select the following that are string literal values
"5", "my_variable"
How do you write each of the following as string literals? The first letter of the alphabet, lower case: A tab character: A new line: A double quote: A single quote: The empty string:
"a" "\t" "\n" '"' "'" ""
Mark each of the following expressions if they evaluate to True. Assume that the following code is executed first: title = "Harry Potter"
"word" in "The last word" " " in title "" in title "Alabama" < "Virginia" "Coward" <= "Coward" "Dog" == "Dog"
Enter the symbol (not its name) used for single-line comments in Python.
#
Evaluate each of the following expressions and identify the type of the result. (1 + 2) * 4 / 3 1 + 2 + 3 + 4 + 5 (1 + 2) > 4 "Hello " + "World" 4 > 5 or 5 == 6
(1 + 2) * 4 / 3 -FLOAT 1 + 2 + 3 + 4 + 5 -INTEGER (1 + 2) > 4 -BOOLEAN "Hello " + "World" -STRING 4 > 5 or 5 == 6 -BOOLEAN
Match each debugging strategy to its description. The Scientific Method Wolf Fencing Rubber Duck Debugging
-Develop a hypothesis that you can use to test predictions about your code -Repeatedly divide you code in half using print statements to track the fault -Meticulously review each line of code out loud
How many possible values are there for each type? Integer Boolean String Float None
Integer - More than 100 Boolean - 2 String - More than 100 Float - More than 100 None - 1
Each type's abreviations
Integer - int None - None Boolean - bool String - str
describe each type
Integer - whole numbers String - text Float - decimal numbers None - A spacial value that means nothing Boolean - True or False Python - This is not a type
Which of the following are advantages of unit tests? a. They help you find problems early. b. They help you facilitate changes down the road. c. They make it easier to glue together pieces of code. d. They make your code easier to read. e. They make your code run faster.
a, b, c
input does which of the following? a. Returns a string b. Writes output to the console c. Retrieves input from the user d. Consumes arguments
a, b, c, d
mark all of the following that ARE advantages of functions. a. make it easy to reuse code b. code in a function executes faster c. easier to debug code in a function because it is isolated d. functions allow us to store values in variables
a, c
Which of the following could be represented with a value? A person's height The text of Moby Dick The number of meals you've eaten this week A text description of someone's emotional response to a poem An audio recording of a song
all of them
You have a variable that represents your allowance, measured in dollars. Which of the following are good variable names? In this case, "good" means that they are not too long, not too short, and the intent of the variable is unambiguous. allowance d allowance_in_dollars data integer a_variable_that_will_hold_my_allowance_money
allowance allowance_in_dollars
What is the value stored in "my_var" after the following code is executed? my_var = [1,2,3] + 4
an error occurs
What is the value stored in "my_var" after the following code is executed? my_var = [1,2,3] < 5
an error occurs
define function call: a. send data to the console b. send data to a function c. send data from a function d. do nothing e. take data from the console
b
print does which of the following? a. Returns a string b. Writes output to the console c. Retrieves input from the user d. Consumes arguments
b, d
What does the following value specifically represent? 24 a. The number of hours in a day b. We have no way of knowing without more information. c. The number of cookies I ate last night d. A popular TV show
b. We have no way of knowing without more information.
what type of thing is returned from the function below? def is_on_fire(): return True
boolean
Why are global variables bad? a. Global variables are not bad. b. They make the program slower. c. They make it harder to think about the program. d. They "leak" memory so that data can escape the program.
c
define return: a. send data to the console b. send data to a function c. send data from a function d. do nothing e. take data from the console
c
Function A calls Function B. Function B calls Function C. In order to pass data from A to C, we need to use the parameters of both A and B. In order to pass data from C back to A, we need to use ____ in both B and C.
returns
Trace the value of the three variables above by filling out the table below. If a value has not been assigned to a variable, write "X" in the box (without quotation marks). salary = 10 taxes = .1 adjusted = salary * taxes salary = 100 taxes = 0 adjusted = salary - taxes
step salary taxes adjusted 1 10 X X 2 10 .1 X 3 10 .1 1.0 4 100 .1 1.0 5 100 0 1.0 6 100 0 100
error type?: green is with the monkey's ears and toes (symantec or syntax?)
symantec
error type?: this Open. Verbed -- ? Outside ... grapes! (symantec or syntax?)
syntax
Multi-line comments in Python are actually just triple-quoted strings (t/f)
t
Values enter functions as arguments when the function is called, and then the arguments' values are assigned to parameters when the function's body begins executing (t/f)
t
every function defined with the "def" keyword MUST have a name (t/f)
t
the keyword "def" is short for "define" (t/f)
t
the scope of a variable is how long it is "alive" (t/f)
t
Which of the following demonstrates how to add something to the following list variable? temperatures = [44, 36, 72]
temperatures.append(42)
Label each of the following as either a "Read", a "Write", or a "Read and Write" for the variable "x". x = 0 print(x) y = x + z x = x + 1 x == 1
x = 0 -WRITE print(x) -READ y = x + z -READ x = x + 1 -WRITE AND READ x == 1 -READ
Match each word to the best definition. Parameter Parentheses Function Argument Documentation Call
-The names given to the values that will be passed into a function -The curved symbols that go after a function name to ensure that it is called -A reusable chunk of code that can be called using its name and parenthesis -The values passed into a function -The extra information about a function supplied to help other programmers understand how to use the function -The act of using a function via its name and parenthesis
Put the following steps into the correct order.
-observe a failure in your code -develop a hypothesis as to why the code failed -turn this hypothesis into a prediction -test the prediction by experimenting -repeat the previous two steps until the code is fixed -Celebrate with cocaine!
Match the following types of errors: NameError TypeError SyntaxError TimeError
-you have referenced a variable that does not exist -you have used an operator with incorrect types -you have a mistake with your code's grammar or punctuation -this is not a common type of error
What will be printed? y = 0 def increase(y): y = y + 1 return y increase(y) increase(y) print(y)
0
what is printed after the code below is executed? def change_value(a_variable): a_variable = 5 return a_variable a = 0 change_value(a) print(a)
0
what is printed after the code is executed? def change_value(a_variable): a_variable = 5 return a_variable a = 0 change_value(a) print(a)
0
Which of the following are Integers, in Python? 0 "Five" "7" 5.5 2.0 -100000257
0 -100000257
How many scopes are there in this program? def print_result(value): print("The result is", value) def massage_number(a_number): return a_number * 5 + 3 start = 10 print_result(massage_number(start))
1 global scope, 2 local scopes
Read the following code, and then fill in the blanks below. 1| def f(x): 2| x = x + 1 3| return x 4| val = 0 5| y = f(val) 6| print(y) What line is executed first? What line is executed second? After line 3 is executed, what will be the next line executed?
1, 4, 6
Predict the result of this expression: 10 / 5 - 1
1.0
The function add5 below returns the number 5 added to the argument. What value will the variable "x" hold after the line is executed? x = add5(10) * add5(2)
105
Predict the result of this expression: 4 + 5 * 2
14
How many branches does this code have? if conditional: print(5) print(2)
2
How many branches does this code have? if conditional: print(5) else: print(2)
2
How many things can be added at once with the "+" operator?
2
How many times is the variable x defined on line 1 used (i.e. either read or written) in this program (including on line 1)? 1 | x = 5 2 | def do_math(x): 3 | return (x ** x + x) 4 | do_math(x)
2
What is the value stored in "my_var" after the following strange code is executed? my_var = [1,2,3][1]
2
How many branches does this code have? if conditional: if second_conditional: print(5) print(1) print(2)
3
Identify whether each of these are True or False. 3 < 5 False 10 == 10.0 10 == "10" 5 == 5.5 7 != 7 3 < "5" not False 1 < 2 and 5 < 7 5 < 4 or 3 == 3 True or (True and False) not True and False
3 < 5 -TRUE False -FALSE 10 == 10.0 -TRUE 10 == "10" -FALSE 5 == 5.5 -FALSE 7 != 7 -FALSE 3 < "5" -ERROR not False -TRUE 1 < 2 and 5 < 7 -TRUE 5 < 4 or 3 == 3 -TRUE True or (True and False) -TRUE not True and False -FALSE
Identify the type of each literal value. 5 False 4.3 "Hello World" "4" "" None 0 "True"
5- INT False -BOOL 4.3 -FLOAT "Hello World" -STR "4" -STR "" -STR None -NONE 0 -INT "True" -STR
Predict the result of this expression: (10+8) % 12
6
If an if statement is nested inside another if statement, how many spaces will the body of the inner if statement have?
8
How many values can each of these operators take? < not and !=
< -2 not -1 and -2 != -2
The function makeCat below returns the string "Cat". What value will the variable x hold after the line is executed? x = makeCat() + makeCat() + makeCat()
CatCatCat
When your program has an error, the first thing you should do is:
Check the error message and consider its meaning.
"Hermione"[4:4]
False
' '
False
-0
False
Does the following expression evaluate to true of false? ""
False
Does the following expression evaluate to true of false? "Hi" and "Low" and ""
False
Does the following expression evaluate to true of false? 0
False
Does the following expression evaluate to true of false? 0.0
False
Every if statement must be followed by either an else or an elif.
False
Given the following code: age = 0 Are the following two expressions equivalent? age == 10 or 0 age == 10 or age == 0
False
Lists can also be correctly referred to as "Arrays" or "Vectors".
False
T/F: A variable is useful because it allows to solve for an unknown value.
False
T/F: Expressions are evaluated from left to right
False
T/F: The "print" instruction can only print literal values.
False
T/F: Variable names are important because computers understand the meaning of names and change their value accordingly.
False
The print function returns a string.
False
True or False: "1" + "1" = "2"
False
True or False: Strings are composed of only letters and symbols.
False
You can nest if statements inside of other if statements, but not functions.
False
What is the subtype of the following literal value? [4.5, 3.2, 7.3]
Float
Which of the following are possible subtypes for a list?
Float String Boolean List
Predict the output of the following code: name = "Hermione" print(name[0])
H
Predict the output of the following code: name = "Hermione" print(name[:2])
He
what will be printed by running print("Hello World!")
Hello World!
What is the type of the following literal value? [4.5, 3.2, 7.3]
List
Identify each of the following as either "List literal" or "String indexing". [] "Hermione"[1] ["Hermione"] [1] ""[1]
List literal String Indexing List literal List literal
Which of the following can be involved in an expression? Literal values Arithmetic operators Other expressions Conditional operators Statements Variables
Literal values Arithmetic operators Other expressions Conditional operators Variables
What order are arithmetic operators evaluated?
Parentheses Exponents Multiplication, Modulo, Division Addition, Subtraction
Identify whether the following is a method call or a function call. list.append() print() function.call() method() "function".method()
Method call function call method call function call method call
What is the value of number_of_users after this program executes? if False: number_of_users = 5 print(number_of_users))
No value, because an error occurs since the value has not been defined
Identify each of the following as a Primitive Type, Composite Type, or Not a Type. None True List Integer
Primitive Not a type Composite Primitive
For each word, choose the best synonym...
Set - WRITE Define -INITIALIZE Change -UPDATE Store -WRITE Use -READ Load -READ Increment -UPDATE Create-INITIALIZE
Given the method call below, identify each component. "Open the pod bay doors, Hal!".replace("Open", "Close") "Open the pod bay doors, Hal!" replace "Open" "Close" (
The calling value Method name Argument Argument Parenthesis
"Harry"[0]
True
1 > 5 or 3
True
144
True
Does the following expression evaluate to true of false? "Daddy"
True
Does the following expression evaluate to true of false? 0.01
True
Functions are reusable chunks of code that have inputs and outputs.
True
Lists and strings are both sequences of data.
True
T/F: Literal values are specific values written in the source code.
True
T/F: Normally, statements are executed from top to bottom.
True
T/F: Print is an instruction to the computer to retrieve input from the user
True
T/F: Programs may execute very quickly in computers, but they always happen one step at a time.
True
T/F: The purpose of a value is to represent data from the real world.
True
T/F: Variables change their value over time according to the instructions in a program.
True
The elif is equivalent to an else with an if statement nested inside it.
True
The input function returns a string.
True
What is the value stored in "my_var" after the following code is executed? my_var = 5 in [1,3,5]
True
Write exactly what will be printed by running the following command: input("Type a number:") (The user will type 5 in the box that appears)
Type a number:
Input/Output: Typing on a keyboard A monitor displaying info a printer making a report a robot moving its arm moving a mouse data being loaded from a file
Typing on a keyboard- IN A monitor displaying info- OUT a printer making a report- OUT a robot moving its arm- OUT moving a mouse- IN data being loaded from a file- IN
What is the result if you ask Python to perform the following computation? "2"+2
You cannot add these together
Choose which of the following are valid Python variable names. Note that they do not have to be good variable names, just valid. current-temperature _54__ pag_counnt asdffdf page count "books" _bad_news _5_99_555_999 4th_property EarthQuake
_54__ pag_counnt asdffdf _bad_news _5_99_555_999 EarthQuake
A unit test is: a. A collection of known inputs and outputs for a function. b. A conditional that determines whether the units (e.g., inches, gallons) are correct. c. A function that returns a Boolean value. d. A function that consumes a function and returns whether the function is correct.
a
After data is sent to the console, how do we get it back later in the program? a. You can't, it's a one-way trip. b. Using the input function c. Using the unprint statement d. Use a return statement
a
What is functional decomposition? a. A programming strategy that breaks up a complex task into multiple function definitions. b. An issue where functions decompose over time, and may fail unit tests periodically. c. A math term that describes how you can break a function up so that you can recombine it into one single function definition later. d. A way to get data out of one function and into another one.
a
define print: a. send data to the console b. send data to a function c. send data from a function d. do nothing e. take data from the console
a
define pass: a. send data to the console b. send data to a function c. send data from a function d. do nothing e. take data from the console
d
definition keyword
def
define input: a. send data to the console b. send data to a function c. send data from a function d. do nothing e. take data from the console
e
Return can be used to return a variable so that variable is available outside a local scope (t/f)
f
The print function is necessary to call a function (t/f)
f
The two functions below share a variable named score: def increase(score): return score+1 def decrease(score): return score-1 (t/f)
f
You can use the return statement to return a variable from a function (t/f)
f
a function can either print or return, but not both (t/f)
f
every function created with the "def" keyword must have AT LEAST ONE parameter (t/f)
f
every function must return an integer or string(t/f)
f
print is used to put data on the console, and return is used to take data from the console. (t/f)
f
the first line of the following code is unnecessary: from cisc108 import assert_equal def days_to_hours(days): return 24 * days assert_equal(days_to_hours(2), 24) (t/f)
f
what is the second value printed after the code below is executed? def double(value): value = value * 2 print(value) value = 1 print(double(value)))
none
what type of thing is returned from the function below? def get_name(): print("Harry Potter")
none
when the following code is executed, what will be the value of the variable named tripled? def triple(a_number): print(a_number * 3) tripled = triple(2)
none
what does the pass statement do?
nothing
Predict the output of the following code: name = "Hermione" print(name[-3:])
one
How would you call a function named openDoor? Assume it takes no arguments.
openDoor()
given the function definition below, what is the name of the function? def get_area(width, height): return width*height
get_area
Predict the output of the following code: name = "Hermione" print(name[4])
i
In the following code, the print statement is ____ the IF statement. if conditional: print("The conditional was true")
inside
Predict the output of the following code: name = "Hermione" print(name[4:7])
ion
Mark all of the following that are valid function or method calls. Do not worry if a variable exists, only determine whether 1) this is a function or method call, and 2) the syntax is valid.
list.pop() build(1, 2, 3) print() "harry potter".title()
Predict the output of the following code: name = "Hermione" print(name[-2])
n
What type of value does this function return? def average_values(num1, num2, num3, num4): print((num1+num2+num3+num4)/4)
none