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

Question 1010

Before continuing an infrastructure rollout, the operator must rewrite Terraform configuration into its canonical layout. Which mechanism most directly meets this particular need?

  1. terraform fmt command
  2. terraform fmt -recursive
  3. terraform fmt -diff
  4. terraform fmt -check

Explanation

Correct Answer

A:

The task is to rewrite Terraform configuration into its canonical layout; terraform fmt command directly supports that requirement: terraform fmt formats Terraform language files consistently, including spacing and alignment. Its relevant boundary is: Formatting does not validate external credentials or apply changes.

Incorrect Answers

B:

terraform fmt -recursive is designed to format Terraform configurations in nested local directories, whereas this task is to rewrite Terraform configuration into its canonical layout: The recursive flag expands formatting beyond just the current directory. Against terraform fmt command, its limitation is: It does not fetch remote modules to reformat their source repositories.

C:

terraform fmt -diff is designed to display differences that canonical formatting would introduce, whereas this task is to rewrite Terraform configuration into its canonical layout: terraform fmt -diff shows a style diff for files that need formatting. Against terraform fmt command, its limitation is: A style diff is not an infrastructure change plan.

D:

terraform fmt -check is designed to fail a style gate without modifying source files, whereas this task is to rewrite Terraform configuration into its canonical layout: terraform fmt -check tests whether files are already formatted and returns an exit status. Against terraform fmt command, its limitation is: The check does not fix configuration automatically.

Question 1011

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

  1. "webapidb"
  2. "db/api/web"
  3. "web/api/db/db"
  4. "web/api/db"

Explanation

Correct Answer

D:

For the specific expression join("/", split("|", "web|api|db")), First, split divides a pipe-delimited value at each separator; The outer join uses a slash only between consecutive list members and preserves their computed order.

Incorrect Answers

A:

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

B:

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

C:

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

Question 1012

A configuration walkthrough identifies "terraform fmt command". Which task is it principally intended to support?

  1. fail a style gate without modifying source files
  2. format Terraform configurations in nested local directories
  3. display differences that canonical formatting would introduce
  4. rewrite Terraform configuration into its canonical layout

Explanation

Correct Answer

D:

The practical result of terraform fmt command, used to rewrite Terraform configuration into its canonical layout, is: terraform fmt formats Terraform language files consistently, including spacing and alignment. A qualification for this effect is: Formatting does not validate external credentials or apply changes.

Incorrect Answers

A:

That outcome applies to terraform fmt -check, intended to fail a style gate without modifying source files, rather than the question's feature terraform fmt command: terraform fmt -check tests whether files are already formatted and returns an exit status. Unlike terraform fmt -check, terraform fmt command is intended to rewrite Terraform configuration into its canonical layout, not to fail a style gate without modifying source files.

B:

That outcome applies to terraform fmt -recursive, intended to format Terraform configurations in nested local directories, rather than the question's feature terraform fmt command: The recursive flag expands formatting beyond just the current directory. Unlike terraform fmt -recursive, terraform fmt command is intended to rewrite Terraform configuration into its canonical layout, not to format Terraform configurations in nested local directories.

C:

That outcome applies to terraform fmt -diff, intended to display differences that canonical formatting would introduce, rather than the question's feature terraform fmt command: terraform fmt -diff shows a style diff for files that need formatting. Unlike terraform fmt -diff, terraform fmt command is intended to rewrite Terraform configuration into its canonical layout, not to display differences that canonical formatting would introduce.

Question 1013

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

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

Explanation

Correct Answer

B:

For the specific expression reverse(split("|", "web|api|db")), First, split divides a pipe-delimited value at each separator; reverse then returns the exact opposite element order, without sorting or removing members.

Incorrect Answers

A:

For the concrete expression reverse(split("|", "web|api|db")), this rotates the reversed list rather than returning the exact reversed sequence; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

C:

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

D:

For the concrete expression reverse(split("|", "web|api|db")), alphabetic sorting is not the same as reversing the sequence produced by the inner expression; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

Question 1014

An engineer tests the flatten behavior in terraform console. What is the result of length(flatten([[3, 5], [7], [9, 11, 13]]))?

  1. 2
  2. 3
  3. 6
  4. 5

Explanation

Correct Answer

C:

Evaluating the actual call length(flatten([[3, 5], [7], [9, 11, 13]])): flatten removes nested list boundaries, giving six scalar integers before length counts them.

Incorrect Answers

A:

Given the specific operands supplied to the Terraform expression length(flatten([[3, 5], [7], [9, 11, 13]])), two only counts the first inner list, overlooking the other elements.

B:

Given the specific operands supplied to the Terraform expression length(flatten([[3, 5], [7], [9, 11, 13]])), three is the number of original inner lists, not the flattened element count.

D:

Given the specific operands supplied to the Terraform expression length(flatten([[3, 5], [7], [9, 11, 13]])), five misses one member of the last inner list; flatten retains all six.

Question 1015

The team is documenting risks of terraform fmt command while planning to rewrite Terraform configuration into its canonical layout. Which caveat specifically applies to that mechanism?

  1. The check does not fix configuration automatically.
  2. Formatting does not validate external credentials or apply changes.
  3. A style diff is not an infrastructure change plan.
  4. It does not fetch remote modules to reformat their source repositories.

Explanation

Correct Answer

B:

For terraform fmt command in the task to rewrite Terraform configuration into its canonical layout, the important caution is: Formatting does not validate external credentials or apply changes. The reason this matters is its actual behavior: terraform fmt formats Terraform language files consistently, including spacing and alignment.

Incorrect Answers

A:

The proposed warning belongs to terraform fmt -check, which serves the different goal to fail a style gate without modifying source files, rather than to terraform fmt command: terraform fmt -check tests whether files are already formatted and returns an exit status. The question concerns terraform fmt command acting to rewrite Terraform configuration into its canonical layout, not a limit of terraform fmt -check.

C:

The proposed warning belongs to terraform fmt -diff, which serves the different goal to display differences that canonical formatting would introduce, rather than to terraform fmt command: terraform fmt -diff shows a style diff for files that need formatting. The question concerns terraform fmt command acting to rewrite Terraform configuration into its canonical layout, not a limit of terraform fmt -diff.

D:

The proposed warning belongs to terraform fmt -recursive, which serves the different goal to format Terraform configurations in nested local directories, rather than to terraform fmt command: The recursive flag expands formatting beyond just the current directory. The question concerns terraform fmt command acting to rewrite Terraform configuration into its canonical layout, not a limit of terraform fmt -recursive.

Question 1016

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

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

Explanation

Correct Answer

A:

For the specific expression sort(split("|", "web|api|db")), First, split divides a pipe-delimited value at each separator; Terraform sort then compares the resulting strings lexicographically in ascending order; it neither drops values nor preserves a non-sorted arrangement.

Incorrect Answers

B:

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

C:

For the concrete expression sort(split("|", "web|api|db")), this retains the source order rather than explicitly sorting the result (unless it was already sorted); the inner stage produces ["web", "api", "db"] before the outer operation is applied.

D:

For the concrete expression sort(split("|", "web|api|db")), this puts strings in descending lexical order rather than ascending order; the inner stage produces ["web", "api", "db"] before the outer operation is applied.

Question 1017

A Terraform rollout has two separate requirements: first, rewrite Terraform configuration into its canonical layout; second, display differences that canonical formatting would introduce. Which assignment of mechanisms correctly handles both responsibilities?

  1. First use terraform fmt -recursive; then use HCL style consistency in CI.
  2. First use terraform fmt command; then use terraform fmt -diff.
  3. First use HCL style consistency in CI; then use terraform fmt -diff.
  4. First use terraform fmt -diff; then use terraform fmt command.
  5. First use terraform fmt command; then use terraform fmt -recursive.

Explanation

Correct Answer

B:

The first requirement, to rewrite Terraform configuration into its canonical layout, is handled by terraform fmt command: terraform fmt formats Terraform language files consistently, including spacing and alignment. The second requirement, to display differences that canonical formatting would introduce, is handled by terraform fmt -diff: terraform fmt -diff shows a style diff for files that need formatting. Each of these mechanisms has a distinct responsibility.

Incorrect Answers

A:

For the requested tasks to rewrite Terraform configuration into its canonical layout and display differences that canonical formatting would introduce, the first candidate terraform fmt -recursive addresses format Terraform configurations in nested local directories; the second candidate HCL style consistency in CI addresses make formatting a pre-merge quality gate. 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 make formatting a pre-merge quality gate; the actual requirements are rewrite Terraform configuration into its canonical layout first and display differences that canonical formatting would introduce second.

C:

For the requested tasks to rewrite Terraform configuration into its canonical layout and display differences that canonical formatting would introduce, the first candidate HCL style consistency in CI addresses make formatting a pre-merge quality gate; the second candidate terraform fmt -diff addresses display differences that canonical formatting would introduce. This mapping fails because the first mechanism primarily supports the task to make formatting a pre-merge quality gate; the actual requirements are rewrite Terraform configuration into its canonical layout first and display differences that canonical formatting would introduce second.

D:

For the requested tasks to rewrite Terraform configuration into its canonical layout and display differences that canonical formatting would introduce, the first candidate terraform fmt -diff addresses display differences that canonical formatting would introduce; the second candidate terraform fmt command addresses rewrite Terraform configuration into its canonical layout. This mapping fails because the first mechanism primarily supports the task to display differences that canonical formatting would introduce and the second mechanism primarily supports the task to rewrite Terraform configuration into its canonical layout; the actual requirements are rewrite Terraform configuration into its canonical layout first and display differences that canonical formatting would introduce second.

E:

For the requested tasks to rewrite Terraform configuration into its canonical layout and display differences that canonical formatting would introduce, the first candidate terraform fmt command addresses rewrite Terraform configuration into its canonical layout; the second candidate terraform fmt -recursive addresses format Terraform configurations in nested local directories. This mapping fails because the second mechanism primarily supports the task to format Terraform configurations in nested local directories; the actual requirements are rewrite Terraform configuration into its canonical layout first and display differences that canonical formatting would introduce second.

Question 1018

For Terraform string processing with upper and trimspace, evaluate upper(trimspace(" api ")). Which output is correct?

  1. " api " (with original casing and padding)
  2. " API " (with surrounding spaces)
  3. "API"
  4. "api" (lowercase retained)

Explanation

Correct Answer

C:

Evaluating the actual call upper(trimspace(" api ")): The inner trimspace removes surrounding spaces before upper converts api to uppercase, giving the unpadded result API.

Incorrect Answers

A:

Given the specific operands supplied to the Terraform expression upper(trimspace(" api ")), this leaves both leading and trailing spaces and the lowercase input untouched, ignoring both functions.

B:

Given the specific operands supplied to the Terraform expression upper(trimspace(" api ")), this includes padding removed by the inner trimspace call, so the outer upper cannot restore it.

D:

Given the specific operands supplied to the Terraform expression upper(trimspace(" api ")), this retains lowercase letters despite the explicit outer upper transformation.

Question 1019

In a reusable deployment, the primary objective is to fail a style gate without modifying source files. Which mechanism most directly meets this particular need?

  1. Formatting and code review noise
  2. terraform fmt -check
  3. HCL style consistency in CI
  4. Formatting preserved semantics

Explanation

Correct Answer

B:

The task is to fail a style gate without modifying source files; terraform fmt -check directly supports that requirement: terraform fmt -check tests whether files are already formatted and returns an exit status. Its relevant boundary is: The check does not fix configuration automatically.

Incorrect Answers

A:

Formatting and code review noise is designed to reduce insignificant whitespace changes across developers, whereas this task is to fail a style gate without modifying source files: Canonical formatting minimizes differences due solely to manual layout preferences. Against terraform fmt -check, its limitation is: A formatted file can still describe unsafe infrastructure operations.

C:

HCL style consistency in CI is designed to make formatting a pre-merge quality gate, whereas this task is to fail a style gate without modifying source files: A pipeline can run fmt -check before validate and plan to enforce consistent source layout. Against terraform fmt -check, its limitation is: Style compliance alone cannot prove semantic correctness.

D:

Formatting preserved semantics is designed to change whitespace and presentation without intending resource changes, whereas this task is to fail a style gate without modifying source files: terraform fmt standardizes language presentation instead of performing infrastructure reconciliation. Against terraform fmt -check, its limitation is: Operators should still review unexpected source diffs from any automated formatter.

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.

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