Skip to content
Sharp

Object-oriented programming · interview question

Given the following code, what is the output of calling product.checkStock()? Assume stock is 10?

Multiple choiceHardchapter-2-curatedpremiumoopinstance-methods
java
class Product {
    private int stock;
    public Product(int stock) { this.stock = stock; }
    public String checkStock() {
        if (stock > 0) return "In stock";
        else return "Out of stock";
    }
}
Choose one answer
Answer and explanation

Correct answer

C. "In stock"

Why

The method checkStock() checks if the product's stock is greater than zero. If it is, it returns "In stock". Since the stock is 10, which is greater than zero, "In stock" is returned.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →