0
0
Swiftprogramming~20 mins

Explicit type annotation in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Type 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 code with explicit type annotation?

Consider the following Swift code snippet. What will be printed?

Swift
let number: Int = 5
let result = number * 2
print(result)
ACompilation error
B5
C10
DType error
Attempts:
2 left
💡 Hint

Look at the type of number and the operation performed.

Predict Output
intermediate
2:00remaining
What is the type of variable value here?

Given the code below, what is the type of value?

Swift
let value: Double = 3.14
print(type(of: value))
AInt
BDouble
CFloat
DString
Attempts:
2 left
💡 Hint

Check the explicit type annotation used when declaring value.

🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a compilation error?

Examine the code below. Why does it fail to compile?

Swift
let text: Int = "Hello"
print(text)
ACannot assign a String to an Int variable
BMissing semicolon at the end of the line
CVariable <code>text</code> is not initialized
DPrint statement syntax is incorrect
Attempts:
2 left
💡 Hint

Look at the type annotation and the assigned value.

Predict Output
advanced
2:00remaining
What is the output of this Swift code with explicit type annotation and optional?

What will this code print?

Swift
let optionalNumber: Int? = 10
if let number: Int = optionalNumber {
    print(number * 3)
} else {
    print("No value")
}
A30
BNo value
COptional(30)
DCompilation error
Attempts:
2 left
💡 Hint

Check how optional binding with explicit type annotation works.

🧠 Conceptual
expert
2:00remaining
Why is explicit type annotation useful in Swift?

Choose the best reason why explicit type annotation is important in Swift programming.

AIt automatically converts all types to String
BIt makes the code run faster at runtime
CIt allows variables to change types dynamically
DIt helps the compiler catch type errors early and improves code readability
Attempts:
2 left
💡 Hint

Think about how Swift uses types to prevent bugs.