HashiCorp Terraform Associate 004 (HashiCorp Certified: Terraform Associate (004)) Practice Test 103

Question 1020

A Terraform 1.12 configuration evaluates element(split("|", "web|api|db"), 4). Which result follows from the expression as written?

  1. "api"
  2. "web"
  3. "db"
  4. "index out of range"

Explanation

Correct Answer

A:

For the specific expression element(split("|", "web|api|db"), 4), First, split divides a pipe-delimited value at each separator; The list has 3 entries, so element wraps index 4 modulo 3 and selects zero-based position one.

Incorrect Answers

B:

For the concrete expression element(split("|", "web|api|db"), 4), index wraparound lands at position one, not the first position of the inner list; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

C:

For the concrete expression element(split("|", "web|api|db"), 4), the final list member is not selected; the effective index is one; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

D:

For the concrete expression element(split("|", "web|api|db"), 4), element wraps a positive index instead of failing like ordinary square-bracket indexing; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

Question 1021

A Terraform 1.12 configuration evaluates slice(split("|", "web|api|db"), 1, 3). Which result follows from the expression as written?

  1. ["db", "api"]
  2. ["api", "db"]
  3. ["web", "api", "db"]
  4. ["web", "api"]

Explanation

Correct Answer

B:

For the specific expression slice(split("|", "web|api|db"), 1, 3), First, split divides a pipe-delimited value at each separator; slice begins at zero-based index one and continues through the final member, stopping before the exclusive end index 3.

Incorrect Answers

A:

For the concrete expression slice(split("|", "web|api|db"), 1, 3), slice preserves order rather than reversing the extracted portion; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

C:

For the concrete expression slice(split("|", "web|api|db"), 1, 3), this incorrectly keeps the first list member, although the slice begins at index one; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

D:

For the concrete expression slice(split("|", "web|api|db"), 1, 3), this takes an initial prefix rather than starting with the second entry; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

Question 1022

A Terraform runbook describes the mechanism "terraform fmt -check". Which responsibility should a reviewer associate with it?

  1. reduce insignificant whitespace changes across developers
  2. fail a style gate without modifying source files
  3. make formatting a pre-merge quality gate
  4. change whitespace and presentation without intending resource changes

Explanation

Correct Answer

B:

The practical result of terraform fmt -check, used to fail a style gate without modifying source files, is: terraform fmt -check tests whether files are already formatted and returns an exit status. A qualification for this effect is: The check does not fix configuration automatically.

Incorrect Answers

A:

That outcome applies to Formatting and code review noise, intended to reduce insignificant whitespace changes across developers, rather than the question's feature terraform fmt -check: Canonical formatting minimizes differences due solely to manual layout preferences. Unlike Formatting and code review noise, terraform fmt -check is intended to fail a style gate without modifying source files, not to reduce insignificant whitespace changes across developers.

C:

That outcome applies to HCL style consistency in CI, intended to make formatting a pre-merge quality gate, rather than the question's feature terraform fmt -check: A pipeline can run fmt -check before validate and plan to enforce consistent source layout. Unlike HCL style consistency in CI, terraform fmt -check is intended to fail a style gate without modifying source files, not to make formatting a pre-merge quality gate.

D:

That outcome applies to Formatting preserved semantics, intended to change whitespace and presentation without intending resource changes, rather than the question's feature terraform fmt -check: terraform fmt standardizes language presentation instead of performing infrastructure reconciliation. Unlike Formatting preserved semantics, terraform fmt -check is intended to fail a style gate without modifying source files, not to change whitespace and presentation without intending resource changes.

Question 1023

A Terraform 1.12 configuration evaluates format("primary=%s", element(split("|", "web|api|db"), 0)). Which result follows from the expression as written?

  1. "primary=api"
  2. "web"
  3. "secondary=web"
  4. "primary=web"

Explanation

Correct Answer

D:

For the specific expression format("primary=%s", element(split("|", "web|api|db"), 0)), First, split divides a pipe-delimited value at each separator; element at zero selects the first member, and the single %s placeholder in format substitutes that member after the literal primary= prefix.

Incorrect Answers

A:

For the concrete expression format("primary=%s", element(split("|", "web|api|db"), 0)), this uses the second list element; element at index zero selects the first value; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

B:

For the concrete expression format("primary=%s", element(split("|", "web|api|db"), 0)), the format string has a literal primary= prefix that must appear in the result; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

C:

For the concrete expression format("primary=%s", element(split("|", "web|api|db"), 0)), the prefix is specified literally as primary= rather than secondary=; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

Question 1024

Before adopting terraform fmt -check to fail a style gate without modifying source files, which statement sets an accurate boundary?

  1. A formatted file can still describe unsafe infrastructure operations.
  2. The check does not fix configuration automatically.
  3. Operators should still review unexpected source diffs from any automated formatter.
  4. Style compliance alone cannot prove semantic correctness.

Explanation

Correct Answer

B:

For terraform fmt -check in the task to fail a style gate without modifying source files, the important caution is: The check does not fix configuration automatically. The reason this matters is its actual behavior: terraform fmt -check tests whether files are already formatted and returns an exit status.

Incorrect Answers

A:

The proposed warning belongs to Formatting and code review noise, which serves the different goal to reduce insignificant whitespace changes across developers, rather than to terraform fmt -check: Canonical formatting minimizes differences due solely to manual layout preferences. The question concerns terraform fmt -check acting to fail a style gate without modifying source files, not a limit of Formatting and code review noise.

C:

The proposed warning belongs to Formatting preserved semantics, which serves the different goal to change whitespace and presentation without intending resource changes, rather than to terraform fmt -check: terraform fmt standardizes language presentation instead of performing infrastructure reconciliation. The question concerns terraform fmt -check acting to fail a style gate without modifying source files, not a limit of Formatting preserved semantics.

D:

The proposed warning belongs to HCL style consistency in CI, which serves the different goal to make formatting a pre-merge quality gate, rather than to terraform fmt -check: A pipeline can run fmt -check before validate and plan to enforce consistent source layout. The question concerns terraform fmt -check acting to fail a style gate without modifying source files, not a limit of HCL style consistency in CI.

Question 1025

A Terraform 1.12 configuration evaluates length(compact(["api", "", "edge", ""])). Which result follows from the expression as written?

  1. 0
  2. 2
  3. 1
  4. 4

Explanation

Correct Answer

B:

For the specific expression length(compact(["api", "", "edge", ""])), First, compact eliminates empty strings but retains the nonempty input order; that produces exactly 2 elements; The outer length call counts those resulting elements rather than the original textual arguments.

Incorrect Answers

A:

For the concrete expression length(compact(["api", "", "edge", ""])), the inner expression is not empty: it produces 2 elements; the inner stage produces ["api", "edge"] before the outer operation is applied.

C:

For the concrete expression length(compact(["api", "", "edge", ""])), this drops one legitimate list element; the inner operation returns 2 values; the inner stage produces ["api", "edge"] before the outer operation is applied.

D:

For the concrete expression length(compact(["api", "", "edge", ""])), the inner collection has 2 values, not 4; length counts the resulting collection only; the inner stage produces ["api", "edge"] before the outer operation is applied.

Question 1026

A Terraform rollout has two separate requirements: first, fail a style gate without modifying source files; second, format Terraform configurations in nested local directories. Which assignment of mechanisms correctly handles both responsibilities?

  1. First use terraform fmt -check; then use terraform fmt -recursive.
  2. First use terraform fmt -recursive; then use terraform fmt -check.
  3. First use terraform fmt -check; then use HCL style consistency in CI.
  4. First use HCL style consistency in CI; then use Formatting and code review noise.
  5. First use Formatting and code review noise; then use terraform fmt -recursive.

Explanation

Correct Answer

A:

The first requirement, to fail a style gate without modifying source files, is handled by terraform fmt -check: terraform fmt -check tests whether files are already formatted and returns an exit status. The second requirement, to format Terraform configurations in nested local directories, is handled by terraform fmt -recursive: The recursive flag expands formatting beyond just the current directory. Each of these mechanisms has a distinct responsibility.

Incorrect Answers

B:

For the requested tasks to fail a style gate without modifying source files and format Terraform configurations in nested local directories, the first candidate terraform fmt -recursive addresses format Terraform configurations in nested local directories; the second candidate terraform fmt -check addresses fail a style gate without modifying source files. This mapping fails because the first mechanism primarily supports the task to format Terraform configurations in nested local directories and the second mechanism primarily supports the task to fail a style gate without modifying source files; the actual requirements are fail a style gate without modifying source files first and format Terraform configurations in nested local directories second.

C:

For the requested tasks to fail a style gate without modifying source files and format Terraform configurations in nested local directories, the first candidate terraform fmt -check addresses fail a style gate without modifying source files; the second candidate HCL style consistency in CI addresses make formatting a pre-merge quality gate. This mapping fails because the second mechanism primarily supports the task to make formatting a pre-merge quality gate; the actual requirements are fail a style gate without modifying source files first and format Terraform configurations in nested local directories second.

D:

For the requested tasks to fail a style gate without modifying source files and format Terraform configurations in nested local directories, the first candidate HCL style consistency in CI addresses make formatting a pre-merge quality gate; the second candidate Formatting and code review noise addresses reduce insignificant whitespace changes across developers. This mapping fails because the first mechanism primarily supports the task to make formatting a pre-merge quality gate and the second mechanism primarily supports the task to reduce insignificant whitespace changes across developers; the actual requirements are fail a style gate without modifying source files first and format Terraform configurations in nested local directories second.

E:

For the requested tasks to fail a style gate without modifying source files and format Terraform configurations in nested local directories, the first candidate Formatting and code review noise addresses reduce insignificant whitespace changes across developers; the second candidate terraform fmt -recursive addresses format Terraform configurations in nested local directories. This mapping fails because the first mechanism primarily supports the task to reduce insignificant whitespace changes across developers; the actual requirements are fail a style gate without modifying source files first and format Terraform configurations in nested local directories second.

Question 1027

A Terraform 1.12 configuration evaluates join("/", compact(["api", "", "edge", ""])). Which result follows from the expression as written?

  1. "apiedge"
  2. "edge/api"
  3. "api/edge/edge"
  4. "api/edge"

Explanation

Correct Answer

D:

For the specific expression join("/", compact(["api", "", "edge", ""])), First, compact eliminates empty strings but retains the nonempty input order; The outer join uses a slash only between consecutive list members and preserves their computed order.

Incorrect Answers

A:

For the concrete expression join("/", compact(["api", "", "edge", ""])), this omits the slash delimiters requested by join; the inner stage produces ["api", "edge"] before the outer operation is applied.

B:

For the concrete expression join("/", compact(["api", "", "edge", ""])), this reverses the members even though join preserves the order returned by the inner expression; the inner stage produces ["api", "edge"] before the outer operation is applied.

C:

For the concrete expression join("/", compact(["api", "", "edge", ""])), join must not repeat the last item after the list has been joined; the inner stage produces ["api", "edge"] before the outer operation is applied.

Question 1028

A Terraform 1.12 configuration evaluates reverse(compact(["api", "", "edge", ""])). Which result follows from the expression as written?

  1. ["edge", "api"]
  2. ["api", "edge"]
  3. ["edge"]
  4. []

Explanation

Correct Answer

A:

For the specific expression reverse(compact(["api", "", "edge", ""])), First, compact eliminates empty strings but retains the nonempty input order; reverse then returns the exact opposite element order, without sorting or removing members.

Incorrect Answers

B:

For the concrete expression reverse(compact(["api", "", "edge", ""])), this leaves the inner result unchanged and skips the outer reverse call; the inner stage produces ["api", "edge"] before the outer operation is applied.

C:

For the concrete expression reverse(compact(["api", "", "edge", ""])), the reverse function does not discard an element of the input list; the inner stage produces ["api", "edge"] before the outer operation is applied.

D:

For the concrete expression reverse(compact(["api", "", "edge", ""])), reversing a nonempty list does not create an empty list; the inner stage produces ["api", "edge"] before the outer operation is applied.

Question 1029

A Terraform operator is specifically tasked to display differences that canonical formatting would introduce. Which mechanism most directly meets this particular need?

  1. terraform fmt -recursive
  2. terraform fmt -check
  3. HCL style consistency in CI
  4. terraform fmt -diff

Explanation

Correct Answer

D:

The task is to display differences that canonical formatting would introduce; terraform fmt -diff directly supports that requirement: terraform fmt -diff shows a style diff for files that need formatting. Its relevant boundary is: A style diff is not an infrastructure change plan.

Incorrect Answers

A:

terraform fmt -recursive is designed to format Terraform configurations in nested local directories, whereas this task is to display differences that canonical formatting would introduce: The recursive flag expands formatting beyond just the current directory. Against terraform fmt -diff, its limitation is: It does not fetch remote modules to reformat their source repositories.

B:

terraform fmt -check is designed to fail a style gate without modifying source files, whereas this task is to display differences that canonical formatting would introduce: terraform fmt -check tests whether files are already formatted and returns an exit status. Against terraform fmt -diff, its limitation is: The check does not fix configuration automatically.

C:

HCL style consistency in CI addresses the different requirement to make formatting a pre-merge quality gate: A pipeline can run fmt -check before validate and plan to enforce consistent source layout; this does not by itself satisfy the requested task to display differences that canonical formatting would introduce. For the stated need, terraform fmt -diff is the more direct mechanism.

Question 1030

A Terraform 1.12 configuration evaluates sort(compact(["api", "", "edge", ""])). Which result follows from the expression as written?

  1. ["api"]
  2. []
  3. ["api", "edge"]
  4. ["edge", "api"]

Explanation

Correct Answer

C:

For the specific expression sort(compact(["api", "", "edge", ""])), First, compact eliminates empty strings but retains the nonempty input order; Terraform sort then compares the resulting strings lexicographically in ascending order; it neither drops values nor preserves a non-sorted arrangement.

Incorrect Answers

A:

For the concrete expression sort(compact(["api", "", "edge", ""])), sorting does not remove the final element of the list; the inner stage produces ["api", "edge"] before the outer operation is applied.

B:

For the concrete expression sort(compact(["api", "", "edge", ""])), the outer sort does not empty the nonempty collection; the inner stage produces ["api", "edge"] before the outer operation is applied.

D:

For the concrete expression sort(compact(["api", "", "edge", ""])), a reversal is not guaranteed to produce ascending lexical order; the inner stage produces ["api", "edge"] before the outer operation is applied.

HashiCorp Certified: Terraform Associate (004) (Terraform Associate 004) Practice Tests