Bird
0
0

You want to calculate the sum of squares of two variables a and b in bash. Which command correctly does this using arithmetic expansion?

hard🚀 Application Q8 of 15
Bash Scripting - Quoting and Expansion
You want to calculate the sum of squares of two variables a and b in bash. Which command correctly does this using arithmetic expansion?
Aresult=$((a^2 + b^2))
Bresult=$((a ** 2 + b ** 2))
Cresult=$((a * a + b * b))
Dresult=$((a * 2 + b * 2))
Step-by-Step Solution
Solution:
  1. Step 1: Understand exponentiation in bash

    Use ** for powers, not ^.
  2. Step 2: Evaluate each option

    result=$((a ** 2 + b ** 2)) correctly calculates a squared plus b squared. result=$((a * a + b * b)) multiplies variables by themselves but lacks clarity. result=$((a^2 + b^2)) uses ^ which is bitwise XOR, not power. result=$((a * 2 + b * 2)) doubles variables, not squares.
  3. Final Answer:

    result=$((a ** 2 + b ** 2)) -> Option B
  4. Quick Check:

    Correct power syntax = result=$((a ** 2 + b ** 2)) [OK]
Quick Trick: Use ** for powers, not ^ in bash arithmetic [OK]
Common Mistakes:
MISTAKES
  • Using ^ for power
  • Confusing multiplication with exponentiation
  • Incorrect operator precedence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes