Bird
Raised Fist0
Swiftprogramming~20 mins

Print function for output in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Print Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift print statement?
Look at the code below. What will it print when run?
Swift
print("Hello, \"Swift\" world!")
AHello, 'Swift' world!
BHello, "Swift" world!
CHello, Swift world!
DHello, \"Swift\" world!
Attempts:
2 left
💡 Hint
Remember how escape characters work in strings.
❓ Predict Output
intermediate
2:00remaining
What does this Swift code print?
Check the code and choose the correct output.
Swift
let name = "Anna"
print("Hello, \(name)!")
AHello, Anna!
BHello, \(name)!
CHello, name!
DHello, (name)!
Attempts:
2 left
💡 Hint
Look at how Swift uses \(variable) inside strings.
❓ Predict Output
advanced
2:00remaining
What is the output of this Swift code with multiple print statements?
Analyze the code and select the correct output sequence.
Swift
print("Line 1")
print("Line 2", terminator: " ")
print("Line 3")
A
Line 1
Line 2 Line 3
B
Line 1 Line 2 Line 3
C
Line 1
Line 2
Line 3
D
Line 1 Line 2
Line 3
Attempts:
2 left
💡 Hint
Check how the terminator parameter changes the print behavior.
❓ Predict Output
advanced
2:00remaining
What will this Swift code print?
Consider the code and pick the correct output.
Swift
let number = 5
print("Number is \(number * 2)")
ANumber is 5 * 2
BNumber is number * 2
CNumber is 10
DNumber is \(number * 2)
Attempts:
2 left
💡 Hint
Swift evaluates expressions inside \( ) in strings.
❓ Predict Output
expert
3:00remaining
What is the output of this Swift code using multiline string and print?
Look carefully at the code and select the exact output.
Swift
let poem = """
Roses are red,
Violets are blue,
Swift is fun,
And so are you.
"""
print(poem)
ARoses are red,\nViolets are blue,\nSwift is fun,\nAnd so are you.
BRoses are red, Violets are blue, Swift is fun, And so are you.
C"Roses are red,\nViolets are blue,\nSwift is fun,\nAnd so are you."
D
Roses are red,
Violets are blue,
Swift is fun,
And so are you.
Attempts:
2 left
💡 Hint
Multiline strings keep line breaks as they are.

Practice

(1/5)
1.

What does the print function do in Swift?

easy
A. It shows text or values on the screen.
B. It saves data to a file.
C. It creates a new variable.
D. It deletes a program.

Solution

  1. Step 1: Understand the purpose of print

    The print function is used to display output on the screen, such as text or variable values.
  2. Step 2: Compare options with print's function

    Only It shows text or values on the screen. describes showing text or values on the screen, which matches print's purpose.
  3. Final Answer:

    It shows text or values on the screen. -> Option A
  4. Quick Check:

    print function = display output [OK]
Hint: Remember: print means show on screen [OK]
Common Mistakes:
  • Thinking print saves data
  • Confusing print with variable creation
  • Assuming print deletes data
2.

Which of the following is the correct way to print the text Hello, Swift! in Swift?

?
easy
A. echo "Hello, Swift!"
B. println('Hello, Swift!')
C. print("Hello, Swift!")
D. print Hello, Swift!

Solution

  1. Step 1: Identify correct Swift print syntax

    Swift uses print() with double quotes for strings.
  2. Step 2: Check each option

    print("Hello, Swift!") uses print("Hello, Swift!"), which is correct. Others use wrong functions or missing parentheses/quotes.
  3. Final Answer:

    print("Hello, Swift!") -> Option C
  4. Quick Check:

    Correct print syntax = print("Hello, Swift!") [OK]
Hint: Use print() with double quotes for text [OK]
Common Mistakes:
  • Using single quotes for strings
  • Missing parentheses
  • Using wrong function names
3.

What will be the output of this Swift code?

let name = "Anna"
print("Hello, \(name)!")
medium
A. Error: Invalid syntax
B. Hello, \(name)!
C. Hello, name!
D. Hello, Anna!

Solution

  1. Step 1: Understand string interpolation

    The syntax \(name) inserts the value of variable name into the string.
  2. Step 2: Substitute variable value

    Since name is "Anna", the printed string becomes "Hello, Anna!".
  3. Final Answer:

    Hello, Anna! -> Option D
  4. Quick Check:

    String interpolation = Hello, Anna! [OK]
Hint: Use \(variable) to insert values in strings [OK]
Common Mistakes:
  • Printing variable name literally
  • Forgetting \( ) syntax
  • Syntax errors in string interpolation
4.

Find the error in this Swift code snippet:

let age = 25
print("Age is: " + age)
medium
A. Missing parentheses in print.
B. Cannot add String and Int directly.
C. Variable age is not declared.
D. print function is misspelled.

Solution

  1. Step 1: Analyze the print statement

    The code tries to add a String and an Int using +, which is not allowed in Swift.
  2. Step 2: Identify correct way to combine String and Int

    Use string interpolation like "Age is: \(age)" instead of + operator.
  3. Final Answer:

    Cannot add String and Int directly. -> Option B
  4. Quick Check:

    String + Int needs interpolation [OK]
Hint: Use \(variable) to combine text and numbers [OK]
Common Mistakes:
  • Trying to add String and Int
  • Missing string interpolation
  • Assuming + works for different types
5.

You want to print a list of numbers from 1 to 3 on the same line separated by spaces using Swift. Which code will do this correctly?

?
hard
A. print(1, 2, 3, separator: " ")
B. print("1, 2, 3")
C. print(1 + " " + 2 + " " + 3)
D. print(1, 2, 3, sep: " ")

Solution

  1. Step 1: Understand print's separator parameter

    Swift's print can take multiple values and a separator argument to join them with a string.
  2. Step 2: Check each option

    print(1, 2, 3, separator: " ") uses separator: " " correctly to print "1 2 3". Adding numbers and strings causes type error, print("1, 2, 3") prints commas instead of spaces, wrong parameter name fails.
  3. Final Answer:

    print(1, 2, 3, separator: " ") -> Option A
  4. Quick Check:

    Use separator: " " to join values [OK]
Hint: Use separator: " " to print values with spaces [OK]
Common Mistakes:
  • Using wrong parameter name sep
  • Trying to add Int and String
  • Printing fixed string instead of variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard