0
0
Bash Scriptingscripting~20 mins

Integer variables in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Integer Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of integer arithmetic in Bash
What is the output of this Bash script?
Bash Scripting
a=5
b=3
c=$((a + b * 2))
echo $c
A11
B16
C13
D8
Attempts:
2 left
💡 Hint
Remember that multiplication has higher precedence than addition.
📝 Syntax
intermediate
2:00remaining
Correct integer assignment in Bash
Which option correctly assigns the integer value 10 to variable x in Bash?
Ax=10
Bx := 10
Cx == 10
Dlet x = 10
Attempts:
2 left
💡 Hint
In Bash, assignment does not use spaces around = and no extra symbols.
🔧 Debug
advanced
2:00remaining
Why does this Bash script fail to add integers?
Given this script: x=5 y=3 z=$x + $y echo $z What is the output and why?
Bash Scripting
x=5
y=3
z="$x + $y"
echo $z
A53
B8
CSyntax error
D5 + 3
Attempts:
2 left
💡 Hint
Check how arithmetic is done in Bash and how variables are expanded.
🚀 Application
advanced
3:00remaining
Calculate factorial using integer variables in Bash
Which script correctly calculates factorial of 4 and prints the result?
A
n=4
fact=1
while [ $n -gt 0 ]; do
  fact=$((fact * n))
  n=$((n - 1))
done
echo $fact
B
n=4
fact=1
for ((i=1; i<=n; i++)); do
  fact=$((fact * i))
done
echo $fact
C
n=4
fact=1
for i in $(seq 1 $n); do
  fact=$((fact + i))
done
echo $fact
D
n=4
fact=1
for i in {1..n}; do
  fact=$((fact * i))
done
echo $fact
Attempts:
2 left
💡 Hint
Check the loop syntax and arithmetic operation used for factorial.
🧠 Conceptual
expert
2:00remaining
Integer variable behavior in Bash with arithmetic expansion
Consider this Bash snippet: x=10 y=3 z=$((x / y)) echo $z What is the output and why?
A3.3333 because Bash supports floating point division
B4 because Bash rounds up the division result
C3 because Bash integer division truncates the decimal part
DError because division is not supported in Bash arithmetic
Attempts:
2 left
💡 Hint
Bash arithmetic expansion works with integers only.