30.9k views
1 vote
Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t?

A. s is 19 and t is 38
B. s is 20 and t is 39
C. s is 19 and t is 39
D. s is 20 and t is 40
E. s is 20 and t cannot be determined

User Donkeydown
by
7.3k points

1 Answer

5 votes

Final answer:

The value of s is 20 and the value of t is 41.

Step-by-step explanation:

The code 'int s = 20; int t = s++ + --s;' is a combination of different operators (post-increment, pre-decrement, addition). The code assigns the initial value of 20 to the variable s. In the expression 's++ + --s', the post-increment operator is applied first, which means the value of s is used and then incremented by 1. So, the value of s becomes 21 for the addition operation. Next, the pre-decrement operator is applied, which means the value of s is decremented by 1 and then used. So, the value of s becomes 20 for the addition operation. Therefore, the value of t is 21 + 20, which is 41.

User Alokraop
by
7.7k points