129k views
0 votes
Consider the following method. public double calculate(double x) { return x + 1.5; } The following code segment calls the method calculate in the same class. Double d1 = new Double(7.5); System.out.println(calculate(d1)); What, if anything, is printed when the code segment is executed?A. 8.0

B.8.5
C. 9
D. 9.0
E. Nothing is printed because the code does not compile. The actual parameter d1 passed to calculate is a Double, but the formal parameter x is a double.

User Edenhill
by
5.8k points

2 Answers

3 votes

Final answer:

The code segment will print 9.0 when executed.

Step-by-step explanation:

The method calculate takes a double parameter x and returns x + 1.5. In the code segment, d1 is a Double object with the value 7.5. When d1 is passed as an argument to the method calculate, it will be automatically unboxed to a primitive double before the method is called.

Therefore, the method calculate will perform the calculation 7.5 + 1.5 which results in 9.0. The result is then printed using the System.out.println method, so the output will be 9.0.

User Utkarsh Pandey
by
5.7k points
3 votes

Answer:

E (Nothing is printed because the code will not compile)

Step-by-step explanation:

There is a difference between Double which is a wrapper class and double which is data type. Changing the line Double d1 = new Double(7.5) to Double d1 = new (7.5) or simple declaring double d1 = 7.5 would have giving a printout of 9.0.

User Manuel Miranda
by
6.4k points