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

Question 490

A script needs a stable way to print strings that may begin with dashes or contain percent characters. Which builtin is preferable?

  1. [[ $one == "$two" ]]
  2. #!/usr/bin/env bash
  3. printf '%s\n' "$value"
  4. set -e

Explanation

Correct Answer

C:

printf uses an explicit format string and avoids several echo option and escape ambiguities. Quoting preserves the argument's boundaries.

Incorrect Answers

A:

The == operator performs string-pattern comparison in Bash double brackets; quoting the right side forces literal matching; it will not provide printing arbitrary string data predictably under these conditions.

B:

The interpreter selection header does not provide safe string formatting; Bash printf handles strings predictably. env searches PATH for bash, which can help across distributions; using it would leave printing arbitrary string data predictably unresolved.

D:

errexit causes the shell to exit on certain nonzero statuses outside exception contexts; it does not deliver printing arbitrary string data predictably, which is decisive in this scenario.

Question 491

A script needs a random integer within Bash's basic built-in range for a non-security test fixture. Which variable can help?

  1. RANDOM
  2. IFS
  3. set -x
  4. mktemp

Explanation

Correct Answer

A:

Bash's RANDOM produces pseudorandom integers for lightweight shell tasks. It is unsuitable for cryptographic secrets.

Incorrect Answers

B:

The Internal Field Separator affects shell word splitting and read parsing; its scope differs from generating non-cryptographic test random values, so it cannot meet the stated requirement.

C:

xtrace prints expanded commands before execution, which aids tracing; that is not equivalent to generating non-cryptographic test random values.

D:

mktemp creates unique temporary files or directories securely using a suitable template; the administrator instead needs generating non-cryptographic test random values; this option serves a different purpose.

Question 492

A numeric threshold must trigger only when count exceeds 100, not when it equals 100. Which conditional operator works?

  1. [[ -f /etc/app.conf ]]
  2. [[ -d /srv/reports ]]
  3. [[ -n $token ]]
  4. [[ $count -gt 100 ]]
  5. if ! command; then recover; fi

Explanation

Correct Answer

D:

The -gt operator checks strict numeric greater-than. The -ge operator also accepts equality, which is not the stated condition.

Incorrect Answers

A:

-f evaluates true for an existing regular file, with symlink dereference under normal shell test behavior; that changes a different part of the system; the requested outcome is testing strict numeric greater-than.

B:

The -d file-test operator succeeds when the path refers to an existing directory, including one reached through a valid symlink; this does not achieve testing strict numeric greater-than on the system described.

C:

-n succeeds when the expanded string has nonzero length; it will not provide testing strict numeric greater-than under these conditions.

E:

The ! operator negates the tested command's success status; the requirement is testing strict numeric greater-than, outside the effect of this choice.

Question 493

A script must halt with a custom error if TOKEN is unset or empty. Which parameter expansion supports this directly?

  1. ${PORT:-8080}
  2. ${TOKEN:?TOKEN is required}
  3. $?
  4. ${HOST}

Explanation

Correct Answer

B:

The :? operator reports an error and can exit a noninteractive shell when a required variable is missing. This is useful for early validation of critical inputs.

Incorrect Answers

A:

The :- form supplies a fallback when the variable is unset or null; it does not deliver rejecting a missing required parameter, which is decisive in this scenario.

C:

$? expands to the most recent command or pipeline's exit status under normal shell semantics; the task involves rejecting a missing required parameter, whereas this addresses another subsystem.

D:

Braced parameter expansion clearly delimits the variable name; its scope differs from rejecting a missing required parameter, so it cannot meet the stated requirement.

Question 494

A numeric loop should execute exactly five iterations with indices from 0 through 4. Which Bash syntax fits?

  1. until healthcheck; do sleep 2; done
  2. continue
  3. break
  4. "${items[@]}"
  5. for dir in /etc /var /opt; do ...; done
  6. for ((i=0; i<5; i++)); do ...; done

Explanation

Correct Answer

F:

The arithmetic for loop initializes, tests and increments the counter. Its strict comparison excludes index 5.

Incorrect Answers

A:

An until loop repeats while its test returns a failure status; the administrator instead needs iterating a bounded integer counter; this option serves a different purpose.

B:

continue advances loop control to the next iteration; it answers a neighboring question rather than iterating a bounded integer counter.

C:

break exits a loop before its normal completion, rather than controlling the five planned arithmetic-loop iterations. continue would instead skip only the remainder of the current iteration; that changes a different part of the system; the requested outcome is iterating a bounded integer counter.

D:

Quoted array-at expansion yields one word per element while preserving embedded whitespace; this does not achieve iterating a bounded integer counter on the system described.

E:

A for loop iterates over supplied words in order; it will not provide iterating a bounded integer counter under these conditions.

Question 495

A script needs to store the current date printed by date in a variable. Which Bash syntax executes the command and captures stdout?

  1. ${TOKEN:?TOKEN is required}
  2. today=$(date +%F)
  3. ${HOST}
  4. ( VAR=new; command )

Explanation

Correct Answer

B:

Command substitution replaces the expression with the command's standard output. Trailing newlines are removed during substitution.

Incorrect Answers

A:

The :? operator reports an error and can exit a noninteractive shell when a required variable is missing; using it would leave capturing command output into a variable unresolved.

C:

Braced parameter expansion clearly delimits the variable name; it does not deliver capturing command output into a variable, which is decisive in this scenario.

D:

Parentheses run shell commands in a subshell context; the evidence calls for capturing command output into a variable, not merely this separate operation.

Question 496

An admin writes a reusable Python helper module named netutil.py. How does another script access its functions?

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

Explanation

Correct Answer

B:

Python imports modules and exposes their named functions and values through the module namespace. The module must be discoverable on the import path.

Incorrect Answers

A:

Linters and formatters can flag style inconsistencies and common errors before runtime; its scope differs from reusing functions from another Python module, so it cannot meet the stated requirement.

C:

The module's __name__ value identifies direct execution versus normal import; the administrator instead needs reusing functions from another Python module; this option serves a different purpose.

D:

pip resolves and installs Python distributions from configured indexes; it answers a neighboring question rather than reusing functions from another Python module.

Question 497

A Python project uses a dedicated virtual environment. An engineer must inspect the installed requests package version, installation location and declared dependencies without modifying the environment. Which command fits?

  1. python -m pip show requests
  2. python -m pip install requests
  3. python -m pip freeze
  4. python3 -m venv .venv
  5. python -m pip uninstall requests

Explanation

Correct Answer

A:

pip show prints metadata for an installed Python distribution, including version, location and required distributions, without downloading or changing packages.

Incorrect Answers

B:

pip install may download or change the installed package and dependencies. It is a modification operation, not a metadata-only inspection.

C:

pip freeze lists installed distributions and pinned versions for environment snapshots. It does not report the requested package location or dependency metadata together.

D:

The venv module creates an isolated Python environment. Creating another environment does not inspect metadata in the already activated one.

E:

Uninstalling removes the package rather than querying it. This would break the project environment instead of providing a read-only inventory.

Question 498

A Python task must catch an expected missing-file condition without swallowing unrelated errors. Which exception should it handle?

  1. FileNotFoundError
  2. with open(path, encoding='utf-8') as f:
  3. pathlib.Path
  4. json.loads(response_text)

Explanation

Correct Answer

A:

FileNotFoundError identifies a missing path during file operations. Catching a broad Exception could hide programming faults.

Incorrect Answers

B:

The with statement uses a context manager to close an opened file reliably; the task involves handling absent files with a specific exception, whereas this addresses another subsystem.

C:

Path objects provide path joining and filesystem operations while handling platform path conventions; its scope differs from handling absent files with a specific exception, so it cannot meet the stated requirement.

D:

json.loads parses JSON text into dictionaries, lists and scalar values; that is not equivalent to handling absent files with a specific exception.

Question 499

A deployment must install precisely the packages in a reviewed requirements file into an activated Python environment. Which command fits?

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

Explanation

Correct Answer

B:

pip reads dependency specifications from the file and installs compatible distributions into the chosen interpreter environment. Pinning versions improves reproducibility.

Incorrect Answers

A:

The activate script adjusts the invoking shell's PATH and environment for the virtual environment; it answers a neighboring question rather than installing dependencies declared in a requirements file.

C:

venv creates a dedicated environment with interpreter paths and site-packages for a project; this does not achieve installing dependencies declared in a requirements file on the system described.

D:

Python's sys.executable identifies the path of the executing interpreter; it will not provide installing dependencies declared in a requirements file under these conditions.

E:

pip freeze lists installed distributions in requirement-style syntax; using it would leave installing dependencies declared in a requirements file unresolved.

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.

CompTIA Linux+ (XK0-006) Practice Tests