nerdexam
CompTIA

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.

Vulnerability discovery and analysis

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)
  • A
    86% (30)
  • B
    6% (2)
  • C
    6% (2)
  • D
    3% (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.

ATo the screenCorrect

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.

BTo a network server

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.

CTo a file

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.

DTo /dev/null

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

#Python scripting#output redirection#code analysis

Community Discussion

No community discussion yet for this question.

Full PT0-001 Practice