Coursera: Python Data-Structures University of Michigan- all quizzes solutions


Coursera: Python Data-Structures University of Michigan all quizzes solutions



Chapter 6 quiz :

1> What does the following Python Program print out? str1 = "Hello"; str2 = 'there'; bob = str1 + str2; print bob

sol)    Hellothere


2> What does the following Python program print out? x = '40'; y = int(x) + 2; print y

sol)    42


3> How would you use the index operator [] to print out the letter q from the following string? x = 'From marquard@uct.ac.za'

sol)    print(x[8])


4> How would you use string slicing [:] to print out 'uct' from the following string? x = 'From marquard@uct.ac.za'

sol)    print(x[14:17])


5> What is the iteration variable in the following Python code? for letter in 'banana' : print letter

sol)    letter


6> What does the following Python code print out? print len('banana')*7

sol)     42


7> How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'

sol)    print(greet.upper())


8> Which of the following is not a valid string method in Python?

sol)    twist()


9> What will the following Python code print out? data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008';pos = data.find('.');print(data[pos:pos+3])

sol)    .ma


10> Which of the following string methods removes whitespace from both the beginning and end of a string?

sol)    strip()




Chapter 7 quiz :


1> Given the architecture and terminology we introduced in Chapter 1, where are files stored?

sol)  Secondary memory


2> What is stored in a "file handle" that is returned from a successful open() call?

sol)  The handle is a connection to the file's data


3> What do we use the second parameter of the open() call to indicate?

sol)  Whether we want to read data from the file or write data to the file


4> What Python function would you use if you wanted to prompt the user for a file name to open?

sol)  input()


5> What is the purpose of the newline character in text files?

sol)  It indicates the end of one line of text and the beginning of another line of text


6> If we open a file as follows: xfile = open('mbox.txt'). What statement would we use to read the file one line at a time?

sol)  for line in xfile:


7> What is the purpose of the following Python code? fhand = open('mbox.txt'); x = 0; for line in fhand: x = x + 1; print x

sol)  Count the lines in the file 'mbox.txt'


8> If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?. From: stephen.marquard@uct.ac.za; From: louis@media.berkeley.edu; From: zqian@umich.edu; From: rjlowe@iupui.edu ...

sol)  strip()


9> The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered? fname = raw_input('Enter the file name: '); fhand = open(fname)

sol)  try / except


10> What does the following Python code do? fhand = open('mbox-short.txt'); inp = fhand.read()

sol)  Reads the entire file into the variable inp as a string




Chapter 8 quiz :

1> How are "collection" variables different from normal variables?
sol) Collection variables can store multiple values in a single variable


2> What are the Python keywords used to construct a loop to iterate through a list?
 sol) for/in


3> For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally'] sol) print(friends[2])


4> fruit = 'Banana' fruit[0] = 'b'; print(fruit) sol) Nothing would print the program fails with a traceback


5> Which of the following Python statements would print out the length of a list stored in the variable data?
sol) print(len(data))


6> What type of data is produced when you call the range() function? x = range(5)
sol) A list of integers


7> What does the following Python code print out? a = [1, 2, 3]; b = [4, 5, 6]; c = a + b; print len(c) sol) 6


8> Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15] sol) t[2:4]


9> What list method adds a new item to the end of an existing list?
sol) append()


10> What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ]; friends.sort(); print(friends[0])
sol) Glenn



Chapter 9 quiz :

1> How are Python dictionaries different from Python lists?

sol)  Python lists maintain order and dictionaries do not maintain order


2> What is a term commonly used to describe the Python dictionary feature in other programming languages?

sol)  Associative arrays


3> What would the following Python code print out? stuff = dict(); print(stuff['candy'])

sol)  The program would fail with a traceback


4> What would the following Python code print out? stuff = dict(); print(stuff.get('candy',-1))

sol)  -1


5> (T/F) When you add items to a dictionary they remain in the order in which you added them.

sol)  False


6> What is a common use of Python dictionaries in a program?

sol)  Building a histogram counting the occurrences of various strings in a file


7> Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary? if key in counts: counts[key] = counts[key] + 1 else: counts[key] = 1

sol)  counts[key] = counts.get(key,0) + 1


8> In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ...

sol)  It loops through the keys in the dictionary


9> Which method in a dictionary object gives you a list of the values in the dictionary?

sol)  values()


10> What is the purpose of the second parameter of the get() method for Python dictionaries?

sol)  To provide a default value if the key is not found




Chapter 10 quiz :

1> What is the difference between a Python tuple and Python list?

sol)  Lists are mutable and tuples are not mutable


2> Which of the following methods work both in Python lists and Python tuples?

sol)  index()


3> What will end up in the variable y after this code is executed? x , y = 3, 4

sol)  4


4> In the following Python code, what will end up in the variable y? x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}; y = x.items()

sol)  A list of tuples


5> Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3); if ??? > x : ...

sol)  (6, 0, 0)


6> What does the following Python code accomplish, assuming the c is a non-empty dictionary? tmp = list(); for k, v in c.items(): tmp.append( (v, k))

sol)  It creates a list of tuples where each tuple is a value, key pair


7> If the variable data is a Python list, how do we sort it in reverse order?

sol)  data.sort(reverse=True)


8> Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

sol)  print(days[2])


9> In the following Python loop, why are there two iteration variables (k and v)? c = {'a':10, 'b':1, 'c':22}; for k, v in c.items() : ...

sol)  Because the items() method in dictionaries returns a list of tuples


10> Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?

sol)  For a temporary variable that you will use and discard without modifying


darkmode