Skip to content
Sharp

Object-oriented programming · interview question

In Java, what is the effect of marking a class field as 'private'?

Multiple choiceEasychapter-3-curatedpremiumoopaccess-modifiers-and-visibility
Choose one answer
Answer and explanation

Correct answer

A. It restricts direct access to the field from outside the class.

Why

Marking a field as 'private' restricts its visibility to within the same class. This is a fundamental encapsulation technique to protect the field from being accessed or modified directly from outside the class.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →