Consider this Swift code where a string is assigned and then modified. What will be printed?
var greeting = "Hello" var copy = greeting greeting += ", world!" print(copy)
Remember that strings in Swift are value types, so copying creates a new independent value.
Strings in Swift are value types. When you assign greeting to copy, it copies the value. Changing greeting later does not affect copy. So copy remains "Hello".
What will this Swift code print?
let original = "Swift" var copy = original copy.append(" Lang") print(original)
Think about whether modifying copy changes original.
Since strings are value types, copy is a separate copy. Modifying copy does not affect original. So original stays "Swift".
Examine this Swift code. Why does it print two different strings?
var s1 = "Hi" var s2 = s1 s2 = "Hello" print(s1) print(s2)
Think about how value types behave when assigned and changed.
Strings in Swift are value types. Assigning s1 to s2 copies the value. Changing s2 does not affect s1. So printing s1 shows "Hi" and s2 shows "Hello".
Choose the correct statement about Swift strings and value type behavior.
Recall how value types behave on assignment.
Swift strings are value types. Assigning a string to a new variable copies the value, creating a separate instance. Modifying one does not affect the other.
Analyze this Swift code and determine what it prints.
var a = "Start" var b = a b += " End" print(a) print(b)
Remember that strings are copied on assignment and changes to one do not affect the other.
Variable b is a copy of a. Appending " End" to b does not change a. So a prints "Start" and b prints "Start End".