Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam PCAP All Questions

View all questions & answers for the PCAP exam

Exam PCAP topic 1 question 125 discussion

Actual exam question from Python Institute's PCAP
Question #: 125
Topic #: 1
[All PCAP Questions]

What is the expected output of the following code if the file named non_zero_length_existing_text_file is a non-zero length file located inside the working directory?

  • A. -1
  • B. 0
  • C. an errno value corresponding to file not found
  • D. 1
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

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
deckman
Highly Voted 2 years, 6 months ago
Selected Answer: D
Correct answer is "D" In case would not be found in directory, IOError exception would be caught, so it would print -1 and answer would be "A"
upvoted 11 times
...
kstr
Most Recent 9 months, 2 weeks ago
Correct answer D. If the file named 'non_zero_length_existing_text_file' is a non-zero length file located inside the working directory, the expected output of the provided code would be the length of the first character read from the file. However, the provided code only reads one character from the file and prints the length of that character. Therefore, the expected output would be 1 regardless of the content of the file. This is because len(d) will always return 1, as d contains a single character read from the file. If the file contains more than one character, only the first character will be read due to f.read(1), but the length of that character (which is always 1) will be printed. The remaining characters in the file will not be processed or printed in this code snippet.
upvoted 1 times
...
clecorre
1 year, 3 months ago
Correct answer is "D", the file is in the working directory, so no need any path and there is no error.
upvoted 1 times
...
cufta05
1 year, 5 months ago
try: f = open('non_zero_length_existing_text_file', 'rt') d = f.read(1) print(len(d)) f.close() except IOError: print(-1)
upvoted 2 times
...
swatiphadtare
1 year, 6 months ago
Selected Answer: D
Correct answer is D ie 1, f.read(1) will read one char from file so len of d will be 1
upvoted 2 times
...
Kumar1825
1 year, 8 months ago
Selected Answer: A
IOError exception would be caught, so it would print -1 and answer would be "A"
upvoted 2 times
swatiphadtare
1 year, 7 months ago
why IOError, it said that file would be present inside working directory
upvoted 1 times
...
...
dicksonpwc
1 year, 8 months ago
Correct answer is A As it could not find such a file or directory, an IOError exception could be caught and print -1
upvoted 3 times
...
besha
2 years, 4 months ago
Correct answer is C, it throws FileNotFoundError: [Errno 2] No such file or directory:
upvoted 1 times
zantrz
9 months, 2 weeks ago
The file could not be found in your directory, but the instruction says that the file is located inside the working directory. The answer is D.1
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 ...