[{"id":"kotlinadvanced-001","courseId":"kotlin-advanced","domain":"basics-and-types","topic":"Boxed number caching","prompt":"A JVM service runs the code below and prints false. Which statement explains it?","hint":"Think about what === checks for objects, and when the JVM reuses them.","explain":"A nullable number on the JVM is stored as an object, a box. === compares object identity, while == compares values. The JVM shares boxes only for some values, so check which values qualify.","source":"https://kotlinlang.org/docs/numbers.html","interaction":{"type":"single"},"options":[{"id":"a","text":"5000 is outside the JVM's cache range, so each box is a separate object.","why":"Boxed values outside -128 to 127 are separate objects, so === reports false even though == reports true."},{"id":"b","text":"Nullable Int variables hold primitives, so === compares only the numbers.","whyNot":"A nullable numeric type is boxed into an object on the JVM, so === compares identity, not the number."},{"id":"c","text":"=== does not compile on Int?, so the printed false should never appear.","whyNot":"The comparison compiles; === is allowed on nullable numeric values."},{"id":"d","text":"Both variables come from the same source, so they always share one object.","whyNot":"Each assignment to Int? boxes the value; outside the cache, separate boxes stay separate even when they come from one source."}],"answer":"a","explanation":"On the JVM, boxed numbers outside the cached range -128 to 127 are separate objects, so === can be false while == is true.","concept":"On the JVM, boxed numbers outside the cached range -128 to 127 are separate objects, so === can be false while == is true.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"val limit: Int = 5000\nval a: Int? = limit\nval b: Int? = limit\nprintln(a === b)","takeaways":["Compare nullable numbers with ==; === depends on the JVM's box cache, not on the value."]},{"id":"kotlinadvanced-002","courseId":"kotlin-advanced","domain":"basics-and-types","topic":"Floating-point equality in generic code","prompt":"A helper `fun same(a: Any, b: Any) = a == b` is called as same(0.0, -0.0). It returns false, even though 0.0 == -0.0 is true when both operands are typed Double.","hint":"Ask which equality runs when the static type is Any.","explain":"The == operator follows different rules depending on static type. Floating-point operands typed as Double use IEEE 754 comparison; generic code uses the equals() of the boxed value instead.","source":"https://kotlinlang.org/docs/numbers.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Through Any, Kotlin uses Double.equals(), which treats -0.0 and 0.0 as different values."},{"id":"b","text":"False","whyNot":"This would hold only if generic equality followed the IEEE rule, where 0.0 and -0.0 compare equal."}],"answer":"a","explanation":"In generic code, Kotlin uses Double.equals(), which says 0.0 and -0.0 differ; statically typed Double == follows IEEE 754 and says they are equal.","concept":"In generic code, Kotlin uses Double.equals(), which says 0.0 and -0.0 differ; statically typed Double == follows IEEE 754 and says they are equal.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Floating-point == depends on static type: generic code uses equals(), which treats NaN as equal to itself."]},{"id":"kotlinadvanced-003","courseId":"kotlin-advanced","domain":"basics-and-types","topic":"Integer overflow","prompt":"A payment counter stores a maximum Int and adds one on the JVM, as in the code below. What does it print?","hint":"Think about the largest value an Int can hold and what arithmetic does past it.","explain":"Each integer type stores only values inside a fixed range. An arithmetic result outside that range is still stored in the same type, so the outcome depends on how the bits are kept.","source":"https://kotlinlang.org/docs/numbers.html","interaction":{"type":"single"},"options":[{"id":"a","text":"-2147483648","why":"The result exceeds the Int range and wraps around to the negative end; the compiler does not report overflow."},{"id":"b","text":"2147483648","whyNot":"Arithmetic on Int stays Int; the result is not widened to a bigger type."},{"id":"c","text":"2147483647","whyNot":"Integer overflow wraps around; the value is not clamped at the maximum."},{"id":"d","text":"The compiler reports an overflow error","whyNot":"The compiler does not produce an error for integer overflow; the code compiles and runs."}],"answer":"a","explanation":"Integer arithmetic past the type's range wraps around silently: Int + 1 at the maximum Int gives the negative minimum, with no compile error.","concept":"Integer arithmetic past the type's range wraps around silently: Int + 1 at the maximum Int gives the negative minimum, with no compile error.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"val count: Int = 2147483647\nprintln(count + 1)","takeaways":["Integer overflow wraps around without a compile error, so guard values that can grow large."]},{"id":"kotlinadvanced-004","courseId":"kotlin-advanced","domain":"basics-and-types","topic":"Narrowing conversions","prompt":"A developer converts numeric values in a Kotlin program. Which TWO statements about these conversions are true?","hint":"Consider what a smaller integer type can hold, and how decimals are handled.","explain":"A smaller integer type has a narrower range, so converting a larger value into it can keep only part of the original bits. Converting between floating-point and integer types follows its own rule for the leftover part.","source":"https://kotlinlang.org/docs/numbers.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Converting the Int 130 with toByte() and printing it shows -126.","why":"130 does not fit in a Byte's range, so the stored value wraps to -126."},{"id":"b","text":"Converting the Double 1.5 with toLong() gives 1, because the fraction is discarded.","why":"Converting a floating-point value to an integer type discards the fractional part."},{"id":"c","text":"Converting 130 with toByte() makes the compiler report an error.","whyNot":"The narrowing call compiles; it produces a changed value rather than a compile error."},{"id":"d","text":"Narrowing keeps the number unchanged whenever the value is positive.","whyNot":"Only values inside the smaller type's range survive unchanged; a positive 130 becomes negative as a Byte."},{"id":"e","text":"Double.MAX_VALUE * 2 throws an overflow exception at runtime.","whyNot":"Floating-point results follow IEEE 754, so very large values become Infinity instead of throwing."},{"id":"f","text":"Converting a too-large Long with toInt() throws an exception at runtime.","whyNot":"Narrowing can change the value but does not throw; the result is simply a different number."}],"answer":"ab","explanation":"A narrowing conversion can change the value (130 becomes -126 as Byte), and converting a Double to an integer type discards the fractional part.","concept":"A narrowing conversion can change the value (130 becomes -126 as Byte), and converting a Double to an integer type discards the fractional part.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Narrowing can silently change values; Double-to-integer conversion discards the decimals."]},{"id":"kotlinadvanced-005","courseId":"kotlin-advanced","domain":"basics-and-types","topic":"Value class boxing","prompt":"A value class Foo wraps an Int, and the generic function id(x: T): T returns x. Put these steps of what happens to f in `val c = id(f)`, from first to last.","hint":"Track where the value is wrapped and where it is unwrapped.","explain":"Wrapping a value in a class can add heap allocations, which Kotlin tries to avoid. The compiler picks a plain representation or an object depending on how the value is used at each point.","source":"https://kotlinlang.org/docs/inline-classes.html","interaction":{"type":"sort","items":[{"text":"Create f as Foo(42)","why":"Inline classes are kept as their plain value when possible, so no wrapper object exists yet."},{"text":"Pass f into id, whose parameter is the generic type T","why":"Using f as a generic type boxes it into a wrapper object."},{"text":"id returns, and the value is unboxed back to Foo","why":"Returning the value out of the generic call unboxes it to the plain form."},{"text":"c holds the plain value 42 at the end","why":"After the round trip, c contains the unboxed representation, just 42."}]},"options":[],"answer":"o:abcd","explanation":"Inline classes stay unboxed when possible, but a generic call boxes them on the way in and unboxes them when the result comes back.","concept":"Inline classes stay unboxed when possible, but a generic call boxes them on the way in and unboxes them when the result comes back.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"@JvmInline\nvalue class Foo(val i: Int)\n\nfun <T> id(x: T): T = x\n\nfun main() {\n    val f = Foo(42)\n    val c = id(f)\n}","takeaways":["A value class is boxed whenever it is used as another type, such as a generic T."]}]