Bird
0
0

Which of the following is the correct way to declare a Boolean variable named isActive and assign it the value true in Swift?

easy📝 Syntax Q3 of 15
Swift - Data Types
Which of the following is the correct way to declare a Boolean variable named isActive and assign it the value true in Swift?
Avar isActive = True
Bvar isActive: Bool = true
Cvar isActive = "true"
Dvar isActive = 1
Step-by-Step Solution
Solution:
  1. Step 1: Check correct Boolean literal and type

    In Swift, Boolean literals are lowercase true or false. The type Bool is used for Boolean variables.
  2. Step 2: Evaluate each option

    var isActive: Bool = true correctly declares isActive as Bool and assigns true. var isActive = True uses capital True which is invalid. var isActive = "true" assigns a string "true" which is not Boolean. var isActive = 1 assigns an integer 1 which is not Boolean.
  3. Final Answer:

    var isActive: Bool = true -> Option B
  4. Quick Check:

    Boolean declaration uses Bool and true/false lowercase [OK]
Quick Trick: Use lowercase true/false and Bool type for Booleans [OK]
Common Mistakes:
  • Using True instead of true
  • Assigning strings or numbers
  • Omitting type when needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes