Multiple choice

Consider the following program fragment. main() { int a,b,c; b=2 ; a= 2*(b++); c = 2*(++b); } Which one of the following is correct?

  1. a = 4, c = 6

  2. a = 3, c = 8

  3. b = 3, c = 6

  4. a = 4, c = 8

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

b=2. a = 2 * (b++) uses the current value of b (2) then increments b to 3. So a = 4. Then c = 2 * (++b) increments b to 4 then uses it, so c = 2 * 4 = 8.