Quiz 2 Study Guide
You are responsible for all material needed for Lab 4. Quiz 2 will not cover any graphics topics.
Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, or with meetings with faculty and staff. We do not provide full solutions to the study guides.
The study guide is not indicative of length, difficulty, or format of the quiz.
In addition to all concepts from Quiz 1, you should understand the following:
Python concepts and functions
You should be able to use the following Python concepts and functions:
-
the
bool
data type representingTrue
andFalse
-
if
,if
/else
,if
/elif
/else
-
the relational operators
<
,<=
,==
,>
,>=
,!=
-
how strings are ordered when compared using relational operators.
-
the logical operators
and
,or
, andnot
-
nested statements (e.g., using an
if
/else
in a loop) -
while
loops -
writing functions including functions with parameters and with return values
Sample practice problems
-
Given these assignments:
x = 5 y = 13 phrase = "computer science" name = "Andrew" b = False
Show the value and type of each expression below:
(x < 10) and (y < 10) (x < 10) or (y < 10) (x < 10) and (x > 0) (x > 10) or (x < 0) (5/x) > 7.0 not b b or (not b) len(phrase) >= 10 phrase[0] < phrase[1] name < phrase
-
The code below asks the user for two input values. Provide 5 different sets of input values for
first
andsecond
such that each branch of the if statement is followed.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def main(): first = int(input("Enter the first number: ")) second = int(input("Enter the second number: ")) if (first < 10): print("Option 1") elif (second < 10): print("Option 2") elif (first < second): print("Option 3") elif (second > 20): print("Option 4") else: print("Option 5") main()
-
Show the output from the following code fragments:
word = 'february' result = '' for letter in word: result = result + letter print(result)
word = 'february' result = '' for i in range(len(word)): result = result + word[i] print(result)
-
Modify each block of code so that it prints
yraurbef
instead offebruary
.
-
-
Write a program that asks the user to enter a word and a letter. Your program will then count how many times that letter appeared in the word. For example:
Enter a word: mississippi Enter a letter: s The letter s appears 4 times in mississippi
-
Write a program that asks the user to enter the hour of the day and whether it is AM or PM. You can assume the user always provides an integer between 1-12 for the hour and that they always type either AM or PM. Based on the time of day, tell them if Narples is open and what meal they are serving. For this question, assume that Narples serves breakfast from 7 AM to 10 AM, lunch from 11 AM to 2 PM, dinner from 4 PM to 9 PM, and is closed every other time. For example:
What is the hour? 8 Is that AM or PM? AM Narples is serving breakfast. What is the hour? 2 Is that AM or PM? PM Narples is serving lunch. What is the hour? 9 Is that AM or PM? PM Narples is serving dinner. What is the hour? 11 Is that AM or PM? AM Narples is serving lunch. What is the hour? 11 Is that AM or PM? PM Narples is closed.
-
Show the output from the following code fragment:
x = 0 while (x < 6): print("%d %d" % (x, x*x)) x = x + 1 print("done!")
-
Show the output from the following code fragment:
text = "strings!" i = 0 found = False while (found == False): if (text[i] == "i"): found = True else: print(text[i]) i = i + 1 if (found == True): print("found at position %d" % (i) )
-
Consider the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13
def find_min(a, b): min_value = a if (b < a): min_value = b return min_value def main(): number_one = int(input("Enter the first number: ")) number_two = int(input("Enter the second number: ")) smaller = find_min(number_one, number_two) print("The smaller value is %d" % (smaller)) main()
-
What are the function definitions?
-
What are the function calls for functions defined within this program?
-
Which functions are called that aren’t defined within this program?
-
Give an example of a parameter and give an example of an argument.
-
What would this program print if the user entered the following two numbers:
-
4 and 5
-
5 and 5
-
9 and 2
-
-
-
Write a program that reads in student grades from the user, until the user enters a -1. After reading in the -1, the program should compute and display the average grade.
Please enter your grades below. Enter a -1 when you are all done grade 1: 98 grade 2: 87 grade 3: 65 grade 4: 95 grade 5: 80 grade 6: -1 The average of those 5 grades is 85.000
-
Write a function called
get_int
that has two parameters,lo
andhi
. The function should prompt the user to enter an integer betweenlo
andhi
(inclusive), and then return the value entered. Your function should not return until the user enters a valid integer in the rangelo
tohi
. You may assume bothlo
andhi
are positive integers.Now add a
main()
function that callsget_int
to get an integer from 1-10. Yourmain()
should then print out "Yay!" that many times.Here’s an example run of the full program:
Please enter an integer from 1-10: 99 Please enter an integer from 1-10: -40 Please enter an integer from 1-10: 4 Yay! Yay! Yay! Yay!
-
Consider the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
def mystery(word, letter): print("word: %s" % (word)) print("letter: %s" % (letter)) result = "" for ch in word: if (ch != letter): result = result + ch # draw stack here return result def main(): test = "hello" check = "l" answer = mystery(test, check) print("the answer is %s" % (answer)) main()
-
Draw the function stack as it would appear when you reach line 9. [answer]
-
What variables are in scope at line 9?
-
What is the program’s output (i.e. what does it print when the program is run)?
-
What does the
mystery
function do? -
What would
mystery("banana", "a")
return? -
What would
mystery("zoo", "a")
return?
-