Consider following snippet:

var a = 1;
var с = a+++1;
What will be the value of c variable after executing the code?
Explanation
Expression a+++1 is equivalent to the following expression: (a++) + 1, post-increment is applied after sum expression, so the code is equivalent to

var a = 1;
var c = a+1 // c = 1 + 1 = 2
a++ // a = a + 1 = 2

Actually, it is a quite tricky question. In this case correct answer is 2. With ++i, the value is incremented, then evaluated. So if i was 1, it would be evaluated as 2. With i++, if i was 1, it would be evaluated as 1, but then stored as 2. These statements both seem to mean the same thing, well, they do, if the expressions are used in a standalone fashion. However, it’s when they’re used as part of a larger statement, do the differences shine through. Lets consider evaluation steps for two cases: 1) var c = a+++1; => (a++)+1 => (1) + 1 => 2 2) var c = ++a+1; => (++a)+1 => (2) + 1 => 3

2015 Jul 3, 3:22:38 PM

Correct answer is 3

2015 Jul 2, 8:46:11 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes