Variables and Data Types

Help Questions

AP Computer Science A › Variables and Data Types

Questions 1 - 10
1

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

boolean

String

double

int

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.

2

A variable of type boolean is capable of storing which of the following sets of values?

The integers 0 and 1

The keywords true and false

Any integer value, where non-zero means true

The string literals "true" and "false"

Explanation

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.

3

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?

String averageScore;

double averageScore;

boolean averageScore;

int averageScore;

Explanation

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.

4

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

studentCount int;

int studentCount;

int studentCount

integer 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.

5

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 boolean variable to indicate if they are a current employee.

As a double variable to represent their salary.

As an object created from a class, making it a reference type.

As a single variable of a primitive type like int.

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.

6

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?

String

boolean

int

double

Explanation

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.

7

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 causes a syntax error because int cannot be printed.

It truncates the decimal portion, reducing numeric 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 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.

8

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());

boolean, because names are true or false values.

String, because it stores sequences of characters.

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.

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?

int, because units are numeric constants.

double, because labels may include decimals.

String, because unit symbols are text.

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.

10

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

total_value

final

$amount

level2

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.

Page 1 of 3