[{"id":"javaintermediate-001","courseId":"java-intermediate","domain":"language-basics","topic":"Integer cache for boxed values","prompt":"A hot loop turns many int values into Integer objects. Which call is generally the better choice for that boxing, according to the Integer docs?","hint":"Compare the static factory method with the constructor.","explain":"Boxing an int gives you an Integer object, and the Integer class offers more than one way to get one. The methods differ in how much memory and time they cost, so the docs compare them.","source":"https://docs.oracle.com/en/java/javase/27/docs/api/java.base/java/lang/Integer.html#valueOf(int)","interaction":{"type":"single"},"options":[{"id":"a","text":"Integer.valueOf(n)","why":"The docs say valueOf(int) is generally the better choice, since it is likely to give significantly better space and time performance."},{"id":"b","text":"new Integer(n)","whyNot":"The constructor builds a newly allocated Integer object, and the docs recommend valueOf(int) over it for space and time."},{"id":"c","text":"Integer.toHexString(n)","whyNot":"toHexString returns a String of hexadecimal digits, not an Integer object."},{"id":"d","text":"Integer.parseInt(n)","whyNot":"parseInt(String) converts a String into a primitive int; it does not create an Integer object."}],"answer":"a","explanation":"Integer.valueOf(int) is generally preferred over the Integer(int) constructor, since it is likely to give significantly better space and time performance.","concept":"Integer.valueOf(int) is generally preferred over the Integer(int) constructor, since it is likely to give significantly better space and time performance.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Box ints with Integer.valueOf(int) rather than the Integer constructor."]},{"id":"javaintermediate-002","courseId":"java-intermediate","domain":"language-basics","topic":"Switch expressions","prompt":"A switch expression picks one of its rules and produces a value. Put these steps of evaluating it in order.","hint":"Think about what must exist before a rule can be chosen.","explain":"A switch expression yields one value by matching a selector against its labels. Only the matched rule's code runs, and the expression takes its value from that rule.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.28.1","interaction":{"type":"sort","items":[{"text":"Evaluate the selector expression.","why":"Matching can't start until the selector has a value, so this comes first."},{"text":"Find which switch label applies to that value.","why":"Labels are checked only after the selector's value is known."},{"text":"Evaluate the matched rule's expression, or run its block or throw.","why":"Only the rule that matched runs; the other rules are never executed."},{"text":"Yield the rule's value as the result of the whole switch expression.","why":"Once the matched rule has produced its value, that value is the result of the switch."}]},"options":[],"answer":"o:abcd","explanation":"A switch expression evaluates its selector first, then finds the label that applies, then evaluates only the matched rule and completes with its value.","concept":"A switch expression evaluates its selector first, then finds the label that applies, then evaluates only the matched rule and completes with its value.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A switch expression runs only the rule its selector value matches."]},{"id":"javaintermediate-003","courseId":"java-intermediate","domain":"language-basics","topic":"Exhaustive switch","prompt":"A developer writes switches over enums, sealed types and guarded labels. Which TWO statements about exhaustiveness are true?","hint":"Consider which kinds of case coverage the compiler accepts.","explain":"Exhaustiveness is judged against the selector's type, not just the labels written. The rules name the ways a switch block can meet it, and only some kinds of label are counted.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-14.html#jls-14.11.1.1","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"A switch block with a default label is exhaustive only if it also lists every enum constant.","whyNot":"A default label alone makes the switch block exhaustive, so listing every constant is not also needed."},{"id":"b","text":"An enum switch expression that lists every constant needs no default label.","why":"The spec says a default is permitted but not required when all enum constant names appear as case constants."},{"id":"c","text":"A switch statement over an enum must always have a default label, even when every constant is listed.","whyNot":"A default is not required when every enum constant is listed as a case constant; the same exhaustiveness rule covers statements."},{"id":"d","text":"A switch block with a default label is exhaustive for its selector.","why":"The spec lists a default label as one way a switch block is exhaustive for its selector expression."},{"id":"e","text":"A guarded case label counts toward exhaustiveness, because its when test only narrows a type that is already covered.","whyNot":"Only unguarded case labels count toward exhaustiveness, so a guarded label adds nothing to the coverage."},{"id":"f","text":"A switch over a sealed interface always needs a default label, even when a case covers each permitted subtype.","whyNot":"The spec's sealed example has no default and is exhaustive; a default is permitted, not required."}],"answer":"bd","explanation":"A switch block is exhaustive with a default label, or when its unguarded case labels cover the selector type, such as all enum constants; a default is then not required.","concept":"A switch block is exhaustive with a default label, or when its unguarded case labels cover the selector type, such as all enum constants; a default is then not required.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A default label makes a switch block exhaustive, and listing every enum constant does too."]},{"id":"javaintermediate-004","courseId":"java-intermediate","domain":"language-basics","topic":"Pattern matching for instanceof","prompt":"With Object obj = null, the test obj instanceof String s evaluates to true, so the if-block runs with s set to null.","hint":"Think about what instanceof returns for a null reference.","explain":"The instanceof operator tests a value against a type, and the pattern form also declares a variable. That variable is set only when the test succeeds, so the null case matters.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.20.2","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Wrong: for a null reference, instanceof returns false, so the pattern is not matched."},{"id":"b","text":"False","why":"Right: a null reference gives false from instanceof, so the pattern does not match and the block is skipped."}],"answer":"b","explanation":"For instanceof, a null reference always gives false, so a pattern test never matches null and its pattern variable is not initialized.","concept":"For instanceof, a null reference always gives false, so a pattern test never matches null and its pattern variable is not initialized.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"Object obj = null;\nif (obj instanceof String s) {\n    // does this run?\n}","takeaways":["A pattern instanceof test on null returns false, so the variable is never bound to null."]},{"id":"javaintermediate-005","courseId":"java-intermediate","domain":"language-basics","topic":"Guarded case labels","prompt":"A switch over an Object has a guarded label, case String s when s.isEmpty(), and an unguarded label, case Integer i. Which labels count toward exhaustiveness?","hint":"Think about which labels can match a value with no extra condition.","explain":"A case label can carry a when clause that adds a further test. Exhaustiveness asks whether the switch covers every possible selector value without leaving gaps.","source":"https://docs.oracle.com/javase/specs/jls/se27/html/jls-14.html#jls-14.11.1","interaction":{"type":"single"},"options":[{"id":"a","text":"Only the guarded labels, since their extra tests are more precise.","whyNot":"A guard adds a condition, and guarded labels are left out of the exhaustiveness check entirely."},{"id":"b","text":"Only the unguarded labels.","why":"A label with no when clause, or with a guard that is the constant true, is unguarded and is the only kind that counts."},{"id":"c","text":"Both kinds, because a guard only narrows a pattern that already covers its type.","whyNot":"A guarded label does not count at all, so narrowing it adds nothing to the coverage."},{"id":"d","text":"Only the default label, since it is the only one that covers the type.","whyNot":"A default is one way to be exhaustive, but unguarded case labels that cover the type count too."}],"answer":"b","explanation":"Only unguarded case labels count toward a switch's exhaustiveness; a guarded label never adds to the coverage of the selector type.","concept":"Only unguarded case labels count toward a switch's exhaustiveness; a guarded label never adds to the coverage of the selector type.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"switch (obj) {\n  case String s when s.isEmpty() -> ...\n  case Integer i -> ...\n}","takeaways":["A guarded case label does not count toward exhaustiveness; add an unguarded label for the full type."]}]