Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Variables and Constants
What will be the output of this Swift code?
var greeting = "Hello"
greeting = greeting + ", World!"
print(greeting)
AHello
BError: Cannot concatenate strings
CHello, World!
DWorld!
Step-by-Step Solution
Solution:
  1. Step 1: Analyze string concatenation with mutable variable

    The variable greeting starts as "Hello" and is updated by adding ", World!".
  2. Step 2: Determine print output

    Printing greeting shows the combined string "Hello, World!".
  3. Final Answer:

    Hello, World! -> Option C
  4. Quick Check:

    Mutable string variables can concatenate and update [OK]
Quick Trick: Strings with var can be changed by adding text [OK]
Common Mistakes:
  • Expecting original string
  • Thinking concatenation causes error
  • Printing only added part
  • Confusing with constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes