ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
// 32-bit signed integers with two's-complement wrap-around
Integer x = 2147483647
Print x + 1, x * 2
  1. A. 2147483648 4294967294
  2. B. -2147483648 4294967294
  3. C. 0 -2
  4. D. -2147483648 -2

Correct answer: D. -2147483648 -2

2147483647 is INT_MAX = 0111…1. Adding 1 wraps to 1000…0 = −2³¹. Doubling shifts left: 1111…10 = −2. (In real C, signed overflow is undefined behaviour; the question fixes wrap-around semantics, as Java does.)

More Pseudocode and Code Tracing questions

Exams that ask this topic