CS 116

Ace your homework & exams now with Quizwiz!

Which mode specifier will erase the contents of a file if it already exists and create it if it does not exist?

"w" write write mode 'w' w

Which of these is not a valid file mode when opening a file in Python?

'f'

Which mode specifier will open a file but will not let you change the file or write to it?

'r' r read read mode "r"

What method or operator can be used to concatenate lists?

+

What is the output of the following code? myList=['a','b','c','d','e'] z = 1 for x in range(5): word=myList[x] print(str(z),word,end="") z+=1

1 a2 b3 c4 d5 e

At the end of the following program, how many elements would the dictionary, names, contain? names={'Smith':'Bob','Jones':'Emily'} names['Smith']='George' print(names)

2

What is the output of the following code? count=0 list1=[0]*2 for item in list1: if item==0: count+=1 print(count)

2

What is the output of the following code? person=["Jane",485,329] print(person[1])

485

What is the output of the following code? height=[72, 84, 67, 74] print(max(height))

84

What is the output of the following code? nums={[2,4,6]:'evens',[1,3,5]:'odds'} print(nums[[2,4,6]]) None of these. 'odds' An error is generated in the second line of code. evens odds An error is generated in the first line of code. 'evens'

An error is generated in the first line of code.

When a file is first opened successfully, where is the read position indicator?

At the beginning of the file

Given that there is a large list and a large dictionary containing the same amount of data, when the programmer attempts to retrieve a particular piece of data from each structure, which of these statements is true?

Data in the dictionary can be retrieved faster than in the list.

What is the output of the following code? colors={'red':'angry','blue':'warm'} a,b=colors.popitem() print(a)

Either red or blue could be output.

(T/F) In Python, there is no way to prevent a program from crashing if it attempts to read a file that does not exist.

False

(T/F) When opening a file in write mode, if a file with the specified name already exists, a warning message is generated.

False

Which of these is associated with a specific file, and provides a way for the program to work with that file?

File object

Given a valid file that contains multiple lines of data, what is the output of the following code: f=open("names.txt","w") print(f)

None of these

What is the output of the following code? list1 = [1, 2, 3, 4] list1[3] = 10 print(list1)

None of these

What is the output of the following code? list1=[1,2,3] list2=list1 list1[1]=7 print(list2)

None of these

What is the output of the following code? val1={'small':[1,2,3],'large':[64,87,92]} x=val1.get('medium','gigantic') print(x) An error is generated in the first line of code. None of these. [1,2,3] x [64,87,92] An error is generated in the second line of code.

None of these

Which of the following is used to add a new item to a dictionary? add append concat Correct! None of these +

None of these

Which statement would produce the value of the number of items in the list, list1? None of these list1.size() sizeof(list1) list1+1 list1.index() items in list1

None of these

What is the number of the first index in a dictionary?

None of these.

Which of these causes an IOError?

Opening a file that doesn't exist.

What do you call the process of retrieving data from a file?

Reading

What is the output of the following code? count=0 days=['Monday','Tuesday','Wednesday','Thursday','Friday'] for a in days: if count==2: print("Results:",end="") for b in range(2): print(a,end="") count+=1

Results:WednesdayWednesday

What does the get method do if the specified key is not found in the dictionary?

Returns a default value

You used the following statement to create a dictionary: relationships = {'Jimmy':'brother'}. You then executed the following code, and received a KeyError exception. What is the reason for the exception? print(relationships['jimmy'])

String comparisons are case sensitive so 'jimmy' does not equal 'Jimmy'.

What is needed to delete a single line from a file?

The creation of a new file

What does the index value -1 refer to in a list?

The last item in the list

Given a valid file containing multiple lines of data, what does the print() statement print out the first time that it is called: f=open("data.txt","r") for line in f: line=f.readline() print(line) f.close()

The second line of the file

What is the output of the following code? list1=['a','b','c'] list1.remove(1) print(list1)

This code causes an error

What is the output of the following code? d={0:'a',1:'b',2:'c'} print(d[-1])

This code causes an error.

What would be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages[1] print(value)

This code generates an error

What is the output of the following code? list1=['a','b','c'] print(list1[3])

This code produces an error

What is the output of the following code? list1=[] list1+=4 print(list1)

This code produces an error

What is the output of the following code? list1=[] list1[1]='hello' print(list1[1][0])

This code produces an error

(T/F) A dictionary can include the same value several times.

True

(T/F) A file can safely be opened and closed multiple times within the same program.

True

(T/F) A for loop that reads in lines from a file automatically stops when it reaches the end of the file, whereas the readline() method requires you to check for an empty string to signal the end of the file.

True

(T/F) All members of a set must be unique.

True

(T/F) Closing a file disconnects the communication between the file and the program.

True

(T/F) Dictionaries are mutable types.

True

(T/F) Strings can be written directly to a file with the write method, but integer data types must always be converted to strings before they can be written to a file.

True

(T/F) The following code could potentially raise a ZeroDivisionError. f=open("names.txt", 'r') for x in f: print(5/int(x.rstrip()))

True

(T/F) The following code could potentially raise an IOError. f=open("names.txt", 'r') for x in f: print(5/int(x.rstrip()))

True

What is the output of the following code? nums=[2,5,3,7] if 5 in nums: print('Yes') else: print('No')

Yes

What is the output of the following code? a = '03/07/2014' b = a.split('/') print(b)

['03', '07', '2014']

What is the output of the following code? school = ['primary','secondary',['college','university']] print(school[2])

['college', 'university']

What is the output of the following code? number = [0,1,2,3,4] print(number[:])

[0, 1, 2, 3, 4]

What is the output of the following code? x=[2] y=[7] z=x+y print(z)

[2, 7]

What is the output of the following code? list1=[6,4,65,7,12] print(list1[-2:])

[7, 12]

Given that the file is valid and contains numbers, what does x contain after the following statement?

a file object

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the _____ operator.

in

What method can be used to place an item in the list at a specific index?

insert

Which method would you use to return all the keys in a dictionary and their associated values as a list of tuples?

items

In a dictionary, you use a(n) _____ to locate a specific value.

key

Which function would you use to get the number of elements in a dictionary?

len

Which of the following statements will convert the values in a tuple named tuple1 to the values in a list named list1.

list1=list(tuple1)

Which of the following statements is true? lists are mutable; strings are immutable both lists and strings are immutable lists are immutable; strings are mutable None of these both lists and strings are mutable

lists are mutable; strings are immutable

A(n)__________ is a function that belongs to an object, and performs some operation using that object.

method

The ________________ is one or more statements grouped together with the anticipation that they could potentially raise an exception.

try block

To respond to errors, what kind of a statement must be used?

try/except

The items method of a dictionary returns what type of value?

tuple

What is the correct syntax for creating a dictionary of month names to be accessed by month numbers (only the first three months are given)? { 1, 2, 3 : 'January', 'February', 'March' } [ 1 : 'January', 2 : 'February', 3 : 'March' ] { 1 : 'January', 2 : 'February', 3 : 'March' } { 1 ; 'January', 2 ; 'February', 3 ; 'March' } {' ','January','February','March'}

{ 1 : 'January', 2 : 'February', 3 : 'March' }

What is the output of the following code? phones = {'John': '5555555', 'Julie' : '7777777'} phones['John'] = '1234567' print(phones)

{'John': '1234567', 'Julie': '7777777'}

When a file is opened in this mode, data will be written at the end of the file's existing contents.

append mode

This is a small "holding section" in memory that many systems write data to before writing the data to a file.

buffer

Assume that there is a variable named customer that references a file object. How would you write the string 'Mary Smith' to the file?

customer.write('Mary Smith')

Which statement could you use to remove an existing key-value pair from a dictionary?

del

Given a list, what would you use if an item is to be removed from a specific index number?

del function

What is the output of the following code? list1=['h','e','l','l','o'] list1.sort() print(list1[0])

e

This is a single piece of data within a record.

field

Before a file can be used by a program, it must be _________.

opened

Which method would you use to return the value associated with a specified key and remove that key-value pair from the dictionary?

pop

Given the following list, which statement will print out the character h? list1=[['ab','cd'],['ef','gh'],['ij','kl']]

print(list1[1][1][1])

If a file has been opened properly, which method will return the file's entire contents as a string?

read()

A(n) ______ is a complete set of data about an item, usually a line of data in an input file.

record

Which method could be used to strip specific characters from just the end of a string?

rstrip()


Related study sets

Chapter 7, personality chapter 8, personality chapter 9 & 10

View Set

Othello - Love and Relationships

View Set

Physics Practice Questions- Ch. 16-17

View Set

Chapter 16 inflammation and immunity study with chapter 39

View Set

Unit 9 Intellectual Property (okrent)

View Set

Chapter 18: Regulation of Gene Expression

View Set

Chapter 1: Introduction to Mobile Testing

View Set

(PrepU) Informed Consent: Concept Exemplar

View Set