nerdexam
Oracle

1Z0-819 · Question #12

Given this formula to calculate a monthly mortgage payment: M = P * (i(1+i)^n) / ((1+i)^n - 1) and these declarations: double m; double i = 0.05/12; //monthly interest rate int p = 100_000…

The correct answer is A. m = p * (Math.pow(1 + r, n) / (Math.pow(1 + r, n) - 1)). There is an error in this question's answer key. Option A is actually incorrect - it is missing the r (interest rate) multiplication in the numerator, so it computes P (1+i)^n / ((1+i)^n - 1) instead of the required formula. The correct code should be B or D, both of which…

Working with Java Data Types

Question

Given this formula to calculate a monthly mortgage payment: M = P * (i(1+i)^n) / ((1+i)^n - 1) and these declarations: double m; double i = 0.05/12; //monthly interest rate int p = 100_000; //principal int n = 180; //number of payments How can you code this formula?

Options

  • Am = p * (Math.pow(1 + r, n) / (Math.pow(1 + r, n) - 1));
  • Bm = p * (r * Math.pow(1 + r, n) / (Math.pow(1 + r, n) - 1));
  • Cm = p * (r * Math.pow(1 + r, n) / Math.pow(1 + r, n) - 1);
  • Dm = p * (r * Math.pow(1 + r, n) / (Math.pow(1 + r, n) - 1));

How the community answered

(37 responses)
  • A
    78% (29)
  • B
    3% (1)
  • C
    14% (5)
  • D
    5% (2)

Explanation

There is an error in this question's answer key. Option A is actually incorrect - it is missing the r * (interest rate) multiplication in the numerator, so it computes P * (1+i)^n / ((1+i)^n - 1) instead of the required formula. The correct code should be B or D, both of which faithfully translate M = P * [i(1+i)^n] / [(1+i)^n - 1].

Here is the breakdown of each choice:

  • A - Wrong. Missing r * in the numerator entirely, so the interest rate i is dropped from the calculation.
  • B - Correct implementation. Has r * Math.pow(1 + r, n) in the numerator and (Math.pow(1 + r, n) - 1) properly wrapped in parentheses for the denominator.
  • C - Wrong parentheses around the denominator. Due to operator precedence, / binds before -, so it computes (r * (1+r)^n / (1+r)^n) - 1, which simplifies incorrectly to r - 1.
  • D - Appears identical to B and is also a correct implementation (the question may have a typo distinguishing B from D).

Memory tip: The denominator (1+i)^n - 1 must be wrapped in its own parentheses - the entire expression subtracts 1 from the power, so without those parens Java's division operator will steal the subtraction first and break the formula.

Topics

#Math.pow()#type promotion#formula implementation#operator precedence

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice