Practice mode C#

What is the output of: int x = 5; Console.WriteLine(x++ + ++x);?

Correct answer D. 12

Explanation

C# evaluates operands strictly left to right. x++ yields the old value 5 and then sets x to 6. Next, ++x increments x to 7 and yields 7. The sum is 5 + 7 = 12. Unlike C and C++, where modifying a variable twice in one expression is undefined, C# defines this order exactly, so the result is always 12.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.