Skip to content
Sharp

Object-oriented programming · interview question

Consider this code snippet: What is the visibility of the 'price' field in the Product class?

Multiple choiceMediumchapter-3-curatedpremiumoopaccess-modifiers-and-visibility
java
class Product {
    double price;
}
Choose one answer
Answer and explanation

Correct answer

D. Package-private

Why

In Java, when no access modifier is specified, the default access level is package-private. Therefore, the 'price' field is accessible only within its package.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →