Multiple choice technology programming languages

Why are there no global variables in Java?

  1. Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables)

  2. State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.

  3. When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.

  4. All of above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

All three statements describe why Java avoids global variables: they break referential transparency, reduce cohesion, and limit reusability by tying code to a single instance. Since each individual reason is valid, the combined choice “All of above” correctly captures the rationale.