Question 13
Back to Exercise
What is the output of the following Java program?
class Money {
final long paise;
Money(long p) { paise = p; }
Money plus(Money o) { return new Money(paise + o.paise); }
@Override public String toString() { return "Rs " + paise / 100 + "." + String.format("%02d", paise % 100); }
}
public class Main {
public static void main(String[] args) {
Money a = new Money(1050), b = new Money(275);
Money c = a.plus(b);
System.out.println(a + " " + b + " " + c);
}
}
- A Rs 10.50 Rs 2.75 Rs 13.25 Correct
- B Rs 13.25 Rs 2.75 Rs 13.25
- C Rs 10.50 Rs 2.75 Rs 10.50
- D Rs 1050 Rs 275 Rs 1325
Correct Answer: Option A
plus does not change either operand; it returns a new Money – the immutable value-object style.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!