Bird
0
0

You want to store the number of days in a week as a constant in Swift. Which code correctly declares this and prevents accidental changes later?

hard📝 Application Q15 of 15
Swift - Variables and Constants
You want to store the number of days in a week as a constant in Swift. Which code correctly declares this and prevents accidental changes later?
Alet daysInWeek // Assign value later
Bvar daysInWeek = 7 // Later changed to 8 accidentally
Cconstant daysInWeek = 7 // Prevents changes
Dlet daysInWeek = 7 // daysInWeek cannot be changed later
Step-by-Step Solution
Solution:
  1. Step 1: Choose correct keyword for constant

    Using let declares a constant that cannot be changed after assignment.
  2. Step 2: Check code correctness

    let daysInWeek = 7 // daysInWeek cannot be changed later declares and assigns daysInWeek with 7 and prevents changes later. var daysInWeek = 7 // Later changed to 8 accidentally uses var which allows changes. constant daysInWeek = 7 // Prevents changes uses invalid keyword. let daysInWeek // Assign value later declares without assignment, which is invalid for constants.
  3. Final Answer:

    let daysInWeek = 7 // daysInWeek cannot be changed later -> Option D
  4. Quick Check:

    Use let with assignment to make constant = A [OK]
Quick Trick: Use let with immediate value to make constant [OK]
Common Mistakes:
  • Using var instead of let for constants
  • Trying to declare let without initial value
  • Using non-Swift keywords like constant

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes