Data Structures & AlgorithmsVery hardJava · GATE CS

A Java program stores dp[i] in an int array, sets dp[0] = dp[1] = 1, uses dp[i] = dp[i-1] + dp[i-2] for i up to 50, and never applies a modulus. What value does dp[50] hold, given that Java int wraps around in 32-bit two's complement?

  1. A. 20365011074
  2. B. -298632863
  3. C. -1109825406
  4. D. 365010934

Correct answer: C. -1109825406

The true value of dp[50] is far above 2³¹ − 1, so each addition wraps modulo 2³². Java defines this wrap-around (unlike C signed overflow), so the stored result is the true value reduced into the signed 32-bit range.

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

Exams that ask this topic