PT0-001 · Question #46
Given the following Python script: Which of the following is where the output will go?
The correct answer is A. To the screen. Python's print() function writes to standard output (stdout) by default, which displays on the screen unless the script explicitly redirects output elsewhere.
Question
Given the following Python script:
Which of the following is where the output will go?
Options
- ATo the screen
- BTo a network server
- CTo a file
- DTo /dev/null
How the community answered
(35 responses)- A86% (30)
- B6% (2)
- C6% (2)
- D3% (1)
Why each option
Python's print() function writes to standard output (stdout) by default, which displays on the screen unless the script explicitly redirects output elsewhere.
In Python, the print() function sends output to sys.stdout, which by default is the terminal screen. Unless the script explicitly redirects output using file handles, socket objects, or sys.stdout reassignment, all print() output appears on the screen in the current terminal session.
Outputting to a network server requires explicit socket programming or use of a network library such as socket or requests, which are not indicated in the script.
Writing to a file requires opening a file handle with open() and either passing it to print(file=...) or redirecting sys.stdout to the file object.
Redirecting output to /dev/null requires explicitly setting sys.stdout = open('/dev/null', 'w') within the script or using shell-level redirection outside the script.
Concept tested: Python standard output default behavior
Source: https://docs.python.org/3/library/functions.html#print
Topics
Community Discussion
No community discussion yet for this question.