Challenge - 5 Problems
Swift Bool Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using logical AND?
Consider the following Swift code snippet. What will be printed when it runs?
Swift
let a = true let b = false print(a && b)
Attempts:
2 left
💡 Hint
Remember that logical AND (&&) returns true only if both sides are true.
✗ Incorrect
The logical AND operator returns true only if both operands are true. Here, 'a' is true but 'b' is false, so the result is false.
❓ Predict Output
intermediate2:00remaining
What does this Swift code print using logical OR?
Look at this Swift code. What will be the output?
Swift
let x = false let y = true print(x || y)
Attempts:
2 left
💡 Hint
Logical OR (||) returns true if at least one operand is true.
✗ Incorrect
Since 'y' is true, the OR operation returns true regardless of 'x'.
❓ Predict Output
advanced2:00remaining
What is the output of this Swift code with combined logical operators?
Analyze this Swift code and determine what it prints.
Swift
let p = true let q = false let r = true print(p && q || r)
Attempts:
2 left
💡 Hint
Remember the precedence: && is evaluated before ||.
✗ Incorrect
First, p && q is false (true && false = false). Then false || r is true (false || true = true).
❓ Predict Output
advanced2:00remaining
What does this Swift code print using the NOT operator?
What is the output of this Swift code snippet?
Swift
let flag = false print(!flag)
Attempts:
2 left
💡 Hint
The NOT operator (!) reverses the Boolean value.
✗ Incorrect
Since flag is false, !flag is true.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting dictionary after this Swift code runs?
Consider this Swift code that uses Boolean keys in a dictionary. How many key-value pairs does the dictionary contain after execution?
Swift
var dict = [Bool: String]() dict[true] = "Yes" dict[false] = "No" dict[true] = "Maybe"
Attempts:
2 left
💡 Hint
Dictionary keys must be unique. Assigning a value to an existing key replaces the old value.
✗ Incorrect
The dictionary has two keys: true and false. The value for true is updated from "Yes" to "Maybe", so total pairs remain 2.