Computer Science : Evaluating String Expressions

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #46 : Primitive Data Types

What is the best way to print out a String variable into a sentence in Swift (iOS)? 

Possible Answers:

var str = "Hello"

println("This string says str")

var str = "Hello"

println("This string says \(str)")

var str = "Hello"

println("This string says " + str)

var str = "Hello"

println("This string says Hello")

Correct answer:

var str = "Hello"

println("This string says \(str)")

Explanation:

The \(str) is the best way in Swift. This notation allows for a value of a variable to be input into the sentence. The "+" notation is also correct but is not the best way for Swift. The other two answers print out the words "str" and "Hello" instead of the value of the variable. 

Learning Tools by Varsity Tutors

Incompatible Browser

Please upgrade or download one of the following browsers to use Instant Tutoring: