Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Operators and Expressions
What will be printed by this Swift code?
let minInt8: Int8 = -128
let result = minInt8 &- 1
print(result)
A-127
B-128
C127
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand Int8 range

    Int8 ranges from -128 to 127.
  2. Step 2: Analyze the operation

    Subtracting 1 from -128 with &- (overflow subtraction) wraps around to 127.
  3. Final Answer:

    127 -> Option C
  4. Quick Check:

    Underflow wraps to max Int8 value [OK]
Quick Trick: Underflow with &- wraps to max Int8 value [OK]
Common Mistakes:
  • Assuming normal subtraction without overflow
  • Expecting runtime error on overflow
  • Confusing &- with regular - operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes