Java Code Output Practice for 1Z0-808, 1Z0-819 and 1Z0-829

Read a short Java snippet and predict the result: what it prints, the exception that stops it, or that it does not compile. Tap or type your answer: a wrong one names the slip, then the working shows each step.

What is the result?

The lines run inside main of class Main. Every answer is the real result from javac and java, OpenJDK 21.0.4.

Answer to see the working.

Practise the exams

Going for an Oracle Java certification? Start with the free real exam questions, then get the full pack.

1Z0-808234 real exam questions · US$39
Free: 17 questionsGet the full pack
1Z0-819213 real exam questions · US$39
Free: 27 questionsGet the full pack
1Z0-829130 real exam questions · US$39
Free: 11 questionsGet the full pack

Java output FAQ

Which Java version made the answers?

Every snippet was compiled by javac and run by java from OpenJDK 21.0.4. Each one was compiled four times, with --release 8, --release 11, --release 17 and --release 21, and only snippets that gave the same result every time were kept. None of them uses syntax newer than Java 8.

What surrounds each snippet?

The lines are the body of the main method of a class named Main. When a snippet has the comment // main() of class Main, the lines above it are members of that class (fields, methods, initializers) and the lines below it are the body of main.

Why is -7 / 2 equal to -3 in Java?

Integer division rounds toward 0, so -3.5 becomes -3. % gives a - (a / b) * b, so its result has the sign of the left operand: -7 % 2 is -1. An integer / 0 or % 0 throws ArithmeticException. With a double operand, / 0 never throws: it gives Infinity, -Infinity or NaN.

Why does byte + byte not fit in a byte?

In arithmetic, byte, short and char operands are promoted to int, so a + b is an int, and an int needs a cast to go into a byte. A narrowing cast keeps only the low bits: (byte) 200 is -56. A constant that fits, such as final int k = 10, may be assigned to a byte without a cast.

Why does b += 1 compile but b = b + 1 not?

E1 op= E2 means E1 = (T) ((E1) op (E2)), where T is the type of E1: the cast is built in. The specification gives the example short x = 3; x += 4.6;, which leaves x at 7.

Why does 'a' + 1 print 98?

char is an integral type, so 'a' + 1 is int addition: 'a' is 97. Cast the result with (char) to get b back. + joins text only when one of its two operands is a String.

How do i++ and ++i differ?

Both add 1 to i. The value of i++ is the value from before the change; the value of ++i is the new value. Operands are evaluated left to right, so in i++ + ++i the right operand already sees the changed i.

Why does 1 + 2 + "a" give 3a?

+ groups from left to right. 1 + 2 has two int operands, so it is addition, and then 3 + "a" joins text: 3a. In "a" + 1 + 2 the first + already has a String operand, so both join text: a12. A null reference joins as the text null.

When is == true for two strings?

== on two references is true only when both refer to the same object. Equal string literals are the same object, and so is a string that javac can work out from constants, such as "ja" + "va". A string joined at run time, or made with new String(), is a new object. Compare the characters with equals().

Is == safe for two Integer objects?

Boxing a constant from -128 to 127 always gives the same object, so == is true there. Outside that range the specification makes no promise, so this drill has no such snippet: use equals(). Comparing an Integer with an int unboxes it and compares the numbers, and unboxing null throws NullPointerException.

What is switch fall through?

Execution starts at the case that matches, or at default if none does, and runs every statement after it, across the later labels, until a break or the end of the block. default may stand anywhere in the block.

What do && and || skip?

&& evaluates its right side only if the left side is true, and || only if the left side is false. A skipped side has no effect, so a ++ inside it never happens. & and | always evaluate both sides.

In what order do initializers run?

The class is initialized before its main runs: static field initializers and static blocks run once, in the order written. Each new then runs the instance field initializers and instance blocks in the order written, and after them the rest of the constructor body.

How does Java pick an overloaded method?

In three phases. Phase 1 allows no boxing and no varargs, phase 2 allows boxing and unboxing, phase 3 allows varargs. The first phase in which a method applies decides, and if several apply the most specific one is chosen: for an int, f(long) beats f(double). If no method is the most specific, the call is ambiguous and does not compile.

Does finally run after return?

Yes. return x works out its value first, then every enclosing finally block runs before control leaves the method. Changing x in finally does not change the value already worked out, but a return inside finally replaces it.

How were the answers made?

This is the tool's own method. Each snippet comes from a fixed generator that also states the result its rule predicts. javac and java from OpenJDK 21.0.4 then compile and run it, and the stored answer is the real printed text, the name of the uncaught exception, or the fact that javac reported an error. A snippet whose real result differs from the prediction is dropped; none did. Each wrong option is the result one named slip leads to. The page runs no Java itself: it shows the stored results.

Sources

Checked 20 November 2026 against The Java Language Specification, Java SE 21 Edition, on docs.oracle.com.

More free tools

All free tools