SI 106 midterm

¡Supera tus tareas y exámenes ahora con Quizwiz!

The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while ( n > 0 ): answer = answer + n n = n + 1 print answer (A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive (B) answer starts at 1 and is incremented by n each time, so it will always be positive (C) You cannot compare n to 0 in while loop. You must compare it to another variable. (D) In the while loop body, we must set n to False, and this code does not do that.

(A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive

How many lines will be output by executing this code? def hello(): print "Hello" print "Glad to meet you"

0

What will the following code output? def square(x): return x*x def g(y): return y + 3 def h(y): return square(y) + 3 print g(h(2))

10

What will the following code output? def square(x): y = x * x return y print square(square(2))

16

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23} mydict["mouse"] = mydict["cat"] + mydict["dog"] print mydict["mouse"]

18

How many lines will the following code print? def show_me_numbers(list_of_ints): print 10 print "Next we'll accumulate the sum" accum = 0 for num in list_of_ints: accum = accum + num return accum print "All done with accumulation!" show_me_numbers([4,2,3]) ****************

2

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} answer = mydict.get("cat")//mydict.get("dog") print answer

2

What is printed by the following statements? total = 0 mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} for akey in mydict: if len(akey) > 3: total = total + mydict[akey] print total

43

What will the following code output? def square(x): y = x * x return y print square(5) + square(5)

50

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23} print mydict["dog"]

6

What will the following code output? def square(x): return x*x def g(y): return y + 3 def h(y): return square(y) + 3 print h(2)

7

def hello(): print "Hello" print "Glad to meet you" hello() print "It works" hello() hello()

7

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} yourdict = mydict yourdict["elephant"] = 999 print mydict["elephant"] ******

999; since yourdict is an alias for mydict, the value for the key elephant has been changed

What is a function in Python?

A named sequence of statements.

What is a local variable?

A temporary variable that is only used inside a function

What does the following code print? if (4 + 5 == 10): print "TRUE" else: print "FALSE" print "TRUE"

FALSE TRUE

What does the following code print (choose from output a, b, c or nothing). if (4 + 5 == 10): print "TRUE" else: print "FALSE"

False

What will the following code output? def cyu2(s1, s2): x = len(s1) y = len(s2) return x-y z = cyu2("Yes", "no") if z > 0: print "First one was longer" else: print "Second one was at least as long"

First one was longer

What output will the following code produce? def cyu(s1, s2): if len(s1) > len(s2): print s1 else: print s2 cyu("Hello", "Goodbye")

Goodbye

How many statements can appear in each block (the if and the else) in a conditional statement?

One or more

What does the following code print? x = -10 if x < 0: print "The negative number ", x, " is not valid here." print "This is always printed"

The negative number -10 is not valid here This is always printed

What is a variable's scope?

The range of statements in the code where a variable can be accessed.

What will the following function return? def addEm(x, y, z): print x+y+z

The value None

What is one main purpose of a function?

To help the programmer organize programs into chunks that match how they think about the solution to the problem.

A dictionary is an unordered collection of key-value pairs.

True

True or False: You can rewrite any for-loop as a while-loop.

True

True or false: A function can be called several times by placing a function call in the body of a for loop.

True

Can you use the same name for a local variable as a global variable?

Yes, but it is considered bad form; it is generally considered bad style because of the potential for the programmer to get confused. If you must use global variables (also generally bad form) make sure they have unique names

Will the following code cause an error? x = -10 if x < 0: print "The negative number ", x, " is not valid here." else: print x, " is a positive number" else: print "This is always printed"

Yes; This will cause an error because the second else-block is not attached to a corresponding if-block

What is wrong with the following function definition: def addEm(x, y, z): return x+y+z print 'the answer is', x+y+z

You should not have any statements in a function after the return statement. Once the function gets to the return statement it will immediately stop executing the function.

Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer.

a for-loop or a while-loop

What will the following code print if x = 3, y = 5, and z = 2? if x < y and x < z: print "a" elif y < x and y < z: print "b" else: print "c"

c

Which of the following is a valid function header (first line of a function definition)?

def greet(t):

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print 23 in mydict

false

Which will print out first, square, g, or a number? def square(x): print "square" return x*x def g(y): print "g" return y + 3 print square(g(2)) *********

g g has to be executed and return a value in order to know what paramater value to provide to x.

Which is the right code block to use in place of line 5 if we want to print out the maximum value? d = {'a': 194, 'b': 54, 'c':34, 'd': 44, 'e': 312, 'full':31} a = 0 for c in d: # <what code goes here? See below options> print "max value is " + a

if d[c] > a: a = d[c]

Which of I, II, and III below gives the same result as the following nested if? # nested if-else statement x = -10 if x < 0: print "The negative number ", x, " is not valid here." else: if x > 0: print x, " is a positive number" else: print x, " is 0"

if x < 0: print "The negative number ", x, " is not valid here." elif (x > 0): print x, " is a positive number" else: print x, " is 0"

What are the possible types of the return value from cyu3? def cyu3(x, y, z): if x - y > 0: return y -2 else: z.append(y) return x + 3

integer, float

What are the possible types of variables x and y? def cyu3(x, y, z): if x - y > 0: return y -2 else: z.append(y) return x + 3

integer, float

What are the possible types of variable z? def cyu3(x, y, z): if x - y > 0: return y -2 else: z.append(y) return x + 3

list

Will the following code cause an error? x = -10 if x < 0: print "The negative number ", x, " is not valid here." else: if x > 0: print x, " is a positive number" else: print x," is 0"

no: This is a legal nested if-else statement. The inner if-else statement is contained completely within the body of the outer else-block

Which of the following will print out True if there are more occurrences of e than t in the text of A Study in Scarlet, and False if t occurred more frequently (assumming that the previous code, from dict_accum_5, has already run.)

print x['e'] > x['t']

What is the name of the following function? def print_many(x, y): """Print out string x, y times.""" for i in range(y): print x

print_many

Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)? def print_many(x, y): """Print out string x, y times.""" for i in range(y): print x z = 3

print_many("Greetings", z)

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print "dog" in mydict

true

What are the parameters of the following function? def print_many(x, y): """Print out string x, y times.""" for i in range(y): print x

x,y


Conjuntos de estudio relacionados

Digital Crime Investigation quiz 5, DCOM258: Quiz11: Ch12: Vulnerability and Risk Assessment, Chapter 12 Quiz, DCOM258: Quiz15: Ch16: Redundancy and Disaster Recovery, CC6003 Digital Crime Investigation quiz 3, DCOM258: Quiz: Introduction to Security...

View Set

Conducting Psychology Research in the Real World

View Set

Unit 3: GI/GU/Endocrine Practice

View Set