nerdexam
Oracle

1Z0-803 · Question #166

Given: public class Test { public static void main (String[] args) { int i = 25; int j = i++ + 1; if (j % 5 == 0) { System.out.println(j + " is divisible by 5"); } else { System.out.println(j + " is n

The correct answer is C. 26 is not divisible by 5. This question tests understanding of Java's postfix increment operator (i++) and basic arithmetic operations within an if-else control structure.

Using Operators and Decision Constructs

Question

Given:

public class Test { public static void main (String[] args) { int i = 25; int j = i++ + 1; if (j % 5 == 0) { System.out.println(j + " is divisible by 5"); } else { System.out.println(j + " is not divisible by 5"); } System.out.println("Done"); } } What is the result?

Options

  • ACompilation fails.
  • B25 is divisible by 5
  • C26 is not divisible by 5
  • D27 is not divisible by 5

How the community answered

(16 responses)
  • A
    6% (1)
  • B
    19% (3)
  • C
    69% (11)
  • D
    6% (1)

Why each option

This question tests understanding of Java's postfix increment operator (`i++`) and basic arithmetic operations within an if-else control structure.

ACompilation fails.

The code contains no syntax errors and will compile successfully.

B25 is divisible by 5

The value of `j` is 26 due to the postfix increment, not 25, so '25 is divisible by 5' is incorrect.

C26 is not divisible by 5Correct

The postfix increment operator (`i++`) first uses the current value of `i` (25) in the expression `i++ + 1`, making `j` equal to `25 + 1 = 26`. Then, `j % 5` (26 % 5) equals 1, so the `else` block executes, printing '26 is not divisible by 5'.

D27 is not divisible by 5

The value of `j` is 26, not 27, and 27 % 5 is 2, so '27 is not divisible by 5' would be incorrect based on the calculation.

Concept tested: Java postfix increment operator behavior

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Topics

#operators#post-increment#if-else#arithmetic operations

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice