✏️ Explanatory Question

[Primitive Values, Wrapper Classes, Types and Casting]

Identify the most suitable data type (without losing precision and using memory efficiently) for:

(a) Population of a country
(b) Interest rate in a bank account

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

(a) Population → int / long (preferably long)
(b) Interest rate → float / double (preferably float)

Concept: Selecting Efficient Data Types

While choosing a data type, we consider:

  • Range of values
  • Precision required
  • Memory usage

(a) Population of a Country

Population is:

  • A whole number (no decimals)
  • Can be very large (millions or billions)
Data Type Range
int -2,147,483,648 to 2,147,483,647
long Very large range (up to 9 quintillion approx.)

Since population can exceed the range of int (e.g., India population), the best choice is:

long population;

(b) Interest Rate

Interest rate:

  • Contains decimal values (e.g., 7.25%)
  • Requires precision but not extremely high precision
Data Type Size Precision
float 4 bytes Moderate precision
double 8 bytes High precision

For efficient memory usage (without unnecessary extra precision), the best choice is:

float interestRate;

Best Practice Recommendation:

Data Recommended Type Reason
Population long Large integer values
Interest Rate float Decimal values with enough precision

Conclusion:

  • Population → large integer → long
  • Interest rate → decimal → float
  • Choose smallest sufficient type to optimize memory