Source Python COURSE 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

A

If we open a file as follows: xfile = open('mbox.txt') A/for line in xfile: B/READ (xfile,*,END=10) line C/while (<xfile>) { D/while ( getline (xfile,line) ) {

A

Question 1 Given the architecture and terminology we introduced in Chapter 1, where are files stored? A/Secondary memory B/Motherboard C/Main Memory D/Machine Language

A

Question 2 What does the following Python program print out? x = '40' y = int(x) + 2 print(y) A/42 B/402 C/int402 D/x2

A

Question 8 Which of the following is not a valid string method in Python? A/twist() B/startswith() C/join() D/split()

A

Question 8 Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15] A/t[2:4] B/t[1:3] C/t[2:2] D/t[:] E/t[12:3]

A

What Python function would you use if you wanted to prompt the user for a file name to open? A/input() B/file_input() C/read() D/cin

A

What is a common use of Python dictionaries in a program? A/Building a histogram counting the occurrences of various strings in a file B/Splitting a line of input into words using a space as a delimiter C/Sorting a list of names into alphabetical order D/Computing an average of a set of numbers

A

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 B/It allows us to open more than one files and read them in a synchronized manner C/It enables random movement throughout the file D/It adds a new network connection to retrieve files from the network

A

What list method adds a new item to the end of an existing list? A/append() B/add() C/forward() D/pop() E/index() F/push()

A

What will end up in the variable y after this code is executed? x , y = 3, 4 A/4 B/A two item tuple C/A dictionary with the key 3 mapped to the value 4 D/A two item list E/3

A

What would the following Python code print out? stuff = dict() print(stuff['candy']) A/The program would fail with a traceback B/0 C/-1 D/candy

A

Which of the following is not a valid string method in Python? boldface() upper() lower() lstrip() startswith()

A

1. Question 1 What is the difference between a Python tuple and Python list? A/Lists are indexed by integers and tuples are indexed by strings B/Lists are mutable and tuples are not mutable C/Lists maintain the order of the items and tuples do not maintain order D/Tuples can be expanded after they are created and lists cannot

B

For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ] A/print(friends['Sally']) B/print(friends[2]) C/print(friends[2:1]) D/print friends[3]

B

Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list? A/For a list of items that will be extended as new items are found B/For a temporary variable that you will use and discard without modifying C/For a list of items that want to use strings as key values instead of integers D/For a list of items you intend to sort in place

B

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 tuple with three integers B/A list of tuples C/A list of integers D/A list of strings

B

Question 10 Which of the following string methods removes whitespace from both the beginning and end of a string? A/strtrunc() B/strip() C/wsrem() D/split()

B

Question 3 How would you use the index operator [] to print out the letter q from the following string? A/print(x[9]) B/print(x[8]) C/print(x[q]) D/print(x[7]) E/print x[-1]

B

Question 7 If the variable data is a Python list, how do we sort it in reverse order? A/data = sortrev(data) B/data.sort(reverse=True) C/data = data.sort(-1) D/data.sort.reverse()

B

What do we use the second parameter of the open() call to indicate? A/What disk drive the file is stored on B/Whether we want to read data from the file or write data to the file C/The list of folders to be searched to find the file we want to open D/How large we expect the file to be

B

What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str2 print(bob) A/Hello there B/Hellothere C/Hello D/Hello there

B

What does the following Python code do? fhand = open('mbox-short.txt') inp = fhand.read() A/Turns the text in the file into a graphic image like a PNG or JPG B/Reads the entire file into the variable inp as a string C/Prompts the user for a file name D/Checks to see if the file exists and can be written

B

What is a term commonly used to describe the Python dictionary feature in other programming languages? A/Associative arrays B/Closures C/Sequences D/Lambdas

B

What is stored in a "file handle" that is returned from a successful open() call? A/All the data from the file is read into memory and stored in the handle B/The handle is a connection to the file's data C/The handle has a list of all of the files in a particular folder on the hard drive D/The handle contains the first 10 lines of a file

B

What type of data is produced when you call the range() function? x = list(range(5)) A/A list of words B/A list of integers C/A string D/A boolean (true/false) value E/A list of characters

B

What would the following Python code print out? fruit = 'Banana' fruit[0] = 'b' print(fruit) A/B B/Nothing would print - the program fails with a traceback error C/Banana D/b E/banana F/[0]

B

What would the following Python code print out? stuff = dict() print(stuff.get('candy',-1)) A/'candy' B/-1 C/0 D/The program would fail with a traceback

B

In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ... It loops through the values in the dictionary It loops through all of the dictionaries in the program It loops through the keys in the dictionary It loops through the integers in the range from zero through the length of the dictionary

C

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) A/begin / rescue / end B/signal handlers C/try / except D/setjmp / longjmp

C

Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') A/print(days.get(1,-1)) B/print(days{2}) C/print(days[2]) D/print[days(2)] E/print(days[1]) F/print(days(2))

C

What are the Python keywords used to construct a loop to iterate through a list? A.try / except B.foreach / in C.for / in D.def / return

C

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 computes the average of all of the values in the dictionary B/It sorts the dictionary based on its key values C/It creates a list of tuples where each tuple is a value, key pair D/It computes the largest of all of the values in the dictionary

C

What does the following Python code print out? print(len('banana')*7) A/banana7 B/-1 C/42 D/banana banana banana banana banana banana banana

C

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/Remove the leading and trailing spaces from each line in mbox.txt B/Convert the lines in mbox.txt to upper case C/Count the lines in the file 'mbox.txt' D/Reverse the order of the lines in mbox.txt

C

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]) A/mar B/09:14 C/.ma D/ucT

C

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

C

Which method in a dictionary object gives you a list of the values in the dictionary? A/items() B/all() C/values() D/each() E/keys()

C

Which of the following methods work both in Python lists and Python tuples? A/append() B/sort() C/index() D/reverse() E/pop()

C

How are "collection" variables different from normal variables? A/Collection variables pull multiple network documents together B/Collection variables can only store a single value C/Collection variables merge streams of output into a single stream D/Collection variables can store multiple values in a single variable

D

How would you print out the following variable in all upper case in Python? greet = 'Hello Bob' A/puts(greet.ucase); B/print(upper(greet)) C/int i=0; char c; while (greet[i]){ c=greet[i]; putchar(toupper(c)); i++; } D/print(greet.upper())

D

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] ... A/startswith() B/ljust() C/trim() D/rstrip()

D

Question 1 How are Python dictionaries different from Python lists? A/Python lists can store strings and dictionaries can only store words B/Python dictionaries are a collection and lists are not a collection C/Python lists store multiple values and dictionaries store a single value D/Python lists are indexed using integers and dictionaries can use strings as indexes

D

Question 1 How are Python dictionaries different from Python lists? A/Python lists can store strings and dictionaries can only store words B/Python dictionaries are a collection and lists are not a collection C/Python lists store multiple values and dictionaries store a single value D/Python lists maintain order and dictionaries do not maintain order

D

Question 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() : ... A/Because for each item we want the previous and current key B/Because the keys for the dictionary are strings C/Because there are two items in the dictionary D/Because the items() method in dictionaries returns a list of tuples

D

What is the purpose of the second parameter of the get() method for Python dictionaries? A/The value to retrieve B/An alternate key to use if the first key cannot be found C/The key to retrieve D/To provide a default value if the key is not found

D

Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3) if ??? > x : ... A/(4, 100, 200) B/(0, 1000, 2000) C/(5, 0, 300) D/(6, 0, 0)

D

Question 4 How would you use string slicing [:] to print out 'uct' from the following string? x = 'From [email protected]' A/print(x[14+17]) B/print(x[14:3]) C/print(x[15:3]) D/print(x[14/17]) E/print(x[14:17]) F/print(x[15:18])

E

What is the iteration variable in the following Python code? for letter in 'banana' : print(letter) A/for B/print C/in D/'banana' E/letter

E

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] = key + 1 B/counts[key] = counts.get(key,-1) + 1 C/counts[key] = (counts[key] * 1) + 1 D/counts[key] = (key in counts) + 1 E/counts[key] = counts.get(key,0) + 1

E

What does the following Python code print out? a = [1, 2, 3] b = [4, 5, 6] c = a + b print(len(c)) A/[1, 2, 3] B/[4, 5, 6] C/21 D/[1, 2, 3, 4, 5, 6] E/15 F/6

F

Which of the following Python statements would print out the length of a list stored in the variable data? A/print(strlen(data)) B/print(data.length()) C/print(data.length) D/print(length(data)) E/print(data.Len) F/print(len(data))

F


Ensembles d'études connexes

COBIT 2019 Foundation Exam question & answers

View Set

Prep U Management of Patients with Urinary Disorders

View Set

Chapter 14 - Bonds and Long-Term Notes

View Set

Section 6-2: Hinduism and Buddhism

View Set

Legal Foundations of Business Chapter 17

View Set

Pragmatic Ways to Solve Workplace Accidents and Crimes.

View Set

Foundations PrepU: Chapter 38 Oxygenation and Perfusion

View Set