PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 38


What can you deduce from the following statement? (Choose two.)

Show Answer
Correct Answer: BD

When a file is opened in text mode with the 'rt' mode, it signifies that the file is opened for reading as text. Newline characters will be translated to the appropriate line endings for the platform, which is why option B is correct. Additionally, the file opened with 'rt' mode cannot be written to using the file object (str in this case), making option D correct. Option A is incorrect because the variable 'str' will be a file object, not a string containing the file content. Option C is incorrect because if the file file.txt does not exist, it will not be created and a FileNotFoundError will be raised instead.

Discussion

17 comments
Sign in to comment
NetspudOptions: AD
Feb 14, 2023

For me A is more correct than B

TheFivePipsOptions: BD
Dec 6, 2023

A. str is a string read in from the file named file.txt. The variable name str might be misleading, but in this context, it represents a file object, not a string. The open() function is used to open a file, not to read the content into a string. So, option A is not correct. B. A newline character translation will be performed during the reads. The mode "rt" stands for "read text." In text mode, newline characters are automatically translated to the appropriate line ending for the platform (e.g., \n on Unix-based systems, \r\n on Windows). So, option B is correct. C. If file.txt does not exist, it will be created. The mode "rt" opens the file in read text mode. If the file does not exist, it will raise a FileNotFoundError. So, option C is not correct. D. The opened file cannot be written with the use of the str variable. The file is opened in read text mode, which means you can read from it, but attempting to write to it would result in an error. So, option D is correct. Therefore, the correct deductions are B and D.

rbishunOptions: BD
Oct 20, 2021

The correct answers are: B and D. "rt" means read file as text (which is the default anyway). More info here: https://docs.python.org/3/library/functions.html#open

agus007Options: AC
Nov 4, 2022

A. may be correct if they change from 'string' to 'stream' there may be a typo. B. Is correct, only if the we are running over windows. With Unix-like OS there is no translation. And C is correct.

Rizos
Mar 21, 2023

How is C correct? I tried this, and if the file does not exist, it will say: "No such file or directory". So that one is incorrect.

DKMOptions: BD
Nov 4, 2021

b. a newline character translation will be performed during the reads d. the opened file cannot be written with the use of the str variable

hibana2077Options: BD
May 2, 2022

The correct answers are: B and D.

macxszOptions: BD
May 3, 2022

B. a newline character translation will be performed during the reads D. the opened file cannot be written with the use of the str variable

EltonAugustonOptions: BD
May 25, 2023

this is why A is incorrect : The variable str does not necessarily represent a string read from the file. It represents the file object returned by the open() function. To read the contents of the file into a string, you would need to use additional code like str_contents = str.read(). >>> B & D (correct)

SenthorusOptions: BD
Jul 4, 2023

It's BD because open return a file object not a string. https://github.com/sundowndev/phoneinfoga

Senthorus
Jul 4, 2023

oops wrong link https://www.w3schools.com/python/ref_func_open.asp

Valcon_doo_NoviSadOptions: BD
Oct 12, 2023

B and D.

Emirates34
Sep 28, 2021

I believe the given answer is incorrect. A - str is a file handle and does not contain the file contents.

rocky48Options: BD
Mar 8, 2022

B. a newline character translation will be performed during the reads D. the opened file cannot be written with the use of the str variable

666_mOptions: AD
May 6, 2022

Correct answers are A&D

palagusOptions: AD
May 24, 2022

Both statements are true. Statement B could be true or not regarding its content.

rmehmood
Aug 11, 2022

A is not correct, as str is not a string, its a file object

JnanadaOptions: BD
Aug 18, 2022

Answer should be B and D

Ello2023Options: AD
Jun 15, 2023

'rt' means read 'wt' means write In this piece of code we are opening a file with the name file.txt which is readable. str = open ('file.txt', 'rt')

Oracleist
Feb 9, 2024

but str is not a string