[{"id":"swiftintermediate-001","courseId":"swift-intermediate","domain":"values-and-control-flow","topic":"Optional chaining","prompt":"Residence stores numberOfRooms as an Int, and Person keeps its residence as a Residence?. What is the type of rooms after the line below runs?","hint":"Consider what the chain can produce when an earlier link is nil.","explain":"A question mark lets a lookup stop early when an optional along the way is empty. Think about what the caller must receive when that happens, even if the member reached is plain.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/optionalchaining/","interaction":{"type":"single"},"options":[{"id":"a","text":"Int?","why":"Optional chaining wraps the result in an optional, even though numberOfRooms is a non-optional Int."},{"id":"b","text":"Int","whyNot":"A plain Int would mean the chain always yields a number, but it yields nil when residence is nil."},{"id":"c","text":"Residence?","whyNot":"The chain stops at numberOfRooms, so the result carries that property's type, not the residence's."},{"id":"d","text":"Int!","whyNot":"An implicitly unwrapped optional is not what a chain yields; the result is an ordinary optional."}],"answer":"a","explanation":"The result of an optional chaining call is always optional, even when the queried property returns a non-optional type such as Int.","concept":"The result of an optional chaining call is always optional, even when the queried property returns a non-optional type such as Int.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"class Residence {\n    var numberOfRooms = 1\n}\nclass Person {\n    var residence: Residence?\n}\nlet person = Person()\nlet rooms = person.residence?.numberOfRooms","takeaways":["An optional chain's result is always optional, even when the member it reaches is not."]},{"id":"swiftintermediate-002","courseId":"swift-intermediate","domain":"values-and-control-flow","topic":"Forced unwrapping","prompt":"The code below forces an optional that is currently nil. Which TWO statements about what happens next are correct?","hint":"Think about what happens when the value you are asserting is missing.","explain":"An optional holds a value or nothing. Reading it with a shortcut that skips the check is a bet that something exists. Think about what follows when that bet is lost, and whether the compiler can catch it.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"It triggers a runtime error and stops the program.","why":"Forcing a nil optional triggers a runtime error, as the page states."},{"id":"b","text":"Writing ! is effectively a shorter spelling of calling fatalError(_:file:line:).","why":"The page describes ! as, effectively, a shorter spelling of this fatalError call."},{"id":"c","text":"The variable quietly becomes nil and the next line keeps running.","whyNot":"That describes optional chaining, which fails gracefully; a forced unwrap triggers a runtime error instead."},{"id":"d","text":"Swift substitutes the zero value of the type, such as 0 for an Int, and continues.","whyNot":"Swift never fills in a default for a forced unwrap; the nil case is a runtime error."},{"id":"e","text":"The compiler rejects the line before the program runs, because force unwrapping is not allowed.","whyNot":"Force unwrapping is valid Swift and compiles; the failure happens only when the line runs with nil."},{"id":"f","text":"The expression evaluates to nil, just as an optional chain does when a link is missing.","whyNot":"A forced unwrap never produces nil; an optional chain does, but a forced unwrap triggers a runtime error."}],"answer":"ab","explanation":"Force unwrapping with ! returns the wrapped value, but forcing a nil optional triggers a runtime error; ! is effectively a shorter spelling of fatalError(_:file:line:).","concept":"Force unwrapping with ! returns the wrapped value, but forcing a nil optional triggers a runtime error; ! is effectively a shorter spelling of fatalError(_:file:line:).","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"var convertedNumber: Int? = nil\nlet n = convertedNumber!","takeaways":["Use ! only where a nil value would be a programmer error, not a value you expect to be missing."]},{"id":"swiftintermediate-003","courseId":"swift-intermediate","domain":"values-and-control-flow","topic":"Implicitly unwrapped optionals","prompt":"The declaration below makes assumedString a non-optional String, so the comparison assumedString != nil fails to compile.","hint":"Consider what the exclamation mark changes, and what it leaves alone.","explain":"An exclamation mark on a type affects when unwrapping happens. Consider what the variable is underneath, and whether checking it against nothing still makes sense for it.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The ! only permits automatic unwrapping; the type stays an optional, so comparing it with nil compiles."},{"id":"b","text":"False","why":"An implicitly unwrapped optional is a normal optional behind the scenes, and checking it against nil works like any optional."}],"answer":"b","explanation":"An implicitly unwrapped optional is a normal optional behind the scenes: it can hold nil, can be compared with nil, and is unwrapped automatically only where a non-optional is needed.","concept":"An implicitly unwrapped optional is a normal optional behind the scenes: it can hold nil, can be compared with nil, and is unwrapped automatically only where a non-optional is needed.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"let assumedString: String! = \"Hello\"","takeaways":["An exclamation mark on a type allows automatic unwrapping; it does not promise the value is never nil."]},{"id":"swiftintermediate-004","courseId":"swift-intermediate","domain":"values-and-control-flow","topic":"Where clauses in loops and cases","prompt":"A repeat-while loop moves a player around a snakes-and-ladders board. Put these steps of one pass through the loop body, and its check, in order.","hint":"Read what the passage calls the first action in each pass.","explain":"A loop body is a chain of stages, and each stage works on what the earlier ones left behind. A repeat-while loop tests its exit condition at a different point from a plain while loop.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/","interaction":{"type":"sort","items":[{"text":"Move up or down if a snake or ladder is on the current square.","why":"The passage calls the snake and ladder check the first action in the loop."},{"text":"Roll the dice and update diceRoll.","why":"The dice is rolled only after the snake and ladder check has run."},{"text":"Move the player forward by the rolled amount.","why":"The move uses diceRoll, so it has to come after the roll that sets it."},{"text":"Check whether square is still below finalSquare.","why":"The loop condition is evaluated only at the end of the pass, after the move."}]},"options":[],"answer":"o:abcd","explanation":"A repeat-while loop runs its body before testing the condition. On the board, the snake and ladder check comes first, then the dice roll, then the move by the roll, then the condition test.","concept":"A repeat-while loop runs its body before testing the condition. On the board, the snake and ladder check comes first, then the dice roll, then the move by the roll, then the condition test.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A repeat-while loop always runs its body once before it tests the condition."]},{"id":"swiftintermediate-005","courseId":"swift-intermediate","domain":"values-and-control-flow","topic":"Multiple patterns in one case","prompt":"In the code below, one branch of this if expression can throw an error. What, if anything, must you write before the if?","hint":"Consider whether a throwing branch changes the syntax of the if itself.","explain":"An if can produce a value on the right of an assignment, and one branch may end by throwing an error. Think about whether that changes what the surrounding code must mark where the expression appears.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/","interaction":{"type":"single"},"options":[{"id":"a","text":"try","whyNot":"try marks a call that can throw, but the page writes no try before this if expression."},{"id":"b","text":"try?","whyNot":"try? turns a failure into nil; the page does not put any try in front of this if expression."},{"id":"c","text":"No keyword at all","why":"The page notes that you don't write try before an if expression that can throw an error."},{"id":"d","text":"try!","whyNot":"try! forces the call to succeed and crashes if it throws; the page writes nothing before the if."}],"answer":"c","explanation":"An if expression can end one branch by throwing an error, and the page shows it written with no try before it.","concept":"An if expression can end one branch by throwing an error, and the page shows it written with no try before it.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"let advice = if celsius > 100 {\n    throw TemperatureError.boiling\n} else {\n    \"Reasonable.\"\n}","takeaways":["An if expression may throw from a branch and still needs no try written before it."]}]