Python Data Structures Coursera Quiz Answers – Networking Funda

All Weeks Python Data Structures Coursera Quiz Answers

Python Data Structures Week 01 Quiz Answers

Q1. What does the following Python Program print out?

1234str1 = "Hello"

str2 = 'there'

bob = str1 + str2print(bob)

  • Hello
  • there
  • Hello
  • Hellothere
  • Hello there

Q2. What does the following Python program print out?

x = '40'

y = int(x) + 2

print(y)

  • x2
  • int402
  • 402
  • 42

Q3. How would you use the index operator [] to print out the letter q from the following string?

x = 'From [email protected]'

  • print x[-1]
  • print(x[9])
  • print(x[8])
  • print(x[7])
  • print(x[q])

Q4. How would you use string slicing [:] to print out ‘uct’ from the following string?

x = 'From [email protected]'

  • print(x[14:3])
  • print(x[15:18])
  • print(x[14+17])
  • print(x[15:3])
  • print(x[14/17])
  • print(x[14:17])

Q5. What is the iteration variable in the following Python code?

for letter in 'banana' :    

print(letter)

  • for
  • letter
  • print
  • in
  • ‘banana’

Q6. What does the following Python code print out?

print(len('banana')*7)

  • banana7
  • 42
  • -1
  • banana banana banana banana banana banana banana

Q7. How would you print out the following variable in all upper case in Python?

greet = 'Hello Bob'

  • print(greet.upper())
  • puts(greet.ucase);
  • print(uc($greet));
  • print(greet.toUpperCase());

Q8. Which of the following is not a valid string method in Python?

  • shout()
  • split()
  • join()
  • startswith()

Q9. 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])

  • 09:14
  • .ma
  • uct
  • mar

Q10. Which of the following string methods removes whitespace from both the beginning and end of a string?

  • strip()
  • split()
  • wsrem()
  • strtrunc()

Python Data Structures Week 02 Quiz Answers

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

  • Main Memory
  • Secondary memory
  • Motherboard
  • Central Processor

Q2. What is stored in a “file handle” that is returned from a successful open() call?

  • All the data from the file is read into memory and stored in the handle
  • The handle has a list of all of the files in a particular folder on the hard drive
  • The handle contains the first 10 lines of a file
  • The handle is a connection to the file’s data

Q3. What do we use the second parameter of the open() call to indicate?

  • The list of folders to be searched to find the file we want to open
  • Whether we want to read data from the file or write data to the file
  • What disk drive the file is stored on
  • How large we expect the file to be

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

  • file_input()
  • cin
  • read()
  • input()

Q5. What is the purpose of the newline character in text files?

  • It indicates the end of one line of text and the beginning of another line of text
  • It enables random movement throughout the file
  • It adds a new network connection to retrieve files from the network
  • It allows us to open more than one files and read them in a synchronized manner

Q6. 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?

  • while ((line = xfile.readLine()) != null) {
  • while (<xfile>) {
  • READ xfile INTO LINE1
  • for line in xfile:

Q7. What is the purpose of the following Python code?

fhand = open('mbox.txt')

x = 0

for line in fhand:       

x = x + 1

print(x)

  • Reverse the order of the lines in mbox.txt
  • Remove the leading and trailing spaces from each line in mbox.txt
  • Convert the lines in mbox.txt to lower case
  • Count the lines in the file ‘mbox.txt’

Q8. 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: [email protected]

From: [email protected]

From: [email protected] 

From: [email protected] ...

  • split()
  • ljust()
  • trim()
  • rstrip()

Q9. 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 = input('Enter the file name: ')

fhand = open(fname)

  • try / catch / finally
  • try / except
  • setjmp / longjmp
  • signal handlers

Q10. What does the following Python code do?

fhand = open('mbox-short.txt')

inp = fhand.read()

  • Prompts the user for a file name
  • Reads the entire file into the variable inp as a string
  • Checks to see if the file exists and can be written
  • Turns the text in the file into a graphic image like a PNG or JPG

Python Data Structures Week 03 Quiz Answers

Q1. How are “collection” variables different from normal variables?

  • Collection variables can store multiple values in a single variable
  • Collection variables can only store a single value
  • Collection variables pull multiple network documents together
  • Collection variables merge streams of output into a single stream

Q2. What are the Python keywords used to construct a loop to iterate through a list?

  • for / in
  • def / return
  • foreach / in
  • try / except

Q3. For the following list, how would you print out ‘Sally’?

friends = [ 'Joseph', 'Glenn', 'Sally' ]

  • print friends[3]
  • print(friends[‘Sally’])
  • print(friends[2:1])
  • print(friends[2])

Q4. What would the following Python code print out?

fruit = 'Banana'

fruit[0] = 'b'

print(fruit)

  • banana
  • b
  • [0]
  • B
  • Banana
  • Nothing would print – the program fails with a traceback error

Q5. Which of the following Python statements would print out the length of a list stored in the variable data?

  • print(length(data))
  • print(data.length)
  • print(strlen(data))
  • print(data.Len)
  • print(len(data))
  • print(data.length())

Q6. What type of data is produced when you call the range() function?1x = list(range(5))1 point

  • A string
  • A list of characters
  • A list of integers
  • A boolean (true/false) value
  • A list of words

Q7. What does the following Python code print out?

a = [1, 2, 3]

b = [4, 5, 6]

c = a + b

print(len(c))

  • [4, 5, 6]
  • [1, 2, 3]
  • [1, 2, 3, 4, 5, 6]
  • 6
  • 15
  • 21

Q8. Which of the following slicing operations will produce the list [12, 3]?

t = [9, 41, 12, 3, 74, 15]

  • t[1:3]
  • t[2:2]
  • t[2:4]
  • t[:]
  • t[12:3]

Q9. What list method adds a new item to the end of an existing list?

  • add()
  • index()
  • forward()
  • push()
  • pop()
  • append()

Q10. What will the following Python code print out?

friends = [ 'Joseph', 'Glenn', 'Sally' ]

friends.sort()

print(friends[0])

  • Joseph
  • friends
  • Glenn
  • Sally

Python Data Structures Week04 Quiz Answers

Q1. How are Python dictionaries different from Python lists?

  • Python dictionaries are a collection and lists are not a collection
  • Python lists store multiple values and dictionaries store a single value
  • Python lists maintain order and dictionaries do not maintain order
  • Python lists can store strings and dictionaries can only store words

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

  • Lambdas
  • Associative arrays
  • Sequences
  • Closures

Q3. What would the following Python code print out?

stuff = dict()

print(stuff['candy'])

  • -1
  • 0
  • The program would fail with a traceback
  • candy

Q4. What would the following Python code print out?

stuff = dict()

print(stuff.get('candy',-1))

  • The program would fail with a traceback
  • ‘candy’
  • 0
  • -1

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

  • False
  • True

Q6. What is a common use of Python dictionaries in a program?

  • Computing an average of a set of numbers
  • Building a histogram counting the occurrences of various strings in a file
  • Splitting a line of input into words using a space as a delimiter
  • Sorting a list of names into alphabetical order

Q7. 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

  • counts[key] = (key in counts) + 1
  • counts[key] = (counts[key] * 1) + 1
  • counts[key] = key + 1
  • counts[key] = counts.get(key,0) + 1
  • counts[key] = counts.get(key,-1) + 1

Q8. In the following Python, what does the for loop iterate through?

x = dict()

...

for y in x : 

    ...

  • It loops through the keys in the dictionary
  • It loops through the integers in the range from zero through the length of the dictionary
  • It loops through the values in the dictionary
  • It loops through all of the dictionaries in the program

Q9. Which method in a dictionary object gives you a list of the values in the dictionary?

  • each()
  • all()
  • keys()
  • values()
  • items()

Q10. What is the purpose of the second parameter of the get() method for Python dictionaries?

  • To provide a default value if the key is not found
  • The key to retrieve
  • An alternate key to use if the first key cannot be found
  • The value to retrieve

Python Data Structures Week05 Quiz Answers

Q1. What is the difference between a Python tuple and Python list?

  • Tuples can be expanded after they are created and lists cannot
  • Lists maintain the order of the items and tuples do not maintain order
  • Lists are indexed by integers and tuples are indexed by strings
  • Lists are mutable and tuples are not mutable

Q2. Which of the following methods work both in Python lists and Python tuples?

  • append()
  • index()
  • reverse()
  • pop()
  • sort()

Q3. What will end up in the variable y after this code is executed?

x , y = 3, 4

  • 3
  • A two item list
  • 4
  • A two item tuple
  • A dictionary with the key 3 mapped to the value 4

Q4. In the following Python code, what will end up in the variable y?

x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}

y = x.items

  • A list of strings
  • A tuple with three integers
  • A list of tuples
  • A list of integers

Q5. Which of the following tuples is greater than x in the following Python sequence?

x = (5, 1, 3)

if ??? > x :  

 ...

  • (5, 0, 300)
  • (0, 1000, 2000)
  • (4, 100, 200)
  • (6, 0, 0)

Q6. 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) )

  • It creates a list of tuples where each tuple is a value, key pair
  • It computes the average of all of the values in the dictionary
  • It sorts the dictionary based on its key values
  • It computes the largest of all of the values in the dictionary

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

  • data.sort.reverse()
  • data.sort(reverse=True)
  • data = data.sort(-1)
  • data = sortrev(data)

Q8. Using the following tuple, how would you print ‘Wed’?

days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

  • print(days[1])
  • print(days(2))
  • print(days[2])
  • print[days(2)]
  • print(days.get(1,-1))
  • print(days{2})

Q9. 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() :   

 ...

  • Because the keys for the dictionary are strings
  • Because for each item we want the previous and current key
  • Because there are two items in the dictionary
  • Because the items() method in dictionaries returns a list of tuples

Q10. Given that Python lists and Python tuples are quite similar – when might you prefer to use a tuple over a list?

  • For a temporary variable that you will use and discard without modifying
  • For a list of items that want to use strings as key values instead of integers
  • For a list of items that will be extended as new items are found
  • For a list of items you intend to sort in place
Get All Course Quiz Answers of Python for Everybody Specialization

Course 01: Programming for Everybody (Getting Started with Python)

Course 02: Python Data Structures

Course 03: Using Python to Access Web Data

Course 04: Using Databases with Python

Course 05: Capstone: Retrieving, Processing, and Visualizing Data with Python

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *