Question: Part 1 Exercises - Arrays and Lists Enter T for True or F for False. _____ (1) myList.count(x) will count the number of times that

Part 1 Exercises - Arrays and Lists

Enter T for True or F for False.

_____ (1) myList.count(x) will count the number of times that value x is in myList .

_____ (2) myList.pop() will remove and return the item in the list, which is at index 0 .

_____ (3) my_list = list("4") will create a list of four elements.

_____ (4) my_list[i] = x will change the value of the x th element in - place.

_____ (5) myList.insert(i, x) will insert x into myList after position i .

Part 2 Exercises - File Input / Output

Enter T for True or F for False.

_____ (1) The statement f.write(5.0) always produces an error.

_____ (2) The write() method requires a string argument. For example, the programmer should write f.write(str(5.0)) instead of f.write(5.0) .

_____ (3) The write() method immediately writes data to a file.

_____ (4) The flush() method ( and perhaps os.fsync() as well ) forces the output buffer to write to disk.

_____ (5) Use of a with statement is not recommended most of the time when opening files.

Part 3 Programming Exercises - File Input / Output

For the following exercises, refer to the program segment and file contents that follows.

____________________________________________________________

from datetime import *

# read file contents

print ("reading the data ... ")

fin = open("myInData.txt", "r")

lines = fin.readlines()

fin.close()

# iterate over each line

print (" calculating the average ... ")

count = 0

total = 0

for ln in lines :

count += 1

total += int(ln)

# Compute result

avg = total / len(lines)

print ("average value: ", avg)

# record the date / time

dt = datetime.today()

# write the data to a file

fout = open("myOutData.txt", "w")

fout.write("[statistics report ]" + " ")

fout.write("date: " + str(dt)+ " ")

fout.write("number of records: " + str(count) + " ")

fout.write("average score: " + "% 6.2f" % (avg) + " ")

fout.close()

____________________________________________________________

Contents of Input Data File: myInData.txt

87

76

52

93

64

Program Output ( Written to Data File ) : myOutData.txt

[statistics report ]

date: 2018-11-10 11:52:32.770355

number of records: 5

average score: 74.40

(1) ( File Processing )

Which file processing mode is used when the fout file object is assigned to the file

myOutData.txt ?

(a) rw (b) r (c) w (d) a (e) rb

(2) ( File Processing )

Which file processing mode is used when the fin file object is assigned to the file

myInData.txt ?

(a) rw (b) r (c) w (d) a (e) rb

(3) ( File Processing )

When the above code segment is executed, the average value is computed is computed before the for loop is interpreted.

(a) True (b) False

(4) ( File Processing )

Either of the statements shown below could have been used in the above program to display the current date and time.

print (str(datetime.now()))

print (str(datetime.today()))

(a) True (b) False

(5) ( File Processing )

How many data values were processed by the above program?

(a) 5 (b) 4 (c) 6 (d) 10 (e) 8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!