Final Exam Prep Flashcards

1
Q

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

A

read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

write

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

customer.write(‘Mary Smith’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

read()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

rstrip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

x = open(“numbers.txt”, “r”)

A

a file object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

opened

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

This is a single piece of data within a record.

A

field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

append mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

try/except

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of these causes an IOError?

A

Opening a file that doesn’t exist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

File object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

Reading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A(n) ______ is a complete set of data about an item.

A

record

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

try block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

The while loop automatically reads a line in a file without testing for any special condition that signals the end of the file. T/F

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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)

A

None of these
The contents of the entire file are printed
The letter “f” is printed.
Only the first line of the file is printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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)

A

The second line of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

The following code could potentially raise an IOError. T/F

f=open(“names.txt”, ‘r’)
for x in f:
print(5/x.rstrip())

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

The following code could potentially raise a ZeroDivisionError. T/F

f=open(“names.txt”, ‘r’)
for x in f:
print(5/x.rstrip())

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

A

At the beginning of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

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

A

The creation of a new file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

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

A

Buffer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the output of the following code?

number = [0,1,2,3,4]
print(number[:])

A

[0, 1, 2, 3, 4]

32
Q

What would you use if an element is to be removed from a specific index number?

A

del function

33
Q

What is the output of the following code?

list1=[]
list1[1]=’hello’
print(list1[1][0])

A

This code produces an error

34
Q

What is the output of the following code?

list1=[1,2,3]
list2=list1
list1[1]=7
print(list2)

A

None of these

'list1' 
[7,7,7] 
7
[1,2,3] 
This code produces no output 
This code produces an error
35
Q

What is the first negative index in a list?

A

-1

36
Q

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

A

insert

37
Q

What is the output of the following code?

list1=[‘a’,’b’,’c’]
print(list1[3])

A

This code produces an error

38
Q

What is the output of the following code?

list1 = [1, 2, 3, 4]
list1[3] = 10
print(list1)

A

None of these

 [1, 10, 10, 10]
[list1]
[10]  
[1, 2, 10, 4] 
[1, 2, 3, 4] 
This code causes an error
39
Q

What method or operator can be used to concatenate lists?

A

+

40
Q

What is the ouput of the following code?

school = [‘primary’,’secondary’,[‘college’,’university’]]
print(school[2])

A

[‘college’, ‘university’]

41
Q

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
A

Results:WednesdayWednesday

42
Q

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
A

1 a2 b3 c4 d5 e

43
Q

What is the output of the following code?

count=0
list1=[0]*2
for item in list1:
  if item==0:
    count+=1
print(count)
A

2

44
Q

What is the output of the following code?

a = ‘03/07/2014’
b = a.split(‘/’)
print(b)

A

[‘03’, ‘07’, ‘2014’]

45
Q

Which statement would produce the value of the number of items in the list, list1?

A

None of these

46
Q

What is the output of the following code?

list1=[6,4,65,7,12]
print(list1[-2:])

A

[7, 12]

47
Q

Which of the following statements is true?

A

lists are mutable; strings are immutable

48
Q

What is the output of the following code?

list1=[‘a’,’b’,’c’]
list1.remove(1)
print(list1)

A

This code causes an error

49
Q

Given the following list, which statement will print out the character h?

list1=[[‘ab’,’cd’],[‘ef’,’gh’],[‘ij’,’kl’]]

A

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

50
Q

What is the output of the following code?

list1=[‘h’,’e’,’l’,’l’,’o’]
list1.sort()
print(list1[0])

A

e

51
Q

What is the output of the following code?

x=[2]
y=[7]
z=x+y
print(z)

A

[2, 7]

52
Q

What is the output of the following code?

nums=[2,5,3,7]
if 5 in nums:
    print('Yes')
else:
    print('No')
A

Yes

53
Q

What is the output of the following code?

person=[“Jane”,485,329]
print(person[1])

A

485

54
Q

What is the output of the following code?

height=[72, 84, 67, 74]
print(max(height))

A

84

55
Q

What is the output of the following code?

list1=[]
list1+=4
print(list1)

A

This code produces an error

56
Q

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

A

key

57
Q

What is the correct structure for creating a dictionary of month names to be accessed by month numbers (only the first three months are given)?

A

{ 1 : ‘January’, 2 : ‘February’, 3 : ‘March’ }

58
Q

What would be the result of the following code?

ages = {‘Aaron’ : 6, ‘Kelly’ : 3, ‘Abigail’ : 1 }
value = ages[1]
print(value)

A

This code generates an error

59
Q

You created the following dictionary relationships = {‘Jimmy’:’brother’}. You then executed the following code, and received a KeyError exception. What is the reason for the exception?

relationships[‘jimmy’]

A

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

60
Q

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

A

del

61
Q

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

A

len

62
Q

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

A

items

63
Q

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

A

None of these.

 'A' 
0
Size of the dictionary minus one. 
1
 -1
64
Q

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

A

pop

65
Q

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

A

tuple

66
Q

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

A

Returns a default value

67
Q

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

A

key

68
Q

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

A

in

69
Q

What is the output of the following code?

phones = {‘John’: ‘5555555’, ‘Julie’ : ‘7777777’}
phones[‘John’] = ‘1234567’
print(phones)

A

{‘John’: ‘1234567’, ‘Julie’: ‘7777777’}

70
Q

What is the output of the following code?

d={0:’a’,1:’b’,2:’c’}
print(d[-1])

A

This code causes an error.

71
Q

Which of the following is used to add a new item to a dictionary?

A

None of these

72
Q

A dictionary can include the same value several times.

A

True

73
Q

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

A

list1=list(tuple1)

74
Q

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?

A

Any speed difference will depend largely on the type of data stored, sometimes a list will be faster, sometimes a dictionary will be faster.

75
Q

Dictionaries are mutable types. T/F

A

True