Variables and Data Types
Help Questions
AP Computer Science A › Variables and Data Types
Based on the student grade manager below, what is the main advantage of using a double instead of an int for average?
int quiz1 = 85;
int quiz2 = 86;
Gradebook gb = new Gradebook();
double average = gb.computeAverage(quiz1, quiz2);
System.out.println(average);
int is better because it automatically rounds to nearest tenth.
double stores fractional averages instead of truncating decimals.
double changes scope so average is visible in other classes.
double requires no methods, so computeAverage can be removed.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the student grade manager, computing averages often results in fractional values that need decimal precision. Choice A is correct because double preserves decimal portions of averages (like 85.5), while int would truncate to 85, losing important grade information. Choice C is incorrect because int doesn't round - it truncates by removing all decimal portions. To help students: Calculate averages by hand to show decimal results. Demonstrate the difference between truncation and rounding. Watch for: Confusing truncation with rounding, thinking data types affect variable scope.
Based on the library tracker below, which data type is best suited for bookCount and why?
int bookCount = 4;
Library lib = new Library(bookCount);
lib.borrowOne();
System.out.println(lib.getBookCount());
String, because counts are stored as characters.
double, because counts require decimal precision.
int, because inventory is a whole-number quantity.
boolean, because inventory is true or false.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the library tracker scenario, bookCount represents a quantity of books, which is naturally a whole number. Choice B is correct because int is ideal for counting discrete items like books - you can't have 3.5 books in inventory. Choice C is incorrect because book counts don't require decimal precision; fractional books don't make logical sense. To help students: Identify real-world quantities that are inherently whole numbers versus those that can be fractional. Practice choosing appropriate data types based on the nature of the data. Watch for: Students defaulting to double for all numeric values without considering the context.
Based on the student grade manager below, what is the main advantage of using a double instead of an int for average?
GradeBook gb = new GradeBook("Kai");
gb.addScore(89);
gb.addScore(90);
double average = gb.getAverage();
System.out.println(average);
It changes average scope to the GradeBook class.
It makes double use less memory than int.
It guarantees average is always an integer value.
It supports fractional averages like 89.5.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the student grade manager scenario, using double for average allows representation of fractional values like 89.5. Choice B is correct because grade averages often result in decimal values (like averaging 89 and 90 to get 89.5), which double can represent accurately. Choice A is incorrect because using double doesn't guarantee integer values; it allows fractional values. To help students: Calculate averages by hand to show when decimals occur. Compare results using int versus double for average calculations. Watch for: Students not recognizing when calculations naturally produce fractional results.
Based on the student grade manager below, which data type is best suited for studentName and why?
String studentName = "Ava";
GradeBook gb = new GradeBook(studentName);
gb.addScore(90);
gb.addScore(85);
System.out.println(gb.getAverage());
String, because it stores sequences of characters.
boolean, because names are true or false values.
double, because names need decimal precision.
int, because names are stored as whole numbers.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the student grade manager scenario, studentName needs to store text data like "Ava", which requires a String data type. Choice C is correct because String is designed to store sequences of characters, making it the only appropriate choice for storing names. Choice A is incorrect because int can only store whole numbers, not text characters. To help students: Create examples showing different data types and what values they can hold. Practice identifying the appropriate data type based on the kind of data being stored. Watch for: Students thinking any data type can store any value, or confusing String with primitive types.
Based on the shopping cart program below, how does changing taxRate from double to int affect the output?
double subtotal = 40.00;
double taxRate = 0.0825;
double total = Cart.addTax(subtotal, taxRate);
System.out.println(total);
It makes 0.0825 become 0, removing tax.
It lets int store 0.0825 without any casting.
It makes double use less memory than int.
It forces taxRate to remain in method scope.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the shopping cart scenario, changing taxRate from double to int would truncate the decimal value 0.0825 to 0. Choice B is correct because int cannot store decimal values, so 0.0825 becomes 0, effectively removing all tax from the calculation. Choice C is incorrect because int cannot store decimal values at all, even with casting. To help students: Demonstrate data loss when assigning decimal literals to int variables. Show how this affects calculations in real scenarios like tax computation. Watch for: Students thinking int can somehow preserve decimal values, or not recognizing immediate truncation.
Based on this temperature tool:
double celsius = 20.0;
double fahrenheit = toFahrenheit(celsius);
String label = "F";
System.out.println(fahrenheit + label);
Which data type is best suited for label and why?
String, because unit symbols are text.
boolean, because units are true/false.
int, because units are numeric constants.
double, because labels may include decimals.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the temperature tool, the label variable stores the unit symbol "F" which is text, making String the appropriate data type. Choice C is correct because String is designed to store text data like unit symbols, letters, or any character sequences. Choice A is incorrect because int can only store whole numbers, not text characters like "F". To help students: Emphasize matching data types to the actual content being stored - text requires String, numbers require numeric types. Practice identifying appropriate data types based on the literal values in code. Watch for: Students trying to store text in numeric data types or misunderstanding that unit labels are textual, not numeric.
Based on the temperature conversion tool below, how does changing fahrenheit from double to int affect the printed output?
double celsius = 37.0;
Converter conv = new Converter();
double fahrenheit = conv.toFahrenheit(celsius);
System.out.println(fahrenheit);
It lets int store decimals, so output is unchanged.
It makes the value out of scope outside toFahrenheit.
It truncates the decimal portion, reducing numeric precision.
It causes a syntax error because int cannot be printed.
Explanation
This question tests AP Computer Science A skills, focusing on variables and data types. Variables in Java hold data values, where data types determine the kind of data a variable can store, such as int for integers and double for decimals. In the temperature conversion tool, changing fahrenheit from double to int affects how the converted value is stored and displayed. Choice B is correct because storing a double value in an int variable truncates the decimal portion, so 98.6°F becomes 98°F, losing precision. Choice A is incorrect because int values can be printed without syntax errors - Java automatically converts them to strings for output. To help students: Show examples of implicit type conversion and data loss. Practice predicting output when changing variable types. Watch for: Thinking type changes cause syntax errors, not understanding truncation vs rounding.