SCRIPTING-AND-PROGRAMMING-FOUNDATIONS practice question 2 of 25

A program calculates the average miles per gallon given miles traveled and gas consumed How should the item that holds me miles per gallon be declared?

Full set: 137 questions
Answer options
A: Constant float milesTraveled
B: Variable float milesPerGallon Correct answer
C: Variable float milesTraveled
D: Constant float milesPerGallon
Correct Answer:
B. Variable float milesPerGallon
Explanation

The value for miles per gallon is calculated by the program based on user input (miles traveled and gas consumed). Since this value is computed during program execution and can change with different inputs, it must be stored in a variable. A constant is used for values that do not change. The result of division is often a decimal number, making float the appropriate data type to handle fractional values accurately. The name milesPerGallon clearly describes the data being stored.

Why Incorrect

A. The item should be a variable, not a constant, and milesTraveled is an input, not the calculated result.
C. The name milesTraveled is incorrect; the variable should hold the calculated miles per gallon.
D. The item must be a variable because its value is calculated and will change, not a constant.

References

1. Severance, C. R. (2016). Python for Everybody: Exploring Data Using Python 3. Chapter 2, "Variables, expressions, and statements," explains the concept of variables for storing values that can change.

2. MIT OpenCourseWare. (2016). 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Lecture 1, "What is Computation?", introduces the distinction between fixed constants and variables that store results of computations.

3. The C++ Core Guidelines. (n.d.). Con: Constants and Immutability. Retrieved from https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-const (This resource, while for C++, explains the universal programming principle of using constants for unchanging values and variables for calculated results.)

Scroll to Top