Which of the following are good examples of things that should be stored as global variables?

Study for the CodeHS Animation and Games Test. Enhance your coding skills with flashcards and multiple-choice questions, featuring hints and explanations. Get ready for your exam!

Storing a ball for a game as a global variable is appropriate because it is an object that is likely needed across multiple functions within the program. In game development, objects like game elements (e.g., a ball, a player character, or an enemy) often require access in various parts of the code—such as for updating position, checking for collisions, or rendering on the screen. By making the ball a global variable, all functions that need to interact with it can easily access and manipulate it without needing to pass it back and forth, leading to cleaner and more manageable code.

In contrast, the other examples illustrate situations that are less suited for global variables. A counter tracking mouse clicks, while it might be used in multiple places, typically relates to user input events and could be better managed in a more localized scope or as part of an object to encapsulate functionality. A for loop counter variable is usually confined to the loop's context and does not need to exist outside of it. Lastly, a rectangle's color that is only used in one function does not require global scope, as it is pertained to localized logic within that specific function. Keeping such variables local helps maintain code clarity and reduces the risk of unintended side effects elsewhere in the program

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy