CompTIA XK0-006 (CompTIA Linux+) Practice Test 51

Question 500

A Python script must store an ordered, mutable collection of server hostnames. Which built-in type fits?

  1. list
  2. tuple
  3. if 'port' in config:
  4. dict

Explanation

Correct Answer

A:

Python lists preserve element order and allow append, deletion and assignment. A tuple is ordered but immutable.

Incorrect Answers

B:

A tuple is an ordered collection whose element references cannot be reassigned after creation; it does not deliver storing mutable ordered hostnames, which is decisive in this scenario.

C:

The in operator tests membership in dictionary keys; the evidence calls for storing mutable ordered hostnames, not merely this separate operation.

D:

Dictionaries associate unique hashable keys with values and support efficient lookup; the task involves storing mutable ordered hostnames, whereas this addresses another subsystem.

Question 501

A Python tool needs to export currently installed environment packages and versions into a reviewable list. Which command is common?

  1. python -m pip freeze
  2. source .venv/bin/activate
  3. python3 -m venv .venv
  4. python -m pip install -r requirements.txt

Explanation

Correct Answer

A:

pip freeze lists installed distributions in requirement-style syntax. The output may include transient development dependencies, so review it before release.

Incorrect Answers

B:

The activate script adjusts the invoking shell's PATH and environment for the virtual environment; the administrator instead needs recording installed Python distribution versions; this option serves a different purpose.

C:

venv creates a dedicated environment with interpreter paths and site-packages for a project; it answers a neighboring question rather than recording installed Python distribution versions.

D:

pip reads dependency specifications from the file and installs compatible distributions into the chosen interpreter environment; that changes a different part of the system; the requested outcome is recording installed Python distribution versions.

Question 502

A Python script should read UTF-8 text and close the file automatically even when an error occurs. Which idiom fits?

  1. with open(path, encoding='utf-8') as f:
  2. subprocess.run(['systemctl','status','sshd'], check=True)
  3. pathlib.Path
  4. json.loads(response_text)

Explanation

Correct Answer

A:

The with statement uses a context manager to close an opened file reliably. Explicit encoding prevents surprises from platform defaults.

Incorrect Answers

B:

subprocess.run executes a program with explicit argument boundaries; using it would leave safely managing a text-file handle unresolved.

C:

Path objects provide path joining and filesystem operations while handling platform path conventions; the requirement is safely managing a text-file handle, outside the effect of this choice.

D:

json.loads parses JSON text into dictionaries, lists and scalar values; it does not deliver safely managing a text-file handle, which is decisive in this scenario.

Question 503

A project wants PEP 8-compliant readable identifiers for Python variables and functions. Which naming form is recommended?

  1. lower_case_with_underscores
  2. if __name__ == '__main__':
  3. import netutil
  4. Automated linting and style validation

Explanation

Correct Answer

A:

PEP 8 generally favors snake_case for variables and functions. Consistent names improve readability and tool support.

Incorrect Answers

B:

The module's __name__ value identifies direct execution versus normal import; its scope differs from applying Python variable naming conventions, so it cannot meet the stated requirement.

C:

Python imports modules and exposes their named functions and values through the module namespace; that is not equivalent to applying Python variable naming conventions.

D:

Linters and formatters can flag style inconsistencies and common errors before runtime; the administrator instead needs applying Python variable naming conventions; this option serves a different purpose.

Question 504

A project-specific Python environment exists at .venv and Bash users must activate it. Which command is appropriate?

  1. sys.executable
  2. source .venv/bin/activate
  3. python3 -m venv .venv
  4. python -m pip install -r requirements.txt

Explanation

Correct Answer

B:

The activate script adjusts the invoking shell's PATH and environment for the virtual environment. Deactivation reverses those shell changes.

Incorrect Answers

A:

Python's sys.executable identifies the path of the executing interpreter; that changes a different part of the system; the requested outcome is activating a Python virtual environment in Bash.

C:

venv creates a dedicated environment with interpreter paths and site-packages for a project; it will not provide activating a Python virtual environment in Bash under these conditions.

D:

pip reads dependency specifications from the file and installs compatible distributions into the chosen interpreter environment; using it would leave activating a Python virtual environment in Bash unresolved.

Question 505

A script must test whether a key is present in a dictionary before indexing it to avoid KeyError. Which syntax is suitable?

  1. dict
  2. tuple
  3. if 'port' in config:
  4. int('120')
  5. list

Explanation

Correct Answer

C:

The in operator tests membership in dictionary keys. It does not require catching KeyError for every optional field.

Incorrect Answers

A:

Dictionaries associate unique hashable keys with values and support efficient lookup; it does not deliver checking dictionary key membership, which is decisive in this scenario.

B:

A tuple is an ordered collection whose element references cannot be reassigned after creation; the evidence calls for checking dictionary key membership, not merely this separate operation.

D:

int converts a valid numeric string into an integer value; its scope differs from checking dictionary key membership, so it cannot meet the stated requirement.

E:

Python lists preserve element order and allow append, deletion and assignment; that is not equivalent to checking dictionary key membership.

Question 506

An inventory script must map hostname strings to IP address values for fast keyed access. Which built-in type fits?

  1. if 'port' in config:
  2. list
  3. dict
  4. int('120')

Explanation

Correct Answer

C:

Dictionaries associate unique hashable keys with values and support efficient lookup. Insertion order is preserved in current Python versions.

Incorrect Answers

A:

The in operator tests membership in dictionary keys; the administrator instead needs mapping inventory keys to values; this option serves a different purpose.

B:

Python lists preserve element order and allow append, deletion and assignment; it answers a neighboring question rather than mapping inventory keys to values.

D:

int converts a valid numeric string into an integer value; this does not achieve mapping inventory keys to values on the system described.

Question 507

A Python tool reads the text "120" from a file but must perform integer arithmetic. Which conversion is appropriate?

  1. tuple
  2. int('120')
  3. Automated linting and style validation
  4. dict
  5. list
  6. if 'port' in config:

Explanation

Correct Answer

B:

int converts a valid numeric string into an integer value. Strings and integers are different types, so explicit conversion avoids concatenation errors.

Incorrect Answers

A:

A tuple is an ordered collection whose element references cannot be reassigned after creation; using it would leave converting numeric text to an integer unresolved.

C:

Linters and formatters can flag style inconsistencies and common errors before runtime; it does not deliver converting numeric text to an integer, which is decisive in this scenario.

D:

Dictionaries associate unique hashable keys with values and support efficient lookup; the evidence calls for converting numeric text to an integer, not merely this separate operation.

E:

Python lists preserve element order and allow append, deletion and assignment; the task involves converting numeric text to an integer, whereas this addresses another subsystem.

F:

The in operator tests membership in dictionary keys; its scope differs from converting numeric text to an integer, so it cannot meet the stated requirement.

Question 508

A script's main routine should run when executed directly but not when imported as a module. Which guard supports that?

  1. pip
  2. lower_case_with_underscores
  3. import netutil
  4. if __name__ == '__main__':

Explanation

Correct Answer

D:

The module's __name__ value identifies direct execution versus normal import. The guard keeps reusable functions from triggering main-side effects when imported.

Incorrect Answers

A:

pip resolves and installs Python distributions from configured indexes; its scope differs from separating import behavior from direct execution, so it cannot meet the stated requirement.

B:

PEP 8 generally favors snake_case for variables and functions; that is not equivalent to separating import behavior from direct execution.

C:

Python imports modules and exposes their named functions and values through the module namespace; the administrator instead needs separating import behavior from direct execution; this option serves a different purpose.

Question 509

A system-administration tool needs to parse JSON from an API response into Python objects. Which standard-library helper does this?

  1. pathlib.Path
  2. subprocess.run(['systemctl','status','sshd'], check=True)
  3. FileNotFoundError
  4. json.loads(response_text)

Explanation

Correct Answer

D:

json.loads parses JSON text into dictionaries, lists and scalar values. It should be paired with input validation and error handling.

Incorrect Answers

A:

Path objects provide path joining and filesystem operations while handling platform path conventions; this does not achieve decoding JSON text into Python data on the system described.

B:

subprocess.run executes a program with explicit argument boundaries; it will not provide decoding JSON text into Python data under these conditions.

C:

FileNotFoundError identifies a missing path during file operations; using it would leave decoding JSON text into Python data unresolved.

Question 510

A system Python installation should remain untouched while a project tests a new dependency version. Which step creates isolation?

  1. sys.executable
  2. python -m pip freeze
  3. python -m pip install -r requirements.txt
  4. python3 -m venv .venv

Explanation

Correct Answer

D:

venv creates a dedicated environment with interpreter paths and site-packages for a project. Activation changes which python and pip commands are resolved.

Incorrect Answers

A:

Python's sys.executable identifies the path of the executing interpreter; the evidence calls for isolating Python project dependencies, not merely this separate operation.

B:

pip freeze lists installed distributions in requirement-style syntax; the task involves isolating Python project dependencies, whereas this addresses another subsystem.

C:

pip reads dependency specifications from the file and installs compatible distributions into the chosen interpreter environment; its scope differs from isolating Python project dependencies, so it cannot meet the stated requirement.

CompTIA Linux+ (XK0-006) Practice Tests