Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Which of the following file operations is NOT valid for reading a binary file? A) fileName = open(input.dat, r) B) fileName.close() C) fileName.write(Good Bye) D)

Which of the following file operations is NOT valid for reading a binary file?

A)

fileName = open("input.dat", "r")

B)

fileName.close()

C)

fileName.write("Good Bye")

D)

fileName = open("input.dat", "rw")

Save

Question 2

Consider the following code segment:

try :

inputFile = open("lyrics.txt", "r")

line = inputFile.readline()

print(line)

_____________________

print("Error")

What should be placed in the blank so that the program will print Error instead of crashing if an exception occurs while opening or reading from the file?

A)

except RuntimeError :

B)

except EnvironmentError :

C)

except IOError :

D)

except IndexError :

Save

Question 3

In the following code snippet, what happens if the "output.txt" file already exists?

outfile = open("output.txt", w")

A)

Any new data is appended to the end

B)

The existing file is emptied

C)

Nothing, this statement is ignored

D)

An error message is displayed

Question 4

Assume that your program is started with the following command:

python myProgram.py the quick brown fox

What will be displayed by the following statement?

print(sys.argv[5])

A)

brown

B)

fox

C)

A blank line

D)

The program will raise an exception

Question 5

Which of the following statements is NOT valid for reading from a file:

A)

inputFile.readline()

B)

inputFile.read()

C)

inputFile.readline(5)

D)

inputFile.read(5)

Question 6

When your program contains logic to read one or more files, which of the following statements is NOT true about the error handling logic needed:

A)

The file might not exist

B)

The file name might be too long

C)

The file might contain invalid data

D)

All files must be opened and closed prior to program termination

Question 7

Which of the following code segments will display all of the lines in the file object named infile, assuming that it has successfully been opened for reading?

A)

for infile in line :

print(line)

B)

for line in infile :

print(line)

C)

while infile in line :

print(line)

D)

while line in infile :

print(line)

Question 8

What portable file format is commonly used to export data from a spreadsheet so that it can be read and processed by a Python program?

A)

Comma-Separated Values (CSV)

B)

Graphics Interchange Format (GIF)

C)

Hypertext Markup Language (HTML)

D)

Portable Spreadsheet Format (PSF)

Question 9

Python's error handling process includes the finally clause. In the following code snippet, when is the finally clause executed?

inputFile = open("lyrics.txt", "r")

try :

line = inputFile.readline()

words = line.split()

print(words)

finally :

inputfile.close()

A)

Only when there is an error opening the file.

B)

Only when there is an error reading the file.

C)

The finally clause is always executed in this example.

D)

The finally clause is never executed in this example.

Question 10

Consider the following code segment:

import os

filename = input("Enter the name of a file: ")

if ____________________ :

print("That file exists!")

else:

print("That file does not exist.")

This code segment is supposed to print an appropriate message indicating whether or not the file specified by the user exists. What code should be placed in the blank so that the code segment performs its intended task?

A)

filename != ""

B)

os.path.exists()

C)

os.path.exists() == filename

D)

os.path.exists(filename)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

User Defined Tensor Data Analysis

Authors: Bin Dong ,Kesheng Wu ,Suren Byna

1st Edition

3030707490, 978-3030707491

More Books

Students also viewed these Databases questions

Question

What is recorded in the Wages and Salaries Expense account?

Answered: 1 week ago