exam questions

Exam 98-381 All Questions

View all questions & answers for the 98-381 exam

Exam 98-381 topic 1 question 30 discussion

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

You develop a Python application for your school.
You need to read and write data to a text file. If the file does not exist, it must be created. If the file has content, the content must be removed.
Which code should you use?

  • A. open("local_data", "r")
  • B. open("local_data", "r+")
  • C. open("local_data", "w+")
  • D. open("local_data", "w")
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️
Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing). Mode 'w+' truncates the file.
References:
https://docs.python.org/2/library/functions.html
https://pythontips.com/2014/01/15/the-open-function-explained/

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
atxsu
Highly Voted 5 years, 3 months ago
r+ = Open for reading and writing. The stream is positioned at the beginning of the file. w+ = Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. I think the answer is C.
upvoted 5 times
...
print_HelloWorld
Most Recent 3 years, 1 month ago
Selected Answer: C
The correct answer is C, I have used w+ before, r+ does not create a file if it doesn't exist but w+ does. Here's the reference to the website: https://stackoverflow.com/questions/28100993/in-python-why-is-r-but-not-rw-used-to-mean-read-write
upvoted 1 times
...
Billian
4 years, 2 months ago
Correct ans is C. and mode w+ exists. I have tested it
upvoted 2 times
...
sagacious_smith
4 years, 4 months ago
Ans should be r+. There is no mode as w+
upvoted 1 times
...
s_balla_in
5 years, 6 months ago
Is the answer B or C? B. open("local_data", r+)
upvoted 2 times
hedi12
4 years, 2 months ago
C : the correct answer
upvoted 2 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 ...