Challenge - 5 Problems
Arithmetic Expansion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of arithmetic expansion with mixed operators
What is the output of this bash command?
echo $((3 + 4 * 2 - 1))
Bash Scripting
echo $((3 + 4 * 2 - 1))
Attempts:
2 left
💡 Hint
Remember the order of operations: multiplication before addition and subtraction.
✗ Incorrect
Multiplication happens first: 4 * 2 = 8. Then addition and subtraction: 3 + 8 - 1 = 10. So output is 10.
💻 Command Output
intermediate2:00remaining
Result of arithmetic expansion with variable substitution
Given
x=5, what is the output of this command?x=5 echo $((x * 3 + 2))
Bash Scripting
x=5 echo $((x * 3 + 2))
Attempts:
2 left
💡 Hint
Substitute x with 5 and calculate.
✗ Incorrect
x=5, so 5 * 3 + 2 = 15 + 2 = 17.
📝 Syntax
advanced2:00remaining
Identify the syntax error in arithmetic expansion
Which option contains a syntax error in arithmetic expansion?
Attempts:
2 left
💡 Hint
Look for incomplete expressions inside $(( )).
✗ Incorrect
Option A has an incomplete expression '3 + ' which causes a syntax error.
🔧 Debug
advanced2:00remaining
Why does this arithmetic expansion output 0?
Given
a=3 and b=5, what is the output of this command and why?a=3 b=5 echo $((a / b))
Bash Scripting
a=3 b=5 echo $((a / b))
Attempts:
2 left
💡 Hint
Think about how bash handles division with integers.
✗ Incorrect
Bash arithmetic expansion uses integer math, so 3/5 is 0.6 but truncated to 0.
🚀 Application
expert3:00remaining
Calculate factorial using arithmetic expansion in a loop
What is the output of this bash script?
n=4 fact=1 for ((i=1; i<=n; i++)); do fact=$((fact * i)) done echo $fact
Bash Scripting
n=4 fact=1 for ((i=1; i<=n; i++)); do fact=$((fact * i)) done echo $fact
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
✗ Incorrect
The loop multiplies fact by i from 1 to 4: 1*1=1, 1*2=2, 2*3=6, 6*4=24.