29.0k views
5 votes
Consider this code: "int v = 20; --v; System.out.println(v++);". What value is printed, what value is v left with?

A. 20 is printed, v ends up with 19
B. 19 is printed, v ends up with 20
C. 20 is printed, v ends up with 20
D.19 is printed, v ends up with 19
E. cannot determine what is printed, v ends up with 20

1 Answer

5 votes

Final answer:

The code "int v = 20; --v; System.out.println(v++);" prints 19 and leaves v with a value of 20 after execution.

Step-by-step explanation:

When evaluating the given code "int v = 20; --v; System.out.println(v++);", we can derive the value that gets printed and the final value of v after the operations are executed. The prefix decrement operator (--v) will first decrease the value of v by 1, making it 19. Then, the System.out.println function is called, which prints the current value of v, which is 19. However, directly after printing, the postfix increment operator (v++) is used, which increases the value of v by 1, but only after the current value has been used. Therefore, 19 is printed but v is then left with the value 20 after the increment.

User Eikonomega
by
8.1k points