1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In C99 and later, the expression -7 % 3 is evaluated. What…

Added 30 Nov -0001

In C99 and later, the expression -7 % 3 is evaluated. What value results, and which rule fixes it?

  1. 2, because the remainder operator always normalises the result to a non-negative value
  2. -2, because the quotient is floored downward, which shifts the remainder in the negative direction
  3. 1, because the sign of a remainder is taken from the divisor rather than from the dividend
  4. -1, because division truncates toward zero, so the remainder keeps the dividend's sign Correct answer

Correct answer

D. -1, because division truncates toward zero, so the remainder keeps the dividend's sign

15 views

MCQSEXAM

Explanation

Truncating division in C99 makes -7/3 equal -2, and the identity (a/b)*b + a%b == a then forces -7%3 to be -1, so the remainder carries the dividend's sign. Python differs: its // floors, so -7%3 yields 2. The exam trap is assuming a remainder is always non-negative when a negative operand appears.

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