python test
TRANSCRIPT
-
7/29/2019 Python Test
1/12
Test 2 Name_______________________
100 points possible
Multiple Choice 2 points each pick the BEST answer for each:
____1. The raw_input command takes the following types of data from the keyboard:
(a) string
(b) integer
(c) float
(d) all of these
____2. The addition of the character "\n" to a string puts the following into the output:
(a) the letter n
(b) a blank line
(c) a new line
(d) a back slash character
____3. In Python a while loop must have several lines of code.
(a) True
(b) False
____4. In Python the two statements below both produce the same result.
print "Hello"
print "Hello"
(a) True
(b) False
____5. The Python command: str = raw_input("Type a string: " ).rstrip( "\n" ) would put
what into str?:
(a) whatever was typed plus a new line at the end
(b) whatever was typed minus a new line at the end(c) whatever was typed at the keyboard
(d) whatever was typed minus a new line at the beginning
(e) whatever was typed plus a new line at the beginning
____6. In Python, it is possible to write a loop that never ends.
1
-
7/29/2019 Python Test
2/12
(a) True
(b) False
2
-
7/29/2019 Python Test
3/12
____7. All of the following are types of arguments in Python EXCEPT:
(a) default arguments
(b) variable-length arguments
(c) optional arguments(d) keyword arguments
(e) required arguments
Short Answer 3 points each
1. Give one reason why using the input statement in Python is unsafe compared to using
raw_input.
2. Briefly describe the main difference between the rstrip function and the strip function.
3. Give the output of the following:
name = raw_input("Enter your name: ").strip()
email = raw_input("Enter your email: ").strip()
print "Hello %s!\nThe email address we have for you is %s. " % (name, email)
3
-
7/29/2019 Python Test
4/12
4. What list is returned by each of the function calls below?
a) range(5)
b) range(1,5)
c) range(10,0,-1)
5. Explain the difference between the "pass" statement and the "continue" statement as
they relate to loops.
6. Give the output of the following code:
for letter in "Python":
if letter == "t":
continue
print "Current Letter : ", letter
4
-
7/29/2019 Python Test
5/12
7. Give the output of the following code.
x = 1
while x
-
7/29/2019 Python Test
6/12
6
-
7/29/2019 Python Test
7/12
Short answer points vary
1) (10 points total) Write the Python code for the following flowchart (don"t
forget comments!):
7
Count = 8
x = 1
Y NCount
-
7/29/2019 Python Test
8/12
2) (10 points) Draw a flowchart (either style) for the following code:
def godoit(top):
print "Yea ", top
return
junk = 12
for m in range(6):
if m == 5:
print "m is 5"
godoit(m)
else if m > 0:
print junk, " divided by ", m, " is ", junk/m
godoit(m)
else:print "computers do not like dividing by 0"
8
-
7/29/2019 Python Test
9/12
3) (9 points) Give the output of the following Python program:
def godoit(top):
print "Yea ", top
return
junk = 12
for m in range(6):
if m == 5:
print "m is 5"
godoit(m)
else if m > 0:
print junk, " divided by ", m, " is ", junk/m
godoit(m)
else:print "computers do not like dividing by 0"
9
-
7/29/2019 Python Test
10/12
4) (10 points) Draw a flowchart to solve the following problem:
Problem: Count the number of times an input from the user via the keyboard is a real
number between 3 and 27. The user will continue to give input until a valueless than 0 is input. The program should print only one statement similar to
"You put in 6 values between 3 and 27."
10
-
7/29/2019 Python Test
11/12
5) (10 points) Write the Python code to solve the following problem (don"t
forget comments!):
Problem: Count the number of times an input from the user via the keyboard is a realnumber between 3 and 27. The user will continue to give input until a value less than
0 is input. The program should print only one statement similar to "You put in 6
values between 3 and 27."
11
-
7/29/2019 Python Test
12/12
6) (10 points) Give the output of the following program:
def quench(one, two = 25):
three = one + two
return three
first = 5
second = 10
third = 20
print first, " and ", second, " quenched is = ", quench(first, second)
print quench(third)
print third, " and ", first, " quenched is = ", quench(third, first)
print first + second + quench(third)
12