Bird
Raised Fist0
Swiftprogramming~10 mins

Bool type and logical operators in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a Boolean variable set to true.

Swift
let isSunny: Bool = [1]
Drag options to blanks, or click blank then click option'
Atrue
BTrue
C1
D"true"
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase True instead of lowercase true
Using numbers like 1 instead of Boolean values
Using strings like "true" instead of Boolean literals
2fill in blank
medium

Complete the code to check if both conditions are true using a logical AND operator.

Swift
let canGoOutside = isSunny [1] hasFreeTime
Drag options to blanks, or click blank then click option'
A||
B==
C&&
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR operator (||) instead of AND
Using equality (==) instead of logical operators
Using inequality (!=) instead of logical operators
3fill in blank
hard

Fix the error in the code by choosing the correct logical NOT operator.

Swift
let isRaining = false
let shouldTakeUmbrella = [1]isRaining
Drag options to blanks, or click blank then click option'
A!
Bnot
C~
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using ~ which is a bitwise NOT, not logical NOT
Using 'not' which is not valid Swift syntax
Using double exclamation marks (!!) which is invalid
4fill in blank
hard

Fill the blank to create a Boolean expression that is true if either it is weekend or you have no homework.

Swift
let canRelax = isWeekend [1] noHomework
Drag options to blanks, or click blank then click option'
A!=
B&&
C==
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND (&&) instead of OR
Using equality (==) or inequality (!=) operators incorrectly
5fill in blank
hard

Fill all three blanks to create a Boolean expression that is true if the word's length is greater than 3 and the word is not empty.

Swift
let word = "apple"
let condition = (word.count [1] 3) [2] (word [3] "")
Drag options to blanks, or click blank then click option'
A>
B&&
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != to check for non-empty string
Using || instead of && which changes logic
Using < instead of > for length comparison

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

  1. Step 1: Understand Boolean values in Swift

    Swift uses true and false as Boolean values, not numbers or words.
  2. Step 2: Identify the valid Boolean value

    Among the options, only true is a valid Boolean value.
  3. Final Answer:

    true -> Option D
  4. 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

  1. Step 1: Recall Swift logical AND syntax

    Swift uses && for logical AND, not a single ampersand or words.
  2. Step 2: Identify the correct syntax

    a && b uses &&, which is the correct logical AND operator.
  3. Final Answer:

    a && b -> Option A
  4. 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

  1. Step 1: Evaluate the NOT operator

    !a means NOT true, which is false.
  2. Step 2: Evaluate AND operation

    b && !a is false && false, which is false.
  3. Step 3: Evaluate OR operation

    a || (result of AND) is true || false, which is true.
  4. Final Answer:

    true -> Option C
  5. 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

  1. Step 1: Check logical AND operator usage

    In Swift, logical AND uses &&, not a single &.
  2. Step 2: Identify the syntax error

    Using & causes a compile error because it's a bitwise operator, not logical.
  3. Final Answer:

    Using single & instead of && for logical AND -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    number >= 10 && number <= 20 -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard