Bird
0
0

Find the error and fix it in this Swift code:

medium📝 Debug Q7 of 15
Swift - Data Types
Find the error and fix it in this Swift code:
let a: Int = 8
let b: Double = 2.0
let c: Int = a + Int(b)
AChange c to Double type
BConvert a to Double before addition
CRemove Int conversion on b
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze type conversions

    b is converted to Int explicitly, so a + Int(b) is valid Int addition.
  2. Step 2: Check variable c's type

    c is declared as Int, matching the addition result type.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    Explicit conversion matches variable types = No error [OK]
Quick Trick: Explicitly convert types to match variable declarations [OK]
Common Mistakes:
  • Assuming implicit conversion is needed
  • Changing variable types unnecessarily
  • Removing required explicit conversions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes