Bird
0
0

Find the error in this Swift code snippet:

medium📝 Debug Q6 of 15
Swift - Data Types

Find the error in this Swift code snippet:

let pi: Float = 3.14159
let radius = 5
let area = pi * radius * radius
print(area)
ANo error, code runs fine
Bpi should be Double instead of Float
CMissing type annotation for radius
Dradius should be Float for multiplication with pi
Step-by-Step Solution
Solution:
  1. Step 1: Check type compatibility in multiplication

    pi is Float, radius is Int; Swift does not allow direct multiplication of Float and Int.
  2. Step 2: Identify fix

    Convert radius to Float or declare radius as Float to fix type mismatch.
  3. Final Answer:

    radius should be Float for multiplication with pi -> Option D
  4. Quick Check:

    Type mismatch in multiplication = radius needs Float [OK]
Quick Trick: Match types before arithmetic operations [OK]
Common Mistakes:
  • Ignoring type mismatch
  • Changing pi type unnecessarily
  • Assuming implicit conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes