Skip to content
Sharp

Core Java · interview question

In a method calculating the shipping cost, where should you declare a variable to ensure it is not accessible outside the method?

Multiple choiceEasychapter-1-curatedpremiumcore-javascope-and-reassignment
Choose one answer
Answer and explanation

Correct answer

D. Inside the method

Why

Declaring a variable inside a method means it is local to that method, ensuring it is not accessible outside of it, thereby maintaining encapsulation.

Test yourself on Core Java

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

Start free test
All Core Java questions →