Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Variables and Constants
What will be the output of the following Swift code?
let name = "Anna"
let age = 30
print("\(name) is \(age) years old.")
AAnna is 30 years old.
Bname is age years old.
C\(name) is \(age) years old.
DAnna is \(age) years old.
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable values

    name = "Anna", age = 30.
  2. Step 2: Apply string interpolation

    "\(name)" becomes "Anna", "\(age)" becomes "30" in the string.
  3. Final Answer:

    Anna is 30 years old. -> Option A
  4. Quick Check:

    Interpolation replaces variables with values [OK]
Quick Trick: Replace \(variable) with its value inside the string [OK]
Common Mistakes:
  • Printing the literal \(name) instead of value
  • Confusing variable names with strings
  • Missing backslash causing no interpolation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes