



















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 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?
ANSWER (Chapter 1) What will the following program print out: >>> x = 15 >>> x = x + 5 >>> print x
.py
ANSWER (Chapter 1) Python scripts (files) have names that end with:
for
ANSWER (Chapter 1) Which of these words is a reserved word in Python?
quit()
ANSWER (Chapter 1) What is the proper way to say "good-bye" to Python?
Central Processing Unit
ANSWER (Chapter 1) Which of the parts of a computer actually execute the program instructions?
A sequence of instructions in a programming language
ANSWER (Chapter 1) What is "code" in the context of this course?
Secondary Memory
ANSWER (Chapter 1) A USB memory stick is an example of which of the following components of computer architecture?
The computer did not understand the statement that you entered
ANSWER (Chapter 1) What is the best way to think about a "Syntax Error" while programming?
Random steps
ANSWER (Chapter 1) Which of the following is not one of the programming patterns covered in Chapter 1?
ANSWER (Chapter 2) Which of the following is a comment in Python?
ANSWER (Chapter 2) What is the value in x after the following statement executes: x = 1 + 2 * 3 - 8 / 4
ANSWER (Chapter 2) What value be in the variable x when the following statement is executed? x = int(98.6)
Pause the program and read data from the user
ANSWER (Chapter 2) What does the Python raw_input() function do?
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?
ANSWER (Chapter 3) Which of these operators is not a comparison / logical operator?
Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
ANSWER (Chapter 3)What is true about the following code segment: if x == 5 : print 'Is 5' print 'Is Still 5' print 'Third 5'
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?
except
ANSWER (Chapter 3) What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?
Small
i. ii. iii. iv.
ANSWER (Chapter 3) For the following code: astr = 'Hello Bob' istr = 0 try: istr = int(astr) except: istr = - What will the value for istr after this code executes?
A built-in function
ANSWER (Chapter 4) In Python what is the raw_input() feature best described as?
def
ANSWER (Chapter 4) Which Python keyword indicates the start of a function definition?
ANSWER (Chapter 4) What will the following Python code print out? def func(x) : print x func(10) func(20)
Bonjour Michael
i. ii. iii. iv. v.
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'
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?
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
i. ii. iii. iv. v. vi. vii. viii. ix. x.
i.
ii. iii. iv. v.
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
Hellothere
ANSWER (Chapter 6) What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str print bob
ANSWER (Chapter 6) What does the following Python program print out? x = '40' y = int(x) + 2 print y
i. ii. iii. iv. v. vi.
i. ii. iii. iv.
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]'
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]'
letter
ANSWER (Chapter 6) What is the iteration variable in the following Python code? for letter in 'banana' : print letter
ANSWER (Chapter 6) What does the following Python code print out? print len('banana')*
i. ii. iii.
i. ii. iii. iv.
i. ii. iii.
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?
A list of integers
ANSWER (Chapter 8) What type of data is produced when you call the range() function? x = range(5)
append()
ANSWER (Chapter 8) What list method adds a new item to the end of an existing list?
for / in
ANSWER (Chapter 8) What are the Python keywords used to construct a loop to iterate through a list?
print friends[2]
ANSWER (Chapter 8) For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ]
i. ii.
i. ii.
ANSWER (Chapter 8) What does the following Python code print out? a = [1, 2, 3] b = [4, 5, 6] c = a + b print len(c)
Glenn
ANSWER (Chapter 8) What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ] friends.sort() print friends[0]
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]
Nothing would print - the program fails with a traceback
ANSWER (Chapter 8) What would the following Python code print out? fruit = 'Banana' fruit[0] = 'b' print fruit
i. ii. iii. iv. v.
i. ii. iii. iv. v.
i. ii.
i. ii. iii. iv.
print jane()
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
The Main memory
ANSWER (Midterm) Where in the computer is a variable such as "x" stored? x = 123
The program would show an error and a trace-back on the second line
ANSWER (Midterm) What would happen if the following Python code were executed? st = "abc" ix = int(st)
vii.
i. ii. iii. iv. v. vi.
i. ii. iii.
i. ii. iii.
Parenthesis
ANSWER (Midterm) If all of the following were used in a Python expression, which would have the highest precedence (i.e. which would be evaluated first)?
11 Hello
ANSWER (Midterm) What will the following Python program print out? (This is a bit tricky so look carefully). def hello(): print "Hello" print "There" x = 10 x = x + 1 print x, "Hello"
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?
i. ii. iii. iv. v. vi. vii. viii. ix. x. xi.
print x
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)
hello yo Done!
ANSWER (Midterm) What is the output of the following Python code, given the following inputs? hello
yo done
vi.
i. ii. iii. iv. v. vi. vii. viii. ix. x. xi. xii. xiii. xiv. xv. xvi.
i. ii. iii. iv. v. vi.
while True: line = raw_input('>') if line[0] == '#' : continue if line == 'done': break print line print 'Done!'
The program will run indefinitely
ANSWER (Midterm) What will happen if this code is run? n = 5 while True: print n n = n - 1 print 'Done!'
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!'
vii. viii. ix. x. xi. xii. xiii. xiv.
i. ii. iii. iv. v. vi.
i. ii. iii. iv. v. vi. vii.