78.6k views
4 votes
Class Product {

double price;
}
public class Test {
public void updatePrice(Product product, double price) {
price = price *2;
product.price=product.price + price;
}
public static void main ( String args[]) {
Product prt = new Product();
prt.price=200;
doucble newPrice = 100;
Test t = new Test ();
t.updatePrice ( prt, newPrice);
System.out.println(prt.price + " : " + newPrice);
}
}

What is the result?

A) 200.0: 100.0
B) 400.0: 200.0
C) 400.0: 100.0
D) Compilation fails.

2 Answers

5 votes

Answer:

C) 400.0: 100.0

Step-by-step explanation:

prt.price = 200 and updatePrice method is called which makes it 400 & new price is as declared 100.

Both the numbers are double hence

400.0 : 100.0 is printed.

User Olajumoke
by
4.4k points
5 votes

Answer:

D. Compilation Fails

Step-by-step explanation:

There is an error in the program, which if not correct will hinder the successful compilation of the program and will give a compilation error

The error is at doucble newPrice = 100;

The data type has to be spelt correctly to ensure the program is executed successfully

To declare variable newPrice of type double and also initialize it to 100, the right syntax is double newPrice = 100;

After correcting the error, the program will run correctly and produce the following result:

400.0 : 100.0

At line 2 of the main method, an instance of class Product is declared "Product prt = new Product();"

This allows values to be passed between the main method and the class

In class Test

price takes its value from newPrice (in the main method)

So, when newPrice is 100.0

price = 100.0 * 2

price = 200.0

After which product.price is calculated

product.price takes its value from prt.price (in the main method)

So, when prt.price is 200.0

product.price=product.price + price (Recall that price is 200.0; as illustrated above)

product.price = 200.0 + 200.0

product.price = 400.0

At line 5 of the main method, an instance of class Test is declared "Test t = new Test ();"

The next line t.updatePrice ( prt, newPrice); assigns the calculated values to prt and newPrice respectively

prt = 400.0

newPrice = 100.0

The output is done on the next line: System.out.println(prt.price + " : " + newPrice);

prt.Price = prt = 400.0 and newPrice = 100.0

Hence, the output will be done in the following order 400.0 : 100.0

User Guruz
by
3.9k points