Community Q&A
1Z0-829 — frequently asked questions
Community-vetted answers to 64 common questions about this exam, each traced back to a specific practice question.
Questions from real practice questions
Each Q&A comes from a specific question — follow the link for the full analysisJava Stream reduce with Identity and Combiner
Why does the first reduce produce "wTablewChair"?
In parallel streams, the combiner merges partial results. Here, it combines "wTable" and "wChair".
Is the second reduce result deterministic?
Yes, since it uses a two-argument reduce without a combiner on a small list, the order of addition doesn't change the final string for this specific case.
Java Concurrent Thread Execution Order
Does volatile ensure thread execution order?
No. Volatile ensures visibility of changes across threads but does not impose any ordering or mutual exclusion on execution flow.
Why doesn't AtomicInteger cause blocking?
AtomicInteger uses CAS (Compare-And-Swap) instructions at the hardware level, allowing non-blocking increments without acquiring locks.
Java Method Overloading with Binary Literals
Does a binary literal ever default to long?
Only if it is explicitly suffixed with an uppercase L (e.g., 0b1101_1010L). Otherwise, it is always an int.
Why doesn't the large value cause overflow or exception?
The value 0b1101_1010 (218 decimal) fits comfortably within the positive range of a 32-bit signed int.
Java Sealed Interfaces Compilation Rules
Why can't I use 'sealed' for the implementing interface?
You can, but it restricts further extension to only the permitted subclasses listed in the permits clause. Use 'non-sealed' to allow unrestricted implementation.
Is 'public' required for a sealed interface?
No, 'public' is optional. The critical requirement is the sealing modifier (sealed, non-sealed, or final) when extending another sealed type.
Java instanceof Pattern Matching and Method Invocation
Why does v.mB() print from class B if v is actually a C instance?
Because C does not override mB(), the inherited implementation from B is used. Polymorphism only applies to overridden methods.
Can I call v.mC() directly after binding v to B in the first if?
No. The variable v is statically typed as B. To call mC(), you must re-bind v to a variable of type C using another instanceof check, as shown with v1.
FileReader buffer read behavior in Java
Why doesn't the second read overwrite the first output?
It does append. The first write adds '01234567'. The second write adds '89'. The total is '0123456789'.
What happens if I use out.write(buffer) instead of out.write(buffer, 0, count)?
It would write the entire 8-char buffer every time, including old data, leading to corrupted output like '0123456789234567'.
Java Text Blocks and String Equality
Why does myTextBlk1 equal myStr but myTextBlk2 does not?
myTextBlk1's closing quotes are on the same line as the text, so no newline is added. myTextBlk2's closing quotes are on a new line, adding an implicit newline.
Does String.intern() make different text blocks equal?
No. intern() ensures that strings with identical values share the same reference, but it does not change the value of the string itself.
Java String Immutability and indexOf Result
Does String.replace() modify the original string?
No, Strings are immutable. replace() returns a new string; the original variable remains unchanged unless reassigned.
Why does indexOf return 7 again?
Because the variable 'a' was never updated with the result of replace(), so it still points to the original string containing 'Java' at index 7.
Java Thread Waiting State and InterruptedException Handling
Does a thread in waiting state consume CPU?
No. Threads in waiting or blocked states are inactive and yield the CPU to other threads until they become runnable.
Where does a thread go after a timed wait expires?
It moves to the runnable state to contend for the monitor lock or CPU resources, not directly to terminated.
Java Switch Statement Output Without Break
Why doesn't the switch statement stop after the first matching case?
In Java, switch statements do not automatically stop after a match unless a break statement is explicitly used. Without break, execution falls through to the next case.
Is compilation failure expected when break is missing in switch?
No, missing break statements are valid Java syntax. They enable intentional fall-through behavior, so the code compiles successfully.
Java Switch Expression Result Analysis
Why doesn't the switch fall through to other cases?
Switch expressions use the arrow (->) syntax and automatically terminate after the first match, unlike switch statements which require break to prevent fall-through.
Is a break statement needed in switch expressions?
No, break statements are not required in switch expressions. They are designed to return a value immediately upon matching a case.
Java Stream reduce with Identity vs Accumulator
Why does parallelStream().reduce(5, ...) add 5 multiple times?
In parallel streams, the accumulator is applied to partitions. Each partition starts with the identity, so 5 is added per partition before combining results.
Is Integer::sum safe for parallel reduce?
Yes, addition is associative. However, using the identity argument incorrectly causes it to be added to each partition's initial state, inflating the total.
Java Parameter Shadowing and this Keyword
Why does x++ not affect this.x?
Because x++ modifies the local parameter variable, which shadows the instance variable. The instance variable is untouched.
What is the purpose of this.x?
It explicitly references the instance field of the current object, bypassing any local variable with the same name.
Java Collections.binarySearch Unsorted List Behavior
Why does binarySearch return 1 for the unsorted list?
It returns 1 by coincidence because the element 'e3' is located at index 1 (the midpoint) of the array [e1, e3, e2]. This is undefined behavior and not guaranteed.
Can I use binarySearch without sorting the list first?
No. The Java documentation explicitly requires the list to be sorted in ascending order. Using it on an unsorted list produces undefined results.
Java Numeric Literals and Operators for 1Z0-829
Why do parseInt and valueOf fail with underscores?
String parsing methods do not accept underscore separators; they are only supported in source code numeric literals.
Is =+ a valid operator in Java?
No, =+ is not a standard compound operator. It is likely a typo for += in the exam question text.
Java Bitwise and Logical Operators Result
Why is the first output 'false 0' instead of 'false -1'?
The variable c is incremented via c++. It starts at -1 (from XOR), so the comparison uses -1, then c becomes 0. The printed value is the post-incremented c.
What is the difference between & and && in this context?
& always evaluates both operands. && short-circuits if the left operand is false. Here, the second line uses &&, but both sides were true anyway.
Automatic Module Naming from JAR File
How does JPMS handle version numbers in automatic module names?
Version numbers (typically trailing digits and hyphens) are stripped from the filename before transforming special characters.
Why is display.ascii correct instead of display-ascii?
JPMS requires that all non-alphanumeric characters in the resulting name be replaced with dots to form valid module names.
Java Module Migration: Top-Down vs Bottom-Up
Why is bottom-up migration different from top-down?
Bottom-up migration starts from leaf modules and moves up, potentially keeping unnamed modules on the classpath until dependencies are resolved. Top-down moves all modules to the module path from the start.
Can unnamed modules be used in top-down migration?
Yes, but they must be converted to automatic modules or named modules and placed on the module path. The key is that all modules end up on the module path in top-down migration.
Java ZonedDateTime DST Offset Transition Logic
Why does the offset change when adding an hour?
Because the transition back to standard time means the same local hour exists in two different UTC offsets. Advancing the instant moves you to the later occurrence.
How does Java handle ambiguous times in ZonedDateTime?
It uses the Smarter Algorithm by default, preferring the earlier offset for ambiguous times unless specified otherwise, affecting how arithmetic operations resolve.
Java Files.readAllLines Indexing Behavior
Why does accessing index 3 cause an exception?
Because the list has 3 elements, valid indices are 0, 1, and 2. Index 3 is out of bounds.
Does readAllLines return 1-based indexing?
No, it returns a standard Java List which is strictly zero-based.
Java Method Overloading with Double and Float Arguments
Why doesn't sum(int, float) match 10 and 15.25?
Java does not implicitly narrow a double literal (15.25) to a float. It must be explicitly cast or suffixed with 'f'.
When does the varargs method get called?
It is called when no other more specific method signature matches the provided arguments, such as when all args are doubles.
Java Method Overloading Resolution by Declared Type
Does the runtime type matter for overloaded methods?
No. Overloaded methods are resolved at compile time based on the declared parameter types, regardless of the actual object instance.
Why is 'Compilation fails' incorrect here?
The code is valid Java. The compiler can clearly distinguish between the two runs methods by their argument types Animal and Dog.
Java Interface Static and Private Method Invocation
Why can't we call IFace.m4()?
Private methods in interfaces have package-private-like scope restricted to the interface itself, so they cannot be called from outside.
Can we call static methods via an instance?
No, static methods must be called using the interface name (e.g., IFace.m3()). Calling them via an instance is a compilation error.
Java Instant truncatedTo ChronoUnit MINUTES Result
Does truncatedTo round or truncate?
It truncates (chops off) lower units. It does not round to the nearest unit.
Why doesn't it print the logged out message?
Because the login time is strictly before the logout time, the !isBefore check fails, triggering the else block.
Java Serialization readObject Access Modifier
Can readObject be protected?
No, it must be strictly private to be recognized by the serialization runtime.
What happens to transient fields if readObject is ignored?
They are not restored from the stream and revert to their default initialization values (e.g., 0.0 for double).
Java Serialization Constructor Execution and Output
Why doesn't the constructor run during deserialization?
Java deserialization restores object state directly from the byte stream without invoking constructors to ensure security and efficiency, bypassing initialization logic.
Can we make constructors run during deserialization?
Not automatically. You would need to implement a custom readObject method that manually triggers constructor-like logic, but this is rare and complex.
Java AutoCloseable and Exception Handling Modifications
Why can't I use 'extends' for AutoCloseable?
AutoCloseable is an interface, so you must use 'implements'. 'extends' is only for classes.
What happens if I don't override close()?
The code might compile but won't print 'Close'. The try-with-resources calls close(), so it must be implemented.
Java Duration and Period ISO-8601 Output Format
Why does Duration.ofMillis(5000) print as PT5S?
ISO-8601 standardizes duration representation. 5000 milliseconds is exactly 5 seconds, so it converts to the base unit 'S' prefixed by 'PT'.
Does Period include a 'T' separator like Duration?
No. Period uses only the 'P' prefix (e.g., P6D) because it represents calendar-based dates. Duration uses 'PT' to separate the date part from the time part.
Java Reference Assignment and Null Handling
Does p = null affect p1?
No. p = null only sets the variable p to null. p1 still holds a valid reference to the original object.
Why does p.name change when p1.name is modified?
Because p1 = p makes both variables point to the same object instance in memory. Changes to the object are visible through any reference.
Java Record Static Field Compilation Fix
Why can't I add a non-static field to a Java record?
Records are designed to be immutable data carriers. Non-static fields would break the implicit immutability and canonical constructor contract.
Can I use a class instead of a record here?
Yes, a standard class allows non-static instance fields, but the question specifically asks how to fix the record code.
Java Enum toString vs valueOf Output
Why does valueOf return CLOUDY but print SNOWY?
valueOf returns the enum constant object Forecast.CLOUDY. When printed, Java calls the object's toString() method, which is overridden to return 'SNOWY'.
Is ordinal always 0 for the first enum?
Yes, ordinal() returns the index of the enum constant in the declaration order. SUNNY is the first constant, so its ordinal is 0.
Java Garbage Collection Eligibility with Object References
Why isn't Option A correct for garbage collection?
Option A assumes all objects linked to t1 become unreachable. However, in this scenario, only one specific object loses its last reference, while others may remain reachable through different paths.
How does t1 relate to the GC eligibility?
t1 acts as a root or intermediate node. When t1's reference is lost or modified, it triggers the reachability check for the objects it directly pointed to, determining their survival.