AP Computer Science a Quiz: Variables And Data Types
20 questions · exam conditions
0:00
Variables And Data TypesQuestion 1 of 20

A developer is writing a program to manage employee records. An employee is a complex entity with a name, an ID, a salary, and a hire date. How would an individual employee typically be represented in the program?

As a single variable of a primitive type like int.
As an object created from a class, making it a reference type.
As a single boolean variable to indicate if they are a current employee.
As a double variable to represent their salary.
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Variables And Data Types

Practice Variables And Data Types in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Variables And Data Types, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

A developer is writing a program to manage employee records. An employee is a complex entity with a name, an ID, a salary, and a hire date. How would an individual employee typically be represented in the program?

  1. As a single variable of a primitive type like int.
  2. As an object created from a class, making it a reference type. (correct answer)
  3. As a single boolean variable to indicate if they are a current employee.
  4. As a double variable to represent their salary.

Explanation: Complex entities with multiple attributes (like an employee) are best modeled as objects. A variable that holds an employee object would therefore be of a reference type. While double or boolean might represent parts of the employee's data, the entire employee record would be an object.

Question 2

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);
  1. It causes a syntax error because int cannot be printed.
  2. It truncates the decimal portion, reducing numeric precision. (correct answer)
  3. It makes the value out of scope outside toFahrenheit.
  4. It lets int store decimals, so output is unchanged.

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.

Question 3

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());
  1. int, because names are stored as whole numbers.
  2. boolean, because names are true or false values.
  3. String, because it stores sequences of characters. (correct answer)
  4. double, because names need decimal precision.

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.

Question 4

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?

  1. int, because units are numeric constants.
  2. double, because labels may include decimals.
  3. String, because unit symbols are text. (correct answer)
  4. boolean, because units are true/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 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.

Question 5

Which of the following is an invalid variable name in Java?

  1. total_value
  2. level2
  3. final (correct answer)
  4. $amount

Explanation: The word final is a reserved keyword in Java and cannot be used as a variable name. The other options are all valid identifiers.

Question 6

A program requires a variable to store whether a file has been successfully saved. Which declaration is most suitable for this task?

  1. int fileSaved;
  2. double fileSaved;
  3. boolean fileSaved; (correct answer)
  4. String fileSaved;

Explanation: 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.

Question 7

What is the primary distinction between how primitive types and reference types store data?

  1. Primitive types are for numbers only, while reference types are for text only.
  2. Primitive type variables hold actual values, while reference type variables hold memory addresses pointing to objects. (correct answer)
  3. Primitive types are declared with lowercase keywords, while reference types are declared with uppercase keywords.
  4. Primitive types can only be used inside methods, while reference types can be used anywhere in a class.

Explanation: The fundamental difference is that a primitive variable stores the data value directly, whereas a reference variable stores a reference (a memory address) to the location where an object's data is stored.

Question 8

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?

  1. int (correct answer)
  2. double
  3. boolean
  4. String

Explanation: 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.

Question 9

What is stored inside a variable that has been declared with a primitive data type?

  1. A reference pointing to an object in the computer's memory.
  2. A literal value that corresponds to that primitive data type. (correct answer)
  3. A string representation of the value, regardless of the type.
  4. The name of the data type itself, such as 'int' or 'double'.

Explanation: 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.

Question 10

Which of the following statements about Java variables is true?

  1. A variable's data type can be changed to another type after its declaration.
  2. A variable must be declared with a data type before it can be assigned a value. (correct answer)
  3. Variable names are not case-sensitive, so score and Score are the same.
  4. All variables declared in a method are automatically initialized to a default value.

Explanation: 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.

Question 11

Which of the following is NOT one of the three primitive data types specified for the AP Computer Science A course?

  1. boolean
  2. float (correct answer)
  3. double
  4. int

Explanation: 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.

Question 12

What is the main purpose of a data type in a variable declaration?

  1. It specifies the initial value of the variable, which cannot be changed later.
  2. It determines the name of the variable that will be used to access its value.
  3. It informs the compiler of the variable's memory requirements and the set of valid operations. (correct answer)
  4. It provides a descriptive comment for other programmers about the variable's use.

Explanation: A data type is essential because it defines the kind of data a variable can hold (e.g., int, double). This tells the compiler how much memory to allocate and what operations are legal (e.g., you can perform arithmetic on an int but not on a boolean).

Question 13

Which statement correctly declares a double variable named TAXRATETAX_RATE and initializes it to 0.065 in a single line of code?

  1. double TAX_RATE; TAX_RATE = 0.065;
  2. TAX_RATE double = 0.065;
  3. double TAX_RATE = 0.065; (correct answer)
  4. double TAX_RATE = "0.065";

Explanation: 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.

Question 14

Which of the following correctly declares three boolean variables named hasKey, isOpen, and isLocked in a single statement?

  1. boolean hasKey, isOpen, isLocked; (correct answer)
  2. boolean hasKey; isOpen; isLocked;
  3. boolean hasKey, boolean isOpen, boolean isLocked;
  4. boolean hasKey isOpen isLocked;

Explanation: To declare multiple variables of the same type in one statement, the data type is listed once, followed by the variable names separated by commas, and the statement is terminated with a semicolon.

Question 15

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?

  1. The ISBN of a book, which may contain hyphens.
  2. The checkout status of a book (checked out or available).
  3. The year the book was published. (correct answer)
  4. The replacement cost of the book.

Explanation: 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.

Question 16

Consider the declaration double gpa;. What type of information can be stored in the gpa variable?

  1. Only whole numbers, such as 3.
  2. A real number, which may have a fractional part, such as 3.75. (correct answer)
  3. A logical value of either true or false.
  4. A sequence of characters, such as "high".

Explanation: 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).

Question 17

Which of the following best describes the concept of a variable in programming?

  1. A fixed value in a program that does not change, like the number pi.
  2. A named storage location in memory that holds a value of a specific data type. (correct answer)
  3. A block of code that performs a specific task when called.
  4. A rule that the programming language compiler must follow.

Explanation: 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.

Question 18

Which code segment shows a variable declaration but NOT an initialization?

  1. int score = 100;
  2. int score; (correct answer)
  3. score = 100;
  4. String score = "100";

Explanation: 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.

Question 19

In the AP Computer Science A course, which of the following is considered a reference data type?

  1. int
  2. double
  3. boolean
  4. String (correct answer)

Explanation: int, double, and boolean are the three primitive data types covered in the course. String is a reference type because it represents an object, not a single primitive value.

Question 20

Which of the following correctly declares an integer variable named studentCount?

  1. int studentCount; (correct answer)
  2. studentCount int;
  3. integer studentCount;
  4. int studentCount

Explanation: 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.