0
0
Swiftprogramming~20 mins

How Swift compiles to native code - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Native Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Swift Native Compilation Output
What is the output of this Swift code when compiled to native code and run?
Swift
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled)
A[1, 2, 3, 4, 5]
B[2, 4, 6, 8, 10]
CCompilation error
D[0, 2, 4, 6, 8]
Attempts:
2 left
💡 Hint
Think about what the map function does to each element.
🧠 Conceptual
intermediate
1:30remaining
Swift Compilation Stages
Which stage in Swift compilation converts Swift code into machine instructions for the target device?
ACode generation
BLexical analysis
CParsing
DSemantic analysis
Attempts:
2 left
💡 Hint
This stage produces the final instructions the computer understands.
🔧 Debug
advanced
2:30remaining
Identify the Compilation Error
What error will this Swift code produce when compiled to native code?
Swift
func greet(name: String) -> String {
    return "Hello, \(name)"
}

print(greet(name: 123))
ANo error, prints Hello, 123
BMissing return statement error
CSyntax error: unexpected token
DType mismatch error
Attempts:
2 left
💡 Hint
Check the argument type passed to the function.
📝 Syntax
advanced
2:00remaining
Swift Native Compilation Syntax Check
Which option contains the correct Swift syntax that compiles to native code without errors?
Swift
var message = ""
for i in 1...3 {
    message += "Number \(i) "
}
print(message)
A
var message = ""
for i in 1..3 {
    message += "Number \(i) "
}
print(message)
B
var message = ""
for i in 1...3 {
    message += "Number (i) "
}
print(message)
C
var message = ""
for i in 1...3 {
message += "Number \(i) "
}
print(message)
D
var message = ""
for i in 1...3 {
    message += "Number \(i) "
print(message)
}
Attempts:
2 left
💡 Hint
Check the range operator and string interpolation syntax.
🚀 Application
expert
3:00remaining
Swift Native Code Performance
Which Swift code snippet will most likely produce the fastest native code when compiled?
AUsing a for loop with array indexing to sum elements
BUsing map to create a new array and then reduce to sum elements
CUsing recursion to sum elements of the array
DUsing a while loop with manual index increment to sum elements
Attempts:
2 left
💡 Hint
Consider how native code optimizes simple loops versus function calls.