Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Operators and Expressions
Identify the error in this Swift code:
let a: UInt8 = 250
let b: UInt8 = 10
let c = a + b
APossible overflow without using &+ operator
BIncorrect variable types for addition
CMissing semicolon after let c
DUsing + instead of - operator
Step-by-Step Solution
Solution:
  1. Step 1: Check addition of UInt8 values

    Adding 250 + 10 exceeds UInt8 max 255, causing overflow.
  2. Step 2: Identify missing overflow operator

    Using + causes runtime error on overflow; &+ should be used to allow wrapping.
  3. Final Answer:

    Possible overflow without using &+ operator -> Option A
  4. Quick Check:

    UInt8 addition overflow needs &+ [OK]
Quick Trick: Use &+ for addition that may overflow UInt8 [OK]
Common Mistakes:
  • Ignoring overflow possibility
  • Thinking semicolon is required
  • Confusing + with - operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes