Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Data Types

Identify the error in this Swift code snippet:

var x: Int = 10.5
var y: Float = 20
ACannot assign integer 20 to Float variable
BCannot assign decimal 10.5 to Int variable
CMissing semicolon after variable declarations
DVariable names are invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check variable type and assigned value

    Variable x is Int but assigned 10.5 which is a decimal number.
  2. Step 2: Validate other assignments

    Variable y is Float and assigned 20 (an integer), which is allowed by implicit conversion.
  3. Final Answer:

    Cannot assign decimal 10.5 to Int variable -> Option B
  4. Quick Check:

    Int cannot hold decimals [OK]
Quick Trick: Int variables cannot hold decimal values [OK]
Common Mistakes:
  • Thinking Float cannot hold integer values
  • Expecting semicolons in Swift
  • Assuming variable names are invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes