Bird
Raised Fist0
Swiftprogramming~20 mins

What is Swift - Practice Questions & Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is Swift primarily used for?

Swift is a programming language created by Apple. What is its main use?

ABuilding iOS, macOS, watchOS, and tvOS apps
BCreating Android applications
CDeveloping Windows desktop software
DWriting server-side scripts only
Attempts:
2 left
💡 Hint

Think about which devices Apple makes and what language they promote for app development.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code?

Look at this Swift code and choose the output it produces.

Swift
let greeting = "Hello"
let name = "Sam"
print("\(greeting), \(name)!")
AError: Cannot print variables this way
Bgreeting, name!
CHello, Sam!
DHello name!
Attempts:
2 left
💡 Hint

Check how variables are inserted inside strings in Swift.

❓ Predict Output
advanced
2:00remaining
What does this Swift code print?

Analyze the code and select the output it produces.

Swift
var numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled)
AError: map function not found
B[1, 2, 3, 4, 5]
C[1, 4, 9, 16, 25]
D[2, 4, 6, 8, 10]
Attempts:
2 left
💡 Hint

Remember that map applies a function to each item in the array.

🔧 Debug
advanced
2:00remaining
What error does this Swift code raise?

Find the error in this Swift code snippet.

Swift
let number: Int = "123"
print(number)
AType mismatch error
BSyntax error: missing semicolon
CNo error, prints 123
DRuntime error: nil value
Attempts:
2 left
💡 Hint

Check if the type on the left matches the value on the right.

🧠 Conceptual
expert
2:00remaining
Which feature makes Swift safe and fast?

Swift is known for safety and speed. Which feature helps achieve this?

AUsing dynamic typing for flexibility
BOptionals to handle absence of values safely
CManual memory management like in C
DNo type checking at compile time
Attempts:
2 left
💡 Hint

Think about how Swift avoids crashes from missing values.

Practice

(1/5)
1. What is Swift primarily used for?
easy
A. Designing graphics
B. Building websites
C. Creating apps for Apple devices
D. Managing databases

Solution

  1. Step 1: Understand Swift's main purpose

    Swift is designed mainly for developing software on Apple platforms like iPhone and Mac.
  2. Step 2: Compare options with Swift's use

    Only Creating apps for Apple devices matches this use; others relate to different fields.
  3. Final Answer:

    Creating apps for Apple devices -> Option C
  4. Quick Check:

    Swift = Apple app development [OK]
Hint: Swift is Apple's language for apps [OK]
Common Mistakes:
  • Thinking Swift is for websites
  • Confusing Swift with database tools
  • Assuming Swift is for graphic design
2. Which of the following is the correct way to declare a variable in Swift?
easy
A. var name = "John"
B. variable name = "John"
C. let name := "John"
D. dim name = "John"

Solution

  1. Step 1: Recall Swift variable declaration syntax

    Swift uses 'var' to declare variables and '=' to assign values.
  2. Step 2: Check each option

    var name = "John" matches Swift syntax; others use incorrect keywords or symbols.
  3. Final Answer:

    var name = "John" -> Option A
  4. Quick Check:

    Variable declaration = var name = value [OK]
Hint: Use 'var' to declare variables in Swift [OK]
Common Mistakes:
  • Using 'variable' instead of 'var'
  • Using ':=' instead of '='
  • Using 'dim' from other languages
3. What will be the output of this Swift code?
let speed = 60
let time = 2
let distance = speed * time
print(distance)
medium
A. 120
B. 62
C. Error
D. 60

Solution

  1. Step 1: Calculate the multiplication

    speed (60) multiplied by time (2) equals 120.
  2. Step 2: Understand print output

    The print statement outputs the value of distance, which is 120.
  3. Final Answer:

    120 -> Option A
  4. Quick Check:

    60 * 2 = 120 [OK]
Hint: Multiply variables and print result [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Confusing variable names
  • Expecting syntax error
4. Identify the error in this Swift code:
let age = 25
age = 30
print(age)
medium
A. Missing semicolon after print statement
B. Cannot assign a new value to a constant
C. Variable 'age' is not declared
D. Print statement syntax is incorrect

Solution

  1. Step 1: Check variable declaration

    'age' is declared with 'let', making it a constant that cannot change.
  2. Step 2: Identify assignment error

    Trying to assign 30 to 'age' causes an error because constants cannot be reassigned.
  3. Final Answer:

    Cannot assign a new value to a constant -> Option B
  4. Quick Check:

    let means constant, no reassignment [OK]
Hint: 'let' means constant, no change allowed [OK]
Common Mistakes:
  • Thinking 'let' allows reassignment
  • Expecting semicolons in Swift
  • Misreading print syntax
5. You want to write a Swift program that safely calculates the average of an array of numbers, but the array might be empty. Which approach best uses Swift's safety features?
hard
A. Force unwrap the array count and sum
B. Divide the sum by the array count without checks
C. Ignore empty arrays and return zero always
D. Use optional binding to check if the array is empty before dividing

Solution

  1. Step 1: Understand the problem with empty arrays

    Dividing by zero (array count 0) causes a runtime error.
  2. Step 2: Use optional binding for safety

    Optional binding lets you check if the array has elements before dividing, avoiding errors.
  3. Step 3: Evaluate options

    Use optional binding to check if the array is empty before dividing uses Swift's safety features correctly; others risk errors or ignore safety.
  4. Final Answer:

    Use optional binding to check if the array is empty before dividing -> Option D
  5. Quick Check:

    Optional binding = safe checks [OK]
Hint: Check array emptiness with optional binding before dividing [OK]
Common Mistakes:
  • Dividing without checking for zero count
  • Force unwrapping optionals causing crashes
  • Ignoring empty arrays without handling
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