0
0
Swiftprogramming~20 mins

Bool type and logical operators in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Bool 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 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)
Afalse
Btrue
Cnil
DCompilation error
Attempts:
2 left
💡 Hint
Remember that logical AND (&&) returns true only if both sides are true.
Predict Output
intermediate
2: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)
Atrue
Bfalse
Cnil
DRuntime error
Attempts:
2 left
💡 Hint
Logical OR (||) returns true if at least one operand is true.
Predict Output
advanced
2: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)
ACompilation error
Bfalse
Ctrue
Dnil
Attempts:
2 left
💡 Hint
Remember the precedence: && is evaluated before ||.
Predict Output
advanced
2: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)
Afalse
BSyntax error
Cnil
Dtrue
Attempts:
2 left
💡 Hint
The NOT operator (!) reverses the Boolean value.
🧠 Conceptual
expert
2: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"
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Dictionary keys must be unique. Assigning a value to an existing key replaces the old value.