Question 1
Back to Exercise
What is the output of the following Java program?
class Product {
private double price = 100;
public double getPrice() { return price; }
public void setPrice(double p) { if (p > 0) price = p; }
}
public class Main {
public static void main(String[] args) {
Product p = new Product();
p.setPrice(-20);
p.setPrice(0);
double a = p.getPrice();
p.setPrice(249.5);
System.out.println(a + " " + p.getPrice());
}
}
- A 100.0 249.5 Correct
- B -20.0 249.5
- C 0.0 249.5
- D 100.0 100.0
Correct Answer: Option A
The setter accepts only positive prices.
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!