Python Data Structures Quiz Answers 2021

this contains every one of the responses to the Coursera Python Data Structures Quiz Answers for "Programming for Everybody - Python-Data Structures, on Coursera by the University of Michigan.

Coursera Python Data Structures Quiz Answers


Coursera Python Data Structures Quiz Questions and Answers

Python-Data-Structures Chapter 6 Quiz Answers

Q. What does the following Python Program print out? str1 = "Hello"; str2 = 'there'; bob = str1 + str2; print bob
Ans: Hellothere

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

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

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

Q. What is the iteration variable in the following Python code? for letter in 'banana' : print letter
A. letter

Q. What does the following Python code print out? print len('banana')*7
A. 42

Q. How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'
A. print greet.upper()

Q. Which of the following is not a valid string method in Python?
A. twist()

Q. 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]
A. .ma

Q. Which of the following string methods removes whitespace from both the beginning and end of a string?
A. strip()


Python-Data-Structures Chapter 7 Quiz Answers

Q.Given the architecture and terminology we introduced in Chapter 1, where are files stored?
A. Secondary memory

Q. What is stored in a "file handle" that is returned from a successful open() call?
A. The handle is a connection to the file's data

Q. What do we use the second parameter of the open() call to indicate?
A. Whether we want to read data from the file or write data to the file

Q. What Python function would you use if you wanted to prompt the user for a file name to open?
A. raw_input()

Q. What is the purpose of the newline character in text files?
A. It indicates the end of one line of text and the beginning of another line of text

Q. 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?
A. for line in xfile:

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

Q. 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 ...
A. strip()

Q. 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)
A. try / except

Q. What does the following Python code do? fhand = open('mbox-short.txt'); inp = fhand.read()
A. Reads the entire file into the variable inp as a string

Python-Data-Structures Chapter 8 Quiz Answers


Q. How are "collection" variables different from normal variables?
A. Collection variables can store multiple values in a single variable

Q. What are the Python keywords used to construct a loop to iterate through a list?
A. for/in

Q. For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally']
A. print friends[2]

Q. fruit = 'Banana' fruit[0] = 'b'; print fruit
A. Nothing would print the program fails with a traceback

Q. Which of the following Python statements would print out the length of a list stored in the variable data?
A. print len(data)

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

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

Q. Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]
A. t[2:4]

Q. What list method adds a new item to the end of an existing list?
A. append()

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

Python-Data-Structures Chapter 9 Quiz Answers

Q. How are Python dictionaries different from Python lists?
A. Python lists are indexed using integers and dictionaries can use strings as indexes

Q. What is a term commonly used to describe the Python dictionary feature in other programming languages?
A. Associative arrays

Q. What would the following Python code print out? stuff = dict(); print stuff['candy']
A. The program would fail with a traceback

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

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

Q. What is a common use of Python dictionaries in a program?
A. Building a histogram counting the occurrences of various strings in a file

Q. 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
A. counts[key] = counts.get(key,0) + 1

Q. In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ...
A. It loops through the keys in the dictionary

Q. Which method in a dictionary object gives you a list of the values in the dictionary?
A. values()

Q. What is the purpose of the second parameter of the get() method for Python dictionaries?
A. To provide a default value if the key is not found

Python-Data-Structures Chapter 10 Quiz Answers

Q. What is the difference between a Python tuple and a Python list?
A. Lists are mutable and tuples are not mutable

Q. Which of the following methods work both in Python lists and Python tuples?
A. index()
 
Q. What will end up in the variable y after this code is executed? x , y = 3, 4
A. 4

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

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

Q. 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))
A. It creates a list of tuples where each tuple is a value, key pair

Q. If the variable data is a Python list, how do we sort it in reverse order?
A. data.sort(reverse=True)

Q. Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
A. print days[2]

Q. 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() : ...
A. Because the items() method in dictionaries returns a list of tuples

Q. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?
A. For a temporary variable that you will use and discard without modifying

Related Posts: