What does the following program output to the monitor: int value = 0; int count = 1; value = ++count; /* note change from previous */ System.out.println("value:" + value "+ count:" + count );
Advertisements
Question
What does the following program output to the monitor:
int value = 0;
int count = 1;
value = ++count; /* note change from previous */
System.out.println("value:" + value "+ count:" + count );Long Answer
Advertisements
Solution
Initially, the variable value is assigned 0, and the variable count is assigned 1.
The statement value = count++; uses the post-increment operator (++). This operator first assigns the original value of count to value and then increases the value of count by 1.
Therefore, after executing the statement:
- value = 1
- count = 2
However, the given program contains a syntax error in the System.out.println() statement because the concatenation operator (+) is missing between value and "+ count:".
System.out.println("value:" + value + " count:" + count);
Output:
value:1 count:2
shaalaa.com
Is there an error in this question or solution?
