[{"id":"kotlinintermediate-001","courseId":"kotlin-intermediate","domain":"basics-and-types","topic":"Smart casts after is checks","prompt":"A function takes an Any parameter s. Why does s.isEmpty() compile on the right side of the || in this condition?","hint":"Look at where the type check sits relative to the operator.","explain":"A type check tells the compiler what a value is at a given point, and that knowledge can carry into later parts of the same expression. Think about where in an expression that knowledge can reach.","source":"https://kotlinlang.org/docs/typecasts.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Any has an isEmpty function, so no check is needed.","whyNot":"isEmpty belongs to String, not Any; the compiler needs s to be a String before allowing that call."},{"id":"b","text":"A type check on the left of || lets the compiler smart cast s to String on the right-hand side.","why":"A type check on the left of || lets the right-hand operand use the value as the checked type; a negative check works this way too."},{"id":"c","text":"The !is check converts s into a String, so the right side sees a new value.","whyNot":"A check never changes the value; it only tells the compiler what type the value has at that point."},{"id":"d","text":"The compiler skips type checking the right side of ||, so isEmpty is never verified.","whyNot":"The compiler checks every member access, so isEmpty must be valid for the type it knows at that point."}],"answer":"b","explanation":"A type check on the left of || lets the compiler smart cast the value on the right-hand side of that operator.","concept":"A type check on the left of || lets the compiler smart cast the value on the right-hand side of that operator.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"fun check(s: Any) {\n    if (s !is String || s.isEmpty()) return\n}","takeaways":["A type check on the left of || lets the right-hand operand use the checked type.","A check narrows the compiler's view of the type; it does not convert the value."]},{"id":"kotlinintermediate-002","courseId":"kotlin-intermediate","domain":"basics-and-types","topic":"Evaluation order of a call's arguments","prompt":"A call greeter.greet(name(), tag = label()) reaches greet(name: String, times: Int = fallback(), tag: String). Put these steps of the call in the order Kotlin runs them.","hint":"Think about what must be computed before the function can be invoked.","explain":"A call is evaluated in stages before the function body runs. Think about which of its values the call site writes out and which ones the declaration supplies.","source":"https://kotlinlang.org/spec/expressions.html","interaction":{"type":"sort","items":[{"text":"The explicit receiver greeter is evaluated","why":"A call begins with its explicit receiver when one is written."},{"text":"The written arguments name() and label() are evaluated, left to right","why":"Arguments given at the call site are evaluated in the order they appear."},{"text":"The default for times, fallback(), is evaluated","why":"Defaults for omitted parameters are evaluated after all the provided arguments."},{"text":"The function greet itself is invoked","why":"The function is invoked last, once every argument value is ready."}]},"options":[],"answer":"o:abcd","explanation":"Kotlin evaluates the explicit receiver, then the written arguments left to right, then defaults for omitted parameters, and invokes the function last.","concept":"Kotlin evaluates the explicit receiver, then the written arguments left to right, then defaults for omitted parameters, and invokes the function last.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Arguments written in a call are evaluated left to right before the function is invoked.","Defaults for omitted parameters are evaluated after the arguments written in the call."]},{"id":"kotlinintermediate-003","courseId":"kotlin-intermediate","domain":"basics-and-types","topic":"Unsafe cast with as","prompt":"A value is declared as Any and holds a String. The next line casts it with as Int. What happens when that line runs?","hint":"Compare plain as with as? when the value does not match the target type.","explain":"The as operator casts a value to a target type. What happens when the value does not match that type depends on which cast operator is written.","source":"https://kotlinlang.org/docs/typecasts.html","interaction":{"type":"single"},"options":[{"id":"a","text":"wrongCast becomes null and the program keeps running.","whyNot":"That is the behaviour of as?; plain as throws when the cast fails."},{"id":"b","text":"The program throws a ClassCastException at runtime.","why":"A failed as cast throws ClassCastException, which is why as is called the unsafe operator."},{"id":"c","text":"wrongCast is set to 0, the default value for Int.","whyNot":"A failed cast never produces a default number; the cast either succeeds or fails."},{"id":"d","text":"The compiler refuses to build the file.","whyNot":"Any can hold a String, so the compiler accepts the cast and only the runtime check can fail."}],"answer":"b","explanation":"as is the unsafe cast operator: when the value is not of the target type, it throws ClassCastException.","concept":"as is the unsafe cast operator: when the value is not of the target type, it throws ClassCastException.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"val rawInput: Any = \"user-1234\"\nval wrongCast = rawInput as Int","takeaways":["A failed as cast throws ClassCastException at runtime.","Use as? when a failed cast should give null instead of an exception."]},{"id":"kotlinintermediate-004","courseId":"kotlin-intermediate","domain":"basics-and-types","topic":"Safe cast with as?","prompt":"Because the target is `String?`, the expression `config[\"loginAttempts\"] as String?` returns null instead of throwing when the stored value is the Int 3.","hint":"Ask which operator decides what a failed cast produces.","explain":"A failed cast either throws an exception or produces null. Read which cast operator the expression uses, not only the type it casts to.","source":"https://kotlinlang.org/docs/typecasts.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Making the target type nullable does not change failure handling; plain as still throws ClassCastException when the cast fails."},{"id":"b","text":"False","why":"A failed plain as cast throws ClassCastException even with String?; only as? returns null on failure."}],"answer":"b","explanation":"Plain as always throws on a failed cast, even to a nullable type; as? is the form that yields null instead.","concept":"Plain as always throws on a failed cast, even to a nullable type; as? is the form that yields null instead.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A nullable target type does not make as safe.","Choose as? to get null instead of an exception when a cast may fail."]},{"id":"kotlinintermediate-005","courseId":"kotlin-intermediate","domain":"basics-and-types","topic":"Not-null assertion !!","prompt":"A nullable variable holds null, and the next line reads its length with !!. What happens when this runs?","hint":"Think about what the double bang operator asserts about the value.","explain":"A nullable type can hold null, and the compiler will not let you use it as non-null without an explicit operator. Consider what that operator promises at runtime.","source":"https://kotlinlang.org/docs/null-safety.html","interaction":{"type":"single"},"options":[{"id":"a","text":"l is set to 0 and the program keeps running.","whyNot":"!! does not substitute a default; it asserts a non-null value and fails when that assertion is false."},{"id":"b","text":"l is set to null, because b is null.","whyNot":"!! does not pass null along; it treats the value as non-null, so the access fails instead."},{"id":"c","text":"The program throws a NullPointerException.","why":"The !! operator forces a nullable value to non-null, and reading length from null throws a NullPointerException."},{"id":"d","text":"The compiler reports an error and nothing runs.","whyNot":"The compiler accepts !!; the failure only appears at runtime, when the null value is actually used."}],"answer":"c","explanation":"The !! operator converts a nullable value to non-null without checking; if the value is null, it throws a NullPointerException.","concept":"The !! operator converts a nullable value to non-null without checking; if the value is null, it throws a NullPointerException.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"val b: String? = null\nval l = b!!.length","takeaways":["!! turns a null at that point into a NullPointerException.","Use !! only when you are certain the value cannot be null."]}]