[{"id":"swiftbeginner-001","courseId":"swift-beginner","domain":"values-and-control-flow","topic":"Constants and variables","prompt":"A match allows 8 players, and that number never changes once the match starts. Which declaration creates it as a constant?","hint":"Only one of these keywords makes the value unchangeable once it is set.","explain":"Swift has two declaration keywords: one for values fixed once set, and one for values that can change later. Pick the kind that matches how the value behaves.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"single"},"options":[{"id":"a","text":"let maxPlayers = 8","why":"let declares a constant, whose value can't be changed once it is set."},{"id":"b","text":"var maxPlayers = 8","whyNot":"var declares a variable. It compiles, but it signals a value meant to change."},{"id":"c","text":"const maxPlayers = 8","whyNot":"Swift has no const keyword; constants are declared with let."},{"id":"d","text":"static var maxPlayers = 8","whyNot":"static attaches a member to a type; var still allows changes, so the value isn't fixed."}],"answer":"a","explanation":"let declares a constant that can't change after it is set; var declares a variable that can be reassigned later.","concept":"let declares a constant that can't change after it is set; var declares a variable that can be reassigned later.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Default to let, and reach for var only when the value must change."]},{"id":"swiftbeginner-002","courseId":"swift-beginner","domain":"values-and-control-flow","topic":"Type inference and annotations","prompt":"With no type annotation, the declaration let pi = 3.14159 makes pi a Float.","hint":"Consider which default type Swift picks for a decimal literal when you write no annotation.","explain":"Swift can deduce a constant's type from the literal it is given, so writing the type is optional. Whole-number and decimal literals each have a default type that the compiler picks when nothing else is written.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"A decimal literal with no annotation is inferred as Double, the default floating-point type, not Float."},{"id":"b","text":"False","why":"Swift infers Double for a floating-point literal with no type written; Float has to be requested explicitly."}],"answer":"b","explanation":"With no annotation, Swift infers a floating-point literal as Double and an integer literal as Int; Float must be requested explicitly.","concept":"With no annotation, Swift infers a floating-point literal as Double and an integer literal as Int; Float must be requested explicitly.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Let Swift infer Double for decimals; write Float explicitly only when you need its 32-bit size."]},{"id":"swiftbeginner-003","courseId":"swift-beginner","domain":"values-and-control-flow","topic":"Tuples","prompt":"You store (404, \"Not Found\") in response, a tuple with no element names. Which expression reads the message text?","hint":"Look at how the tuple was created, and how its parts are reached.","explain":"A tuple bundles values of mixed types into one value. Each element can be reached by its position, and names are optional: they exist only if they were given when the tuple was created.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"single"},"options":[{"id":"a","text":"response.1","why":"Unnamed tuple elements are read by position, and positions count from zero, so the text sits at 1."},{"id":"b","text":"response.2","whyNot":"Positions start at zero, so the message is at 1; there is no element at 2."},{"id":"c","text":"response[1]","whyNot":"Tuple elements aren't read with subscript brackets; that syntax belongs to collections."},{"id":"d","text":"response.message","whyNot":"The elements have no names, because the tuple was created without them."}],"answer":"a","explanation":"Tuple elements are accessed with .0, .1 and so on, counting from zero; you can also name elements when the tuple is defined.","concept":"Tuple elements are accessed with .0, .1 and so on, counting from zero; you can also name elements when the tuple is defined.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"let response = (404, \"Not Found\")","takeaways":["Unnamed tuple elements are read by position, starting at zero."]},{"id":"swiftbeginner-004","courseId":"swift-beginner","domain":"values-and-control-flow","topic":"Optionals","prompt":"A teammate coming from Objective-C writes var surveyAnswer: String? and assumes some habits carry over. Which TWO statements about optionals are correct?","hint":"Think about the two possible states of an optional, and what a plain type promises.","explain":"A type can be declared to allow missing values, or to forbid them, and the compiler enforces that choice. Consider which statements match that split and which describe other language habits.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"An optional holds either a value of its type or no value at all.","why":"That two-state idea is exactly what an optional type expresses: a value, or nil."},{"id":"b","text":"A non-optional value is guaranteed never to be missing.","why":"A non-optional type promises a value is always present, so the compiler refuses to assign nil to it."},{"id":"c","text":"nil is a pointer to a nonexistent object, as in Objective-C.","whyNot":"In Swift, nil isn't a pointer; it is the absence of a value of a certain type."},{"id":"d","text":"Only class types can be set to nil.","whyNot":"Optionals of any type can be set to nil, not just object types."},{"id":"e","text":"A non-optional var can be set to nil if it is declared with var.","whyNot":"var controls whether a value can change, not whether it can be missing; assigning nil to a non-optional is a compile-time error."},{"id":"f","text":"An optional declared as String? with no value starts as an empty string.","whyNot":"An optional declared without a value is set to nil, not to an empty string."}],"answer":"ab","explanation":"An optional T? holds either a T or nil; a non-optional T is guaranteed to hold a value, and nil can't be assigned to it. In Swift nil isn't a pointer.","concept":"An optional T? holds either a T or nil; a non-optional T is guaranteed to hold a value, and nil can't be assigned to it. In Swift nil isn't a pointer.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["T? allows absence; T promises a value, so nil can't be assigned to it."]},{"id":"swiftbeginner-005","courseId":"swift-beginner","domain":"values-and-control-flow","topic":"Optional binding with if let","prompt":"A function returns Int? and gives nil for bad input. Inside this code, what happens when parse returns nil?","hint":"Think about when the new name is ever given a value at all.","explain":"Optional binding checks whether an optional holds a value, and only when it does makes that value available under a new name inside a block. Consider which path can use that name, and what the other path does.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"single"},"options":[{"id":"a","text":"The if body is skipped, and count is never created.","why":"if let binds the name only when the optional has a value, so with nil the block doesn't run."},{"id":"b","text":"The body runs with count set to 0.","whyNot":"Swift doesn't substitute a default number; with nil, the bound name simply isn't created."},{"id":"c","text":"The program stops with a runtime error.","whyNot":"That happens when you force-unwrap nil with !, not with if let, which checks before it unwraps."},{"id":"d","text":"The body runs, and count holds nil.","whyNot":"Inside the block, count is non-optional, which is exactly why binding is useful."}],"answer":"a","explanation":"if let unwraps an optional only when it holds a value; the bound constant is non-optional and exists only inside the block, so a nil value skips the block.","concept":"if let unwraps an optional only when it holds a value; the bound constant is non-optional and exists only inside the block, so a nil value skips the block.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"func parse(_ s: String) -> Int? {\n    Int(s)\n}\n\nif let count = parse(text) {\n    print(count)\n}","takeaways":["if let is the safe unwrap: the block runs only when a value is present."]}]