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

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

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: C 🗳️
The code doesn't mention the string for a directory if the non-zero length file is located inside of it. So it will be a file not found error.

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
deckman
Highly Voted 1 year, 11 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 2 months 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
7 months, 3 weeks 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
9 months, 4 weeks 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
11 months, 2 weeks 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 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 ago
why IOError, it said that file would be present inside working directory
upvoted 1 times
...
...
dicksonpwc
1 year, 1 month 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
1 year, 9 months ago
Correct answer is C, it throws FileNotFoundError: [Errno 2] No such file or directory:
upvoted 1 times
zantrz
2 months 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 ...