Sunteți pe pagina 1din 12

Test 2

100 points possible

Name_______________________

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

(a) True (b) False

____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)

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

7. Give the output of the following code. x=1 while x<5: print x x=x+1 print x

8. Give the output of the following code. x=1 while x in [1, 3, 5, 7, 9]: if x == 3: print "yahoo" elif x == 7: print "google" elif x == 9: pass else: print "maybe" x=x+2

9. Explain the difference between calling a function and defining a function.

Short answer points vary


1) (10 points total) Write the Python code for the following flowchart (don"t forget comments!): Count = 8 x=1 Y Print "Not positive, no action needed" Count <= 0? while x < Count Print "I love Python" Trig = sin(x) Print "sine of ", x, "is", Trig x=x+1 N

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"

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"

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 value less than 0 is input. The program should print only one statement similar to "You put in 6 values between 3 and 27."

10

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 real number 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

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

S-ar putea să vă placă și