Language basics and types: Java 25 practice questions

Language basics and types is 12% of the free Java 25 mock (6 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.I · Java 25 practice question

A developer combines a long and a float in an arithmetic expression. What does this Java 25 program print?

void main() {
    long count = 10L;
    float factor = 2.5f;
    var result = count + factor;
    System.out.println(((Object) result).getClass().getSimpleName());
}
Choose one answer
Check answer and explanation

Correct answer: Float

Explanation

In a numeric context with a float and a long, the promoted type is float and the long undergoes widening primitive conversion to float, which boxes to Float.

Common trap

Even though long uses 64 bits and float uses 32 bits, numeric promotion still promotes to float rather than long. Promotion only chooses double when at least one operand is already of type double.

Source: JLS SE 25 §5.6. Numeric Contexts
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

What does this Java 25 program print when adding an int and a long?

void main() {
    int base = 5;
    long offset = 7L;
    var total = base + offset;
    System.out.println(((Object) total).getClass().getSimpleName());
}
Choose one answer
Check answer and explanation

Correct answer: Long

Explanation

With no double or float operand present, having a long operand makes the promoted type long, widening the int to long and boxing to Long.

Common trap

The int operand is widened to long rather than narrowing the long to int. Integer arithmetic never promotes automatically to floating-point types like float.

Source: JLS SE 25 §5.6. Numeric Contexts
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

Given this Java 25 code using a compound assignment on an array element, what does it print?

void main() {
    int[] data = {10};
    data[0] += (data[0] = 4);
    System.out.println(data[0]);
}
Choose one answer
Check answer and explanation

Correct answer: 14

Explanation

The current value of data[0] (10) is saved before the right-hand operand is evaluated, so 10 is added to the result 4, storing and printing 14.

Common trap

Assuming the right-hand assignment updates data[0] to 4 before the left-hand value is read leads to 4 + 4 = 8, which contradicts the specification. Thinking the inner assignment overwrites the final result ignores that the outer compound assignment still stores the sum back into data[0].

Source: JLS SE 25 §15.26.2. Compound Assignment Operators
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

How many specific conversions on primitive types are defined as narrowing primitive conversions in Java 25?

Choose one answer
Check answer and explanation

Correct answer: 22

Explanation

The Java Language Specification defines exactly 22 specific conversions on primitive types as narrowing primitive conversions.

Common trap

19 is the count of widening primitive conversions, not narrowing primitive conversions. 28 overcounts by including conversions that are not classified as narrowing primitive conversions.

Source: JLS SE 25 §5.1.3. Narrowing Primitive Conversion
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

In a numeric context where none of the expressions have type double, float, or long, what determines how the promoted type is chosen?

Choose one answer
Check answer and explanation

Correct answer: The kind of numeric context

Explanation

When none of the expressions are double, float, or long, the kind of context itself determines how the promoted type is chosen.

Common trap

Numeric promotion is a compile-time rule and never depends on runtime operand values. Operand order does not dictate the promoted type in Java numeric contexts.

Source: JLS SE 25 §5.6. Numeric Contexts
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

A developer refactors (a * b) * c into a * (b * c) where a, b, and c are double variables. Which statement about floating-point multiplication in Java is accurate?

Choose one answer
Check answer and explanation

Correct answer: Floating-point multiplication is not associative

Explanation

Floating-point multiplication is not associative in Java, so regrouping (a * b) * c as a * (b * c) can produce different results.

Common trap

Having the same type guarantees associativity for integer multiplication, not for floating-point multiplication. Even positive finite floating-point numbers can round differently depending on grouping.

Source: JLS SE 25 §15.17.1. Multiplication Operator *
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

A developer runs the following Java 25 program to update an element in a scores array. What happens at run time?

void main() {
    int[] scores = {10, 20};
    int idx = 2;
    scores[idx] += 5;
    System.out.println(scores[0]);
}
Choose one answer
Check answer and explanation

Correct answer: ArrayIndexOutOfBoundsException

Explanation

Because idx equals the array length of 2, no assignment takes place and ArrayIndexOutOfBoundsException is thrown.

Common trap

Java arrays have fixed lengths and never resize automatically on out-of-bounds compound assignment. Out-of-bounds array access does not fail silently; it throws an exception immediately.

Source: JLS SE 25 §15.26.2. Compound Assignment Operators
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.I · Java 25 practice question

Under what condition does the Java Language Specification consider the binary multiplication operator * to be commutative?

Choose one answer
Check answer and explanation

Correct answer: When the operand expressions have no side effects

Explanation

Multiplication with * is commutative only if the operand expressions have no side effects; otherwise, left-to-right evaluation order can change behavior.

Common trap

Side effects in operand expressions can break commutativity because Java evaluates operands left to right. Commutativity without side effects applies to multiplication generally, not exclusively to integer types.

Source: JLS SE 25 §15.17.1. Multiplication Operator *
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics