Learn AP Comp Sci

Problem of the Day

Tuesday, September 15, 2026


Problem:

Data types commonly used in Java programs include the int, double, and String types. Which of the following declaration/assignment statements would cause an error during compiling?

  1. int age = 17.0;
  2. double shoeSize = 14;
  3. String weight = "105";
  1. I only
  2. II only
  3. III only
  4. I and II
  5. I and III

Show solution:

The correct answer is a.

In statement I, trying to assign a double value to an int variable runs the risk of losing information that might be stored in the digits after the decimal point. In statement II, Java does allow an int value to be referenced by a double variable.

Statement III is legal also, because the 105 is enclosed in double-quotes making it a string value. Although this statement is syntactically correct, it is probably not a good way of storing that data. Numeric values should almost always be placed in int or double variables.