Bool is a data type in Swift that can hold only two values: true or false. It is used to represent logical conditions.
Click to reveal answer
beginner
What does the logical AND operator (&&) do?
The logical AND operator (&&) returns true only if both conditions on its sides are true. Otherwise, it returns false.
Click to reveal answer
beginner
What is the result of true || false in Swift?
The logical OR operator (||) returns true if at least one of the conditions is true. So, true || false evaluates to true.
Click to reveal answer
beginner
How does the logical NOT operator (!) work?
The logical NOT operator (!) reverses the value of a Bool. If the value is true, it becomes false, and vice versa.
Click to reveal answer
beginner
What will be the output of this Swift code?<br><pre>let a = true
let b = false
print(a && b)</pre>
The output will be false because the AND operator (&&) requires both values to be true, but here b is false.
Click to reveal answer
Which of these is a valid Bool value in Swift?
A"false"
Byes
C1
Dtrue
✗ Incorrect
Only true and false are valid Bool values in Swift.
What does the expression false || false evaluate to?
Atrue
Bfalse
Cnil
Derror
✗ Incorrect
The OR operator (||) returns true if at least one side is true. Here both are false, so result is false.
What is the result of !true in Swift?
Anil
Btrue
Cfalse
Derror
✗ Incorrect
The NOT operator (!) reverses the Bool value, so !true is false.
Which operator checks if both conditions are true?
A&&
B!
C||
D==
✗ Incorrect
The AND operator (&&) returns true only if both conditions are true.
What will print(true && false) output?
Afalse
Btrue
Cnil
Derror
✗ Incorrect
AND operator requires both true, here one is false, so output is false.
Explain the Bool type and how logical operators work in Swift.
Think about true/false values and how you combine or reverse them.
You got /4 concepts.
Describe a real-life example where you might use logical AND and OR operators.
Think about everyday decisions with multiple conditions.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a valid Boolean value in Swift?
easy
A. 0
B. 1
C. yes
D. true
Solution
Step 1: Understand Boolean values in Swift
Swift uses true and false as Boolean values, not numbers or words.
Step 2: Identify the valid Boolean value
Among the options, only true is a valid Boolean value.
Final Answer:
true -> Option D
Quick Check:
Boolean values are true or false [OK]
Hint: Remember: Bool values are only true or false [OK]
Common Mistakes:
Confusing 1 or 0 as Boolean values
Using words like 'yes' or 'no' instead of true/false
2. Which of the following is the correct way to write a logical AND operation in Swift?
easy
A. a && b
B. a & b
C. a and b
D. a || b
Solution
Step 1: Recall Swift logical AND syntax
Swift uses && for logical AND, not a single ampersand or words.
Step 2: Identify the correct syntax
a && b uses &&, which is the correct logical AND operator.
Final Answer:
a && b -> Option A
Quick Check:
Logical AND in Swift is && [OK]
Hint: Use double ampersand && for AND in Swift [OK]
Common Mistakes:
Using single & instead of &&
Writing 'and' as a word
Confusing AND with OR operator ||
3. What is the output of the following Swift code?
let a = true
let b = false
print(a || b && !a)
medium
A. Syntax error
B. false
C. true
D. nil
Solution
Step 1: Evaluate the NOT operator
!a means NOT true, which is false.
Step 2: Evaluate AND operation
b && !a is false && false, which is false.
Step 3: Evaluate OR operation
a || (result of AND) is true || false, which is true.
Final Answer:
true -> Option C
Quick Check:
Logical operators follow precedence: NOT, AND, OR [OK]
Hint: Remember operator precedence: ! before && before || [OK]
Common Mistakes:
Ignoring operator precedence
Evaluating OR before AND
Confusing !a as true
4. Identify the error in the following Swift code:
let x = true
let y = false
let result = x & y
medium
A. Missing semicolon at the end of the line
B. Using single & instead of && for logical AND
C. Variables x and y must be integers
D. Using let instead of var for result
Solution
Step 1: Check logical AND operator usage
In Swift, logical AND uses &&, not a single &.
Step 2: Identify the syntax error
Using & causes a compile error because it's a bitwise operator, not logical.
Final Answer:
Using single & instead of && for logical AND -> Option B
Quick Check:
Logical AND requires &&, not & [OK]
Hint: Use && for logical AND, & is bitwise [OK]
Common Mistakes:
Confusing bitwise & with logical &&
Thinking semicolons are required in Swift
Assuming let cannot be used for constants
5. You want to check if a number is between 10 and 20 (inclusive) using Boolean logic in Swift. Which expression correctly uses logical operators to do this?
let number = 15
// Which condition is correct?
hard
A. number >= 10 && number <= 20
B. number > 10 || number < 20
C. number >= 10 || number <= 20
D. number > 10 && number < 20
Solution
Step 1: Understand the range condition
To check if number is between 10 and 20 inclusive, it must be greater or equal to 10 AND less or equal to 20.
Step 2: Analyze each option
number >= 10 && number <= 20 uses && with inclusive comparisons, which is correct. Others use OR or exclusive comparisons, which are incorrect.
Final Answer:
number >= 10 && number <= 20 -> Option A
Quick Check:
Use AND to combine both range checks [OK]
Hint: Use && to combine both conditions for range checks [OK]
Common Mistakes:
Using || instead of && for range
Using > or < instead of >= or <=
Mixing OR and AND incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.