Variables and Data Types - AP Computer Science A

  1. A program needs to store the average score of a test, which could be a value like 92.5. Which variable declaration is most appropriate for this purpose?
    Answer: double averageScore; The double data type is used for floating-point (decimal) numbers, making it the most appropriate choice for storing an average that may have a fractional part. An int would truncate the decimal, a boolean can only store true or false, and a String stores text.
  2. Which of the following is NOT one of the three primitive data types specified for the AP Computer Science A course?
    Answer: float The AP Computer Science A curriculum focuses specifically on three primitive types: int, double, and boolean. While float is a primitive type in Java for decimal numbers, it is explicitly excluded from the scope of the course and exam.
  3. Which code segment shows a variable declaration but NOT an initialization?
    Answer: int score; A declaration introduces a variable's name and type (int score;). An initialization gives it its first value (= 100). Option (B) declares the variable score but does not assign it an initial value.
  4. Which statement correctly declares a double variable named TAXRATETAX_RATE and initializes it to 0.065 in a single line of code?
    Answer: double TAX_RATE = 0.065; The proper syntax to declare and initialize a variable in one statement is type name = value;. Option (C) correctly follows this syntax for a double variable. Option (D) incorrectly attempts to assign a String literal.
  5. A program for a library needs to track various pieces of information about its books. Which of the following quantities is best represented by an int?
    Answer: The year the book was published. The publication year is a whole number and is best represented by an int. The ISBN is best stored as a String due to hyphens. Checkout status is a boolean. Replacement cost would be a double to handle cents.
  6. 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);
    Answer: It makes 0.0825 become 0, removing tax. 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.
  7. 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());
    Answer: int, because inventory is a whole-number quantity. 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.
  8. 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);
    Answer: double stores fractional averages instead of truncating decimals. 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.
  9. 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?
    Answer: String, because unit symbols are text. 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.
  10. A variable of type boolean is capable of storing which of the following sets of values?
    Answer: The keywords true and false The boolean data type in Java has exactly two possible literal values: the keywords true and false. Other values, such as integers or strings, are not directly assignable to a boolean variable.
  11. A program requires a variable to store whether a file has been successfully saved. Which declaration is most suitable for this task?
    Answer: boolean fileSaved; A boolean variable is the most appropriate choice for storing a logical true/false value, which perfectly represents a binary status like whether a file has been saved.
  12. Which of the following correctly declares an integer variable named studentCount?
    Answer: int studentCount; The correct syntax for a variable declaration in Java is the data type (int), followed by the variable name (studentCount), and terminated with a semicolon. Option (A) follows this syntax correctly.
  13. 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);
    Answer: It truncates the decimal portion, reducing numeric precision. 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.
  14. A programmer is writing software for a weather station and needs to store the temperature, which could be -5.7 degrees Celsius. Which data type is the most suitable choice?
    Answer: double The double data type is designed to store floating-point numbers, which include negative values and values with decimal points, making it the ideal choice for a precise temperature reading.
  15. Which of the following best describes the concept of a variable in programming?
    Answer: A named storage location in memory that holds a value of a specific data type. A variable is fundamentally a named location in the computer's memory that a programmer can use to store and retrieve a value. Every variable has a name, a data type, and a value that can change during program execution.
  16. Consider the declaration double gpa;. What type of information can be stored in the gpa variable?
    Answer: A real number, which may have a fractional part, such as 3.75. The double data type is used to store floating-point values, which are numbers that can have a fractional component. It is the appropriate choice for values like a Grade Point Average (GPA).
  17. What is stored inside a variable that has been declared with a primitive data type?
    Answer: A literal value that corresponds to that primitive data type. A variable of a primitive type, such as int myAge = 42;, directly holds the literal value (42) of that type within its allocated memory space. Reference type variables hold a memory address.
  18. A variable is needed to store the number of pages in a book. This value will always be a whole number. Which data type is the most appropriate?
    Answer: int int is the standard data type for storing integer (whole number) values. Using a double would be inefficient as a fractional part is not needed. boolean and String are incorrect types for a numerical count.
  19. Which of the following is an invalid variable name in Java?
    Answer: final The word final is a reserved keyword in Java and cannot be used as a variable name. The other options are all valid identifiers.
  20. Which of the following statements about Java variables is true?
    Answer: A variable must be declared with a data type before it can be assigned a value. Java is a statically-typed language, which requires that every variable be declared with a specific data type before it is used. This type cannot be changed later. Local variables are not automatically initialized.

Start this quiz