[{"id":"javaadvanced-001","courseId":"java-advanced","domain":"language-basics","topic":"Interned compile-time string constants","prompt":"A Java program uses a String that is a constant expression, such as \"ja\" + \"va\". What does the Java Language Specification say happens to that String's instance?","hint":"Think about whether the language gives such Strings one shared instance or a fresh one each time.","explain":"Some String values are fixed at compile time, and the language treats them specially. The == operator compares references, not text, so how these values are shared decides its result.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.29","interaction":{"type":"single"},"options":[{"id":"a","text":"It is copied into a new String object each time the expression is evaluated.","whyNot":"Constant String expressions are shared, so the language does not create a fresh object for each evaluation."},{"id":"b","text":"It is always interned, so it shares one unique instance, as String.intern does.","why":"The specification says constant expressions of type String are always interned so as to share unique instances, using String.intern."},{"id":"c","text":"It is interned only when the variable that holds it is declared final.","whyNot":"The rule says always, with no condition about final variables; a final declaration is not required."},{"id":"d","text":"It is interned only when it is used as a switch case label.","whyNot":"Case labels need constant expressions, but the interning rule covers every constant String expression, not just case labels."}],"answer":"b","explanation":"Constant expressions of type String are always interned so as to share unique instances, using the method String.intern.","concept":"Constant expressions of type String are always interned so as to share unique instances, using the method String.intern.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Constant String expressions are always interned, so they share one unique instance.","Use equals to compare string contents; == checks identity, which interning can make true."]},{"id":"javaadvanced-002","courseId":"java-advanced","domain":"language-basics","topic":"Integer overflow","prompt":"In Java, `int x = Integer.MAX_VALUE + 1;` leaves x holding a negative number, and no exception is thrown.","hint":"Consider what integer addition does when the sum is too large for int.","explain":"Java int arithmetic has a fixed width, so a sum can exceed what an int can hold. The question is whether the language reports that, or produces some value anyway.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.18.2","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Overflow keeps only the low-order bits of the true sum, which flips the sign here; the addition never throws."},{"id":"b","text":"False","whyNot":"The statement holds: integer addition wraps around silently, and the additive operators never throw a run-time exception."}],"answer":"a","explanation":"Integer addition that overflows yields the low-order bits of the true sum, which can have the opposite sign; numeric additive operators never throw a run-time exception.","concept":"Integer addition that overflows yields the low-order bits of the true sum, which can have the opposite sign; numeric additive operators never throw a run-time exception.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Integer overflow in Java wraps silently and never throws.","When a range matters, check it yourself, because the arithmetic itself will not report it."]},{"id":"javaadvanced-003","courseId":"java-advanced","domain":"language-basics","topic":"Shift distance masking","prompt":"In Java 21, a method runs the code below. What does the print statement output?","hint":"Find out how many bits of the shift distance an int shift actually uses.","explain":"A shift operator reads only some low-order bits of its right operand as the distance. How many bits it reads depends on the promoted type of the left operand.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.19","interaction":{"type":"single"},"options":[{"id":"a","text":"0","whyNot":"Shifting past the width does not clear the value; the distance is first reduced to a small number."},{"id":"b","text":"2","why":"For an int, only the low five bits of 33 are used, so the distance is 1, and 1 shifted left once is 2."},{"id":"c","text":"1","whyNot":"The shift is not skipped; the reduced distance of 1 still moves the bit."},{"id":"d","text":"8589934592","whyNot":"That is 2 to the power 33, which would need a long shift; the left operand is an int, so the result stays int."}],"answer":"b","explanation":"For int shifts Java masks the distance with 0x1f, so 33 becomes 1; for long it masks with 0x3f. An out-of-range distance never throws.","concept":"For int shifts Java masks the distance with 0x1f, so 33 becomes 1; for long it masks with 0x3f. An out-of-range distance never throws.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"int x = 1;\nSystem.out.println(x << 33);","takeaways":["Shift distances are masked: five bits for int, six bits for long.","A shift distance at or beyond the width does not throw; it is silently reduced."]},{"id":"javaadvanced-004","courseId":"java-advanced","domain":"language-basics","topic":"Unicode escapes","prompt":"A method runs the code below. What does it print?","hint":"Count the backslashes that come just before the u.","explain":"Java translates Unicode escapes in the raw source before string literals are read. A backslash can start a Unicode escape only when an even number of backslashes come before it.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-3.html#jls-3.3","interaction":{"type":"single"},"options":[{"id":"a","text":"\\u005a","why":"The second backslash cannot start a Unicode escape, so u005a stays plain text, and the literal turns the doubled backslash into one backslash."},{"id":"b","text":"Z","whyNot":"Unicode escapes are read before string literals, but the second backslash blocks this escape, so no Z is produced."},{"id":"c","text":"Compile error: \\u is an illegal escape in a string literal","whyNot":"The doubled backslash becomes one literal backslash, so the parser never sees an illegal escape."},{"id":"d","text":"Two backslashes, then u005a","whyNot":"The doubled backslash is one escape that yields a single backslash, so only one backslash prints."}],"answer":"a","explanation":"Unicode escapes are processed before string literals. The second of two adjacent backslashes is not eligible to start one, so u005a survives as six plain characters.","concept":"Unicode escapes are processed before string literals. The second of two adjacent backslashes is not eligible to start one, so u005a survives as six plain characters.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"String s = \"\\\\u005a\";\nSystem.out.println(s);","takeaways":["In a string literal, a doubled backslash is needed to write a literal backslash followed by u.","Unicode escapes are translated before string literals, so backslash counting matters."]},{"id":"javaadvanced-005","courseId":"java-advanced","domain":"language-basics","topic":"Operand evaluation order","prompt":"A method runs the code below. What does it print?","hint":"Java fixes the order in which the two operands of + are evaluated.","explain":"Java fixes the order in which the operands of an operator are evaluated. An assignment inside one operand takes effect before the later operands are evaluated.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.7","interaction":{"type":"single"},"options":[{"id":"a","text":"2","whyNot":"That would need the right x read before the assignment, but the left operand is fully evaluated first."},{"id":"b","text":"5","whyNot":"This reads the right x as 1, but the assignment in the left operand runs before the right operand is read."},{"id":"c","text":"8","why":"The left operand completes first, leaving x as 4, so the right operand reads 4 and the sum is 8."},{"id":"d","text":"Compile error: x is assigned and read in one expression","whyNot":"Java allows an assignment inside an operand and a read of the same variable in the same expression."}],"answer":"c","explanation":"Operands are evaluated left to right, and the left operand completes before the right begins, so the assignment in (x = 4) is visible to the right-hand x.","concept":"Operands are evaluated left to right, and the left operand completes before the right begins, so the assignment in (x = 4) is visible to the right-hand x.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"int x = 1;\nint y = (x = 4) + x;\nSystem.out.println(y);","takeaways":["Java guarantees left-to-right evaluation of operands.","Side effects inside one operand can change what later operands read; avoid relying on that."]}]