1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In the expression a[i++] += 10, how many times is the…

Added 30 Nov -0001

In the expression a[i++] += 10, how many times is the subscript expression i++ evaluated?

  1. Twice, because it expands to a[i++] = a[i++] + 10 in the abstract machine here
  2. Zero times until the addition completes, since the store is sequenced last
  3. Undefined, because the operand is modified and read in the same expression
  4. Once, because a compound assignment evaluates its left operand a single time Correct answer

Correct answer

D. Once, because a compound assignment evaluates its left operand a single time

20 views

MCQSEXAM

Explanation

A compound assignment is defined as E1 = E1 op E2 except that E1 is evaluated only once, so a[i++] += 10 increments i exactly one time. Writing it out as a[i++] = a[i++] + 10 would double-increment and, more seriously, be undefined for want of sequencing. The guarantee is about evaluation count, not atomicity: the read-modify-write is still unsafe across threads.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs