41.2k views
1 vote
What will be printed as the output of the following program?

public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println("I = " +i);
}
}
(a) I = 0
(b) I = 1
(c) I = 2
(d) I = 3
(e) Compile-time Error.

1 Answer

5 votes

Final answer:

The output of the program will be I = 1.

Step-by-step explanation:

The output of the program will be I = 1.

  1. The expression i++ increments the value of i after it is used in the expression.
  2. So, i++ evaluates to 0 and increments i to 1.
  3. The expression i adds i to itself, which is 1 since it was incremented in the previous step.
  4. Therefore, the final value of i is 1.

User Asudhak
by
8.8k points

Related questions