Data Structures & AlgorithmsVery hardJava · GATE CS

A Java program computes d[0] = d[1] = 1 and d[i] = d[i−1] + d[i−2] using int (32-bit two's complement, wrapping on overflow) with no modulus. What value does d[48] hold?

  1. A. 7778742049
  2. B. 778742000
  3. C. 512559680
  4. D. -811192543

Correct answer: D. -811192543

The exact value of d[48] exceeds 2³¹ − 1, so each addition wraps modulo 2³² into the signed range. Java defines this wrap-around, which is why the stored value differs from the true integer; modding by 10⁹ + 7 after the fact is a different computation.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic