B. Syntax error due to missing slashes in replacement.
C. It replaces 'data' with 'txt' but misses the dot.
D. It replaces 'data' with 'txt' but changes the file extension incorrectly.
Solution
Step 1: Check the replacement syntax
The replacement syntax must be ${var/old/new} with slashes separating parts. Here, ${file/data/txt} is correct syntax.
Step 2: Analyze the replacement effect
The code replaces 'data' with 'txt', so 'data.txt' becomes 'txt.txt'. There is no syntax error.
Step 3: Identify the logical issue
While the code executes correctly, the replacement of 'data' with 'txt' produces 'txt.txt', which alters the filename in a way that incorrectly impacts the intended file extension handling.
Final Answer:
It replaces 'data' with 'txt' but changes the file extension incorrectly. -> Option D
Quick Check:
Replacement changes 'data' to 'txt', affecting filename [OK]
Hint: Check slashes and replaced parts carefully for syntax and effect [OK]
Common Mistakes:
Assuming syntax error when slashes are correct
Ignoring that replacement changes filename parts
Confusing replacement with substring extraction
5. You have a variable sentence="apple banana apple cherry apple". How do you replace only the second occurrence of 'apple' with 'orange' using bash string replacement?
hard
A. Use parameter expansion with substring and manual replacement.
B. Use ${sentence//apple/orange} to replace all.
C. Use ${sentence/apple/orange} twice.
D. Use ${sentence/apple/orange} once; it replaces second occurrence automatically.
Solution
Step 1: Understand limitation of ${var/old/new}
This syntax replaces only the first occurrence, no direct way to replace the second occurrence.
Step 2: Consider manual approach
To replace the second occurrence, you must split or manipulate the string manually using substring extraction and concatenation.
Step 3: Confirm the approach
Bash parameter expansion with single or double slashes cannot target specific non-first occurrences; manual string manipulation using substrings is required.
Final Answer:
Use parameter expansion with substring and manual replacement. -> Option A
Quick Check:
Only manual substring methods can target second occurrence [OK]
Hint: Single slash replaces first only; second needs manual substring handling [OK]
Common Mistakes:
Assuming single replacement syntax can target second occurrence
Using double slash to replace all instead of just second
Expecting repeated use of ${var/old/new} to replace second occurrence
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.