exam questions

Exam 98-381 All Questions

View all questions & answers for the 98-381 exam

Exam 98-381 topic 1 question 28 discussion

Actual exam question from Microsoft's 98-381
Question #: 28
Topic #: 1
[All 98-381 Questions]

You are writing a Python program to automate inventory. Your first task is to read a file of inventory transactions. The file contains sales from the previous day, including the item id, price, and quantity.
The following shows a sample of data from the file:

The code must meet the following requirements:
✑ Each line of the file must be read and printed
✑ If a blank line is encountered, it must be ignored
✑ When all lines have been read, the file must be closed
You create the following code. Line numbers are included for reference only.

Which code should you write for line 05 and line 06?

A.

B.

C.

D.

Show Suggested Answer Hide Answer
Suggested Answer: A
https://www.dotnetperls.com/readline-python

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
chinna1523
Highly Voted 5 years ago
Answer is "C". line !=' ' and line != '\n'.
upvoted 7 times
...
Sarah987
Most Recent 2 years, 1 month ago
The answer is D. Here is why; Bearing mind With deadline()- an empty string always means the end of the file has been reached and a newline string \n means a blank line was encountered. A. if line != '\n', if line != "" #If the line contains a newline then it won't print the rest B. if line != '\n', if line != None #Same as A C. if line != '', if line != "" #This if clauses are not eliminating the newline and it will print the newline D. if line != '', if line != "\n" #Checks the end of the file and eliminates the newline. It will print all lines in the file
upvoted 1 times
Sarah987
2 years, 1 month ago
*readline()
upvoted 1 times
...
Sarah987
2 years, 1 month ago
*#If the line is a newline *These
upvoted 1 times
...
...
sudarchary
3 years, 7 months ago
Both C and D are good.
upvoted 1 times
...
qrkon
3 years, 9 months ago
Correct is D the last one: line !=' ' and line != '\n'.
upvoted 4 times
...
gargaditya
4 years, 7 months ago
A is correct,read method returns empty string on end of file. while eof==False: line=inventory.readline() if line != '\n' : #Not a blank line,then if line!="" : #Not reached end of file(end of file read as empty string) print(line) else: print("End of file") eof=True #next iteration of while loop will make condition false and break inventory.close() #else(for main 'if' clause),nothing happens and go to next iteration
upvoted 2 times
...
Viki2405
4 years, 7 months ago
Correct Answer is C
upvoted 1 times
...
JeffJupiter
4 years, 11 months ago
chinna1523 is correct, the correct answer is the last of the 4 choices. Using the first choice only the lines up to the blank line will be printed. Any lines after the blank line will not be printed. Last choice will print all lines before and after the blank line
upvoted 3 times
Satbabu
3 years, 9 months ago
I verified it you are right.
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago