[{"id":"swiftadvanced-001","courseId":"swift-advanced","domain":"values-and-control-flow","topic":"Canonical equivalence in string equality","prompt":"A scene title, \"Café scene\", stores é as one precomposed character (U+00E9). Your code calls title.hasPrefix with the same letter written as a plain e plus a combining acute accent (U+0301). What does it return?","hint":"Check whether the comparison looks at raw scalars or at the characters a reader sees.","explain":"Different sequences of Unicode scalars can be defined to mean the same text. Swift compares strings by the characters a reader sees, not by raw scalars, and prefix tests use the same rule.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/","interaction":{"type":"single"},"options":[{"id":"a","text":"It returns true, since the two forms are canonically equivalent","why":"hasPrefix compares extended grapheme clusters by canonical equivalence, so precomposed é and e plus accent match."},{"id":"b","text":"It returns false, since the scalars differ","whyNot":"The comparison is not scalar by scalar; the cluster é matches its decomposed spelling."},{"id":"c","text":"It returns false, since prefix tests compare UTF-8 bytes","whyNot":"Prefix tests compare characters, not bytes, so different byte sequences can still match."},{"id":"d","text":"It traps at runtime, since the scalar counts differ","whyNot":"A mismatch in scalar count is not an error; the call simply compares the characters."}],"answer":"a","explanation":"hasPrefix and hasSuffix compare extended grapheme clusters by canonical equivalence, so precomposed and decomposed spellings of the same character match.","concept":"hasPrefix and hasSuffix compare extended grapheme clusters by canonical equivalence, so precomposed and decomposed spellings of the same character match.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["String comparison and prefix tests judge visible characters, not raw scalars or bytes."]},{"id":"swiftadvanced-002","courseId":"swift-advanced","domain":"values-and-control-flow","topic":"The unicodeScalars view","prompt":"A teammate inspects let dogString = \"Dog‼🐶\" through its utf16 view. Which TWO statements about those UTF-16 values are correct?","hint":"Count the code units the emoji needs in UTF-16, not its scalars or its Characters.","explain":"Text can be counted as bytes, encoding units, or scalars, and those counts differ. A scalar may need several units in one encoding and just one in another.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"The 🐶 character is written as a surrogate pair of two 16-bit code units.","why":"U+1F436 is too large for one UTF-16 unit, so it is written as a high and a low surrogate."},{"id":"b","text":"The ‼ character is stored as one 16-bit code unit.","why":"U+203C fits in a single UTF-16 code unit, which the view shows as 8252."},{"id":"c","text":"The view has one value per Character, so it yields five values.","whyNot":"The emoji takes two code units, so the view yields six values for five Characters."},{"id":"d","text":"The 🐶 appears as the single value 128054 in the utf16 view.","whyNot":"128054 is the scalar value from the unicodeScalars view; UTF-16 splits that scalar into two units."},{"id":"e","text":"The utf16 and utf8 views yield the same number of values.","whyNot":"The utf8 view encodes the 🐶 in four bytes and the ‼ in three, so the two counts differ."},{"id":"f","text":"All six utf16 values are byte-sized, below 256.","whyNot":"Values such as 8252 and the surrogates are far above 255, so the view is not limited to bytes."}],"answer":"ab","explanation":"UTF-16 encodes the dog face scalar as a surrogate pair of two 16-bit code units, while U+203C fits in one code unit.","concept":"UTF-16 encodes the dog face scalar as a surrogate pair of two 16-bit code units, while U+203C fits in one code unit.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A string's element count in one Unicode view can differ from its count in another view."]},{"id":"swiftadvanced-003","courseId":"swift-advanced","domain":"values-and-control-flow","topic":"Overflow operators wrap around","prompt":"In Swift, Int8.min &- 1 stops the program with a runtime error, just as the plain Int8.min - 1 does.","hint":"Compare what the ampersand changes about how the result is handled, not just the syntax.","explain":"Fixed-size integers hold a bounded range, so a result can fall outside it. Swift's operators do not all treat such a result the same way: one may report an error, while another keeps only the bits that fit.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/advancedoperators/","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The &- operator wraps around: Int8.min &- 1 gives 127, and no error is raised."},{"id":"b","text":"False","why":"Overflow operators such as &- wrap from the minimum value to the maximum, so the program keeps running with 127."}],"answer":"b","explanation":"Overflow operators (&+, &-, &*) wrap around at the type's bounds, while plain arithmetic reports an out-of-range result as an error.","concept":"Overflow operators (&+, &-, &*) wrap around at the type's bounds, while plain arithmetic reports an out-of-range result as an error.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Int8.min &- 1 wraps to Int8.max; the plain minus operator does not wrap."]},{"id":"swiftadvanced-004","courseId":"swift-advanced","domain":"values-and-control-flow","topic":"Bitwise operators","prompt":"You want a mask that sets each result bit to 1 only where exactly one of two inputs has a 1, and to 0 where the inputs match. Which operator does this?","hint":"Think about which of the four pairs of input bits should produce a 1.","explain":"Bitwise operators combine two numbers position by position. Each has its own rule for turning a pair of input bits into one output bit, and the rules differ in which of the four bit pairs produce a 1.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/advancedoperators/","interaction":{"type":"single"},"options":[{"id":"a","text":"The ^ operator (exclusive OR)","why":"It returns 1 where the input bits differ and 0 where they are the same."},{"id":"b","text":"The & operator (bitwise AND)","whyNot":"AND keeps a 1 only where both inputs have a 1, so it does not flip mismatched bits."},{"id":"c","text":"The | operator (bitwise OR)","whyNot":"OR gives 1 wherever either input has a 1, including positions where both inputs do."},{"id":"d","text":"The ~ operator (bitwise NOT)","whyNot":"NOT inverts the bits of a single number and takes no second operand."}],"answer":"a","explanation":"The XOR operator (^) sets a result bit to 1 only where the input bits differ, and to 0 where they match.","concept":"The XOR operator (^) sets a result bit to 1 only where the input bits differ, and to 0 where they match.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Use ^ to keep only the bits that differ between two values."]},{"id":"swiftadvanced-005","courseId":"swift-advanced","domain":"values-and-control-flow","topic":"Custom operator declarations","prompt":"Swift evaluates 2 + 3 % 4 * 5 by rewriting the expression one stage at a time. Put these stages in the order Swift reaches them.","hint":"The stage order follows precedence first and associativity second.","explain":"An expression is reduced in stages, not strictly left to right. Higher-precedence operators reduce first, and equal ones group by associativity. Each stage swaps part of the expression for a simpler value.","source":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/advancedoperators/","interaction":{"type":"sort","items":[{"text":"2 + ((3 % 4) * 5)","why":"Implicit parentheses group the remainder and the multiplication from the left; this grouped form is the starting point."},{"text":"2 + (3 * 5)","why":"The inner remainder has been reduced to 3, leaving a multiplication and then the addition."},{"text":"2 + 15","why":"The multiplication has been reduced to 15, leaving only the addition."},{"text":"17","why":"The last addition produces the final result."}]},"options":[],"answer":"o:abcd","explanation":"Precedence decides which operator reduces first, and associativity decides grouping among operators of equal precedence.","concept":"Precedence decides which operator reduces first, and associativity decides grouping among operators of equal precedence.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Read a mixed expression by its grouping, not left to right: % and * bind before +."]}]