[{"id":"dartintermediate-001","courseId":"dart-intermediate","domain":"variables-and-operators","topic":"late variables","prompt":"A class declares the field below, and nothing in the program ever reads temperature. Which TWO statements are correct?","hint":"Look for what the variable's initializer waits for before it runs.","explain":"Some variables are set up lazily: the code that gives them a value waits until something needs that value. Think about what it means for a declaration to be evaluated when nobody refers to it.","source":"https://dart.dev/language/variables","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"If nothing reads temperature, readThermometer() is never called.","why":"A late initializer runs the first time the variable is used, so an unused one never runs."},{"id":"b","text":"The first time temperature is used, readThermometer() runs.","why":"The initializer is deferred until that first use, which is what lazy initialization means."},{"id":"c","text":"readThermometer() runs as soon as the declaration is reached.","whyNot":"That is the eager behavior a late initializer avoids; it waits for first use."},{"id":"d","text":"readThermometer() runs once at program start, before main.","whyNot":"Late initializers are lazy, so nothing runs at startup for this field."},{"id":"e","text":"temperature holds null until readThermometer() is called.","whyNot":"A non-nullable late variable never holds null; its initializer runs on first use."},{"id":"f","text":"temperature must be declared nullable, since it might never be set.","whyNot":"No ? is needed: if it is never read the initializer never runs, and if it is read the initializer sets it."}],"answer":"ab","explanation":"A late variable with an initializer runs that initializer the first time the variable is used; if the variable is never used, the initializer never runs.","concept":"A late variable with an initializer runs that initializer the first time the variable is used; if the variable is never used, the initializer never runs.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"class Station {\n  late String temperature =\n    readThermometer();\n}","takeaways":["A late initializer is lazy: unused expensive setup costs nothing."]},{"id":"dartintermediate-002","courseId":"dart-intermediate","domain":"variables-and-operators","topic":"Lazy top-level variables","prompt":"A top-level declaration `final config = loadConfig();` runs loadConfig() before main() starts, whether or not config is ever used.","hint":"Ask when the initialization code runs, not just whether the variable exists.","explain":"Every top-level variable has a name from the start, but its initial value may be computed at a later moment. Think about what event triggers that computation.","source":"https://dart.dev/language/variables","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Top-level initializers don't run at startup; they wait until the variable is first used."},{"id":"b","text":"False","why":"Top-level variables are lazily initialized, so loadConfig() runs only when config is first used."}],"answer":"b","explanation":"Top-level and class variables are lazily initialized: the initialization code runs the first time the variable is used, not when the program starts.","concept":"Top-level and class variables are lazily initialized: the initialization code runs the first time the variable is used, not when the program starts.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Top-level initializers run on first use, so side effects in them happen late."]},{"id":"dartintermediate-003","courseId":"dart-intermediate","domain":"variables-and-operators","topic":"Cascade operator","prompt":"A developer runs the code below. What does the variable user hold afterwards?","hint":"Follow what the whole expression gives back, not what each step gives back.","explain":"A chain of operations can act on one target without spelling it out at each step. Consider what the whole construction hands back once the chain finishes, and whether the individual steps get a say.","source":"https://dart.dev/language/operators","interaction":{"type":"single"},"options":[{"id":"a","text":"The User object that User() created, with its fields set","why":"The constructor returns the object, and the cascade operates on it, ignoring the values the individual steps produce."},{"id":"b","text":"The value 31, the last assignment in the chain","whyNot":"The cascade doesn't pass along the last step's value; the expression yields the object it started from."},{"id":"c","text":"The value 'Ana', the first assignment in the chain","whyNot":"Each step runs on the same object, and the first assignment's value isn't what gets stored."},{"id":"d","text":"null, because assignments produce no value","whyNot":"The steps' values are ignored, and the variable receives the constructed object, not null."}],"answer":"a","explanation":"In a cascade, the value of the whole expression is the object the cascade started on; the values of the individual operations are ignored.","concept":"In a cascade, the value of the whole expression is the object the cascade started on; the values of the individual operations are ignored.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"var user = User()\n  ..name = 'Ana'\n  ..age = 31;","takeaways":["A cascade gives back its receiver, not the result of the last call."]},{"id":"dartintermediate-004","courseId":"dart-intermediate","domain":"variables-and-operators","topic":"??= assignment","prompt":"A settings class keeps `String? theme;`. Its load method runs `theme ??= 'light';`. Which TWO statements about this line are correct?","hint":"Compare what the operator does when the variable already has a value with when it doesn't.","explain":"An assignment can depend on the state of its target. Think about what should happen when the target already holds something, and what should happen when it holds nothing.","source":"https://dart.dev/language/operators","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"It sets theme to 'light' only when theme is currently null.","why":"The ??= operator writes only when the variable is null; otherwise the variable keeps its current value."},{"id":"b","text":"If theme already holds 'dark', the line leaves it as 'dark'.","why":"When the variable already has a value, the line does nothing to it."},{"id":"c","text":"It replaces theme whenever its current value is an empty string.","whyNot":"??= checks only for null; an empty string is a real value and is kept."},{"id":"d","text":"It throws an error if theme already holds a value.","whyNot":"Nothing is thrown; when a value is present the line simply leaves it alone."},{"id":"e","text":"It always writes 'light', then returns the old value of theme.","whyNot":"The write happens only when theme is null, so the variable is not always overwritten."},{"id":"f","text":"It makes theme non-nullable before assigning to it.","whyNot":"The variable's type doesn't change; the operator tests for null and assigns to the same variable."}],"answer":"ab","explanation":"b ??= value assigns only when b is null; otherwise b keeps its current value, and no error is raised.","concept":"b ??= value assigns only when b is null; otherwise b keeps its current value, and no error is raised.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["??= tests for null only; falsy values such as '' or 0 are kept."]},{"id":"dartintermediate-005","courseId":"dart-intermediate","domain":"variables-and-operators","topic":"Spread operator in collections","prompt":"After the code below runs, what does items contain?","hint":"Check whether each spread is null-aware, and what happens to null elements inside a list.","explain":"A spread unpacks the values of another collection into a literal. Some spread forms tolerate a missing collection. Think about what the spread does with each element of a collection that is present.","source":"https://dart.dev/language/collections","interaction":{"type":"single"},"options":[{"id":"a","text":"[0, 1, null, 3, 4]","why":"The null-aware spread adds nothing for the null list a, while the non-null list b is unpacked in full, null element included."},{"id":"b","text":"[0, 1, 3, 4]","whyNot":"Spreading a non-null collection keeps its null elements; only a null collection is skipped."},{"id":"c","text":"[0, null, 4]","whyNot":"A null collection contributes no elements at all, rather than a single null."},{"id":"d","text":"A compile-time error, because a nullable list can't be spread","whyNot":"The ...? form exists for nullable collections, so this line compiles."}],"answer":"a","explanation":"A null-aware spread inserts nothing when its collection is null, but null elements inside a non-null collection are still inserted. Spreading a nullable collection without ...? is an error.","concept":"A null-aware spread inserts nothing when its collection is null, but null elements inside a non-null collection are still inserted. Spreading a nullable collection without ...? is an error.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"List<int>? a = null;\nvar b = [1, null, 3];\nvar items = [0, ...?a, ...b, 4];","takeaways":["Use ...? for nullable collections; null elements inside a list are kept."]}]