Skip to content
Sharp

Object-oriented programming · interview question

Given the following code snippet, what will be the output when cartItem.calculateLineTotal() is called?

Multiple choiceMediumchapter-2-curatedpremiumoopinstance-methods
java
class CartItem {
    double unitPrice = 50.0;
    int quantity = 4;
    double calculateLineTotal() {
        return unitPrice * quantity;
    }
}
Choose one answer
Answer and explanation

Correct answer

A. 200.0

Why

The calculateLineTotal() method multiplies the unitPrice (50.0) by the quantity (4), resulting in a line total of 200.0.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →