









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










What will the value for istr after this code executes? -1 - ANSWER-(Chapter 5) What will the following code print out? smallest_so_far = - for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num print smallest_so_far Hint: This is a trick question and most would say this code has a bug - so read carefully -1 - ANSWER-(Chapter 9) What would the following Python code print out? stuff = dict() print stuff.get('candy',-1) -1 - ANSWER-(Midterm) For the following Python program, what will it print out? x = - for value in [3, 41, 12, 9, 74, 15] : if value < x : x = value print x .ma - ANSWER-(Chapter 6) What will the following Python code print out? data = 'From [email protected] Sat Jan 5 09:14:16 2008' pos = data.find('.') print data[pos:pos+3] .py - ANSWER-(Chapter 1) Python scripts (files) have names that end with: (5/9) - ANSWER-(Midterm) You develop the following program in Python:
f = int(raw_input("Enter:")) c = ( f - 32 ) * ( 5 / 9 ) print "Celsius",c And when you run it three times you get the following output: Enter: Celsius 0 Enter: Celsius 0 Enter: Celsius 0 What part of the program is causing the output to always be zero?
= - ANSWER-(Chapter 3) Which of these operators is not a comparison / logical operator? 0 - ANSWER-(Chapter 5) How many times will the body of the following loop be executed? n = 0 while n > 0 : print 'Lather' print 'Rinse' print 'Dry off!' 1 - ANSWER-(Chapter 3) 'In the following code (numbers added) - which will be the last line to execute successfully?
print x 123abc - ANSWER-(Chapter 2) What does the following code print out? 2 - ANSWER-(Chapter 2) What is the value of the following expression? 42 % 10 2 - ANSWER-(Chapter 4)What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a x = addtwo(2, 7) print x 2 - ANSWER-(Midterm) What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a x = addtwo(2, 7) print x 20 - ANSWER-(Chapter 1) What will the following program print out: >>> x = 15 >>> x = x + 5 >>> print x 42 - ANSWER-(Chapter 6) What does the following Python code print out? print len('banana')* 42 - ANSWER-(Chapter 6) What does the following Python program print out? x = '40' y = int(x) + 2
print y 5 4 3 Done! - ANSWER-(Midterm) What is the output of this code? n = 5 while True: if n < 3: break print n n = n - 1 print 'Done!' 5 - ANSWER-(Chapter 2) What is the value in x after the following statement executes: x = 1 + 2 * 3 - 8 / 4 5 - ANSWER-(Chapter 5) What does the following Python program print out? tot = 0 for i in [5, 4, 3, 2, 1] : tot = tot + 1 print tot 5 3 1 Done - ANSWER-(Midterm) What will this code output? (note the comma after a print statement) n = 5 while n > 0: print n, n = n - 2 print 'Done!' 6
print len(c) 98 - ANSWER-(Chapter 2) What value be in the variable x when the following statement is executed? x = int(98.6) A built-in function - ANSWER-(Chapter 4) In Python what is the raw_input() feature best described as? A list of integers - ANSWER-(Chapter 8) What type of data is produced when you call the range() function? x = range(5) A sequence of instructions in a programming language - ANSWER-(Chapter 1) What is "code" in the context of this course? append() - ANSWER-(Chapter 8) What list method adds a new item to the end of an existing list? Associative arrays - ANSWER-(Chapter 9) What is a term commonly used to describe the Python dictionary feature in other programming languages? Avoiding writing the same non-trivial code more than once in your program - ANSWER- (Chapter 4) What is the most important benefit of writing your own functions? Bonjour - ANSWER-(Midterm) What is the output of the following program, if the user input is: fr?
def greet(lang): if lang == 'es': # if lang is Spanish print 'Hola' return 'adios' elif lang == 'fr': # French print 'Bonjour' return 'au revoir' else:
print 'Hello' return 'bye' xx = raw_input('enter language: ') #----main program begins here--- yy= greet(xx) Bonjour Michael - ANSWER-(Chapter 4) What will the following Python program print out? def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print greet('fr'),'Michael' Central Processing Unit - ANSWER-(Chapter 1) Which of the parts of a computer actually execute the program instructions? Collection variables can store multiple values in a single variable - ANSWER-(Chapter
for friend in friends : print 'Happy New Year:', friend print 'Done!' Glenn - ANSWER-(Chapter 8) What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ] friends.sort() print friends[0] h e l l o - ANSWER-(Midterm) What will the following Python code print out? i = 10 list1 = ['h','e','l','l','o'] for i in list1: print i hello yo Done! - ANSWER-(Midterm) What is the output of the following Python code, given the following inputs? hello
yo done
while True:
line = raw_input('>') if line[0] == '#' : continue if line == 'done': break print line print 'Done!' Hellothere - ANSWER-(Chapter 6) What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str print bob hours - ANSWER-(Chapter 2) Which of the following variables is the "most mnemonic"? Indent the line below the if statement - ANSWER-(Chapter 3) What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true? It loops through the keys in the dictionary - ANSWER-(Chapter 9) In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ... Jane returns! Jane returns! - ANSWER-(Midterm) What will the following Python program print out: def fred():
Pause the program and read data from the user - ANSWER-(Chapter 2) What does the Python raw_input() function do? print 'World' - ANSWER-(Chapter 4) Which line of the following Python program is useless? def stuff(): print 'Hello' return print 'World' stuff() print friends[2] - ANSWER-(Chapter 8) For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ] print greet.upper() - ANSWER-(Chapter 6) How would you print out the following variable in all upper case in Python? greet = 'Hello Bob' print len(data) - ANSWER-(Chapter 8) Which of the following Python statements would print out the length of a list stored in the variable data? print x[14:17] - ANSWER-(Chapter 6) How would you use string slicing [:] to print out 'uct' from the following string? x = 'From [email protected]' printx[8] - ANSWER-(Chapter 6) How would you use the index operator [] to print out the letter q from the following string? x = 'From [email protected]' Python lists maintain order and dictionaries do not maintain order - ANSWER-(Chapter
Small All done - ANSWER-(Chapter 3) What will the following code print out? x = 0 if x < 2 : print 'Small' elif x < 10 : print 'Medium' else : print 'LARGE' print 'All done' stop - ANSWER-(Chapter 2) Which of the following variables is the "most mnemonic"? strip() - ANSWER-(Chapter 6) Which of the following string methods removes whitespace from both the beginning and end of a string? Sum all the elements of a list - ANSWER-(Chapter 5) What is a good description of the following bit of Python code? zork = 0 for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing print 'After', zork t[2:4] - ANSWER-(Chapter 8) Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15] The computer did not understand the statement that you entered - ANSWER-(Chapter
To provide a default value if the key is not found - ANSWER-(Chapter 9) What is the purpose of the second parameter of the get() method for Python dictionaries? Traceback error - ANSWER-(Chapter 9) What would the following Python code print out? stuff = dict() print stuff['candy'] twist() - ANSWER-(Chapter 6) Which of the following is not a valid string method in Python? values() - ANSWER-(Chapter 9) Which method in a dictionary object gives you a list of the values in the dictionary? What is a common use of Python dictionaries in a program? - ANSWER-(Chapter 9) Building a histogram counting the occurrences of various strings in a file What Python statement would you like me to run? - ANSWER-(Chapter 1) When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you? while - ANSWER-(Chapter 5) Which reserved word indicates the start of an "indefinite" loop in Python? x - ANSWER-(Chapter 4) In the following Python code, which of the following is an "argument" to a function? x = 'banana' y = max(x) print y print x You de-indent a line of code to the same indent level as the def keyword - ANSWER- (Chapter 4) In Python, how do you indicate the end of the block of code that makes up the function? You de-indent the next line past the if block to the same level of indent as the original if statement - ANSWER-(Chapter 3) When you have multiple lines in an if block, how do you indicate the end of the if block? You have most likely mixed tabs and spaces in the file - ANSWER-(Chapter 3) You look at the following text:
if x == 6 : print 'Is 6' print 'Is Still 6' print 'Third 6' It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason? Zap ABC Zap - ANSWER-(Midterm) What will the following Python program print out:
def fred(): print "Zap" def jane(): print "ABC"
Zap = 42 ABC = 22 fred() jane() fred()