Variables and data types are fundamental building blocks of Java programs. In the current AP Computer Science A framework, students work with three primitive data types: int, double, and boolean. The course also introduces reference types, which are used for objects.
In this interactive practice set, we cover Topic 1.2: Variables and Data Types. These 10 multiple-choice questions test your ability to declare and use variables, identify data types, recognize uninitialized local variables, understand type compatibility, and distinguish primitive values from reference types.
Key Concepts to Remember Before You Start:
-
Primitive Data Types:
The three primitive data types used in AP Computer Science A are
int,double, andboolean. - Variables: Every variable has a name and an associated data type. A primitive variable stores a value of its primitive type.
- Uninitialized Local Variables: Local variables declared inside a method do not automatically receive default values. A local variable must be assigned a value before it is used.
-
Type Compatibility:
An
intvalue can be assigned to adouble, because Java can perform the widening conversion automatically. Adoublecannot be assigned to anintwithout an explicit cast. -
Reference Types:
Reference types are used for objects rather than primitive values. For example,
Stringis a reference type.
AP CSA 1.2: Variables and Data Types Practice
score = score + 5;
System.out.println(score);
score is declared but has not been initialized before being used in
score + 5, the code produces a compilation error.System.out.println(temperature);
98.6 is a double. Java does not automatically convert a
double to an int because that conversion can lose the fractional part.
An explicit cast would be required, such as
int temperature = (int) 98.6;.System.out.println(price);
int value to a double variable is allowed.
Java performs a widening conversion, so the value 50 is represented as
the double value 50.0.is_Winner is valid.int currentLives = MAX_LIVES;
MAX_LIVES = MAX_LIVES – 1;
System.out.println(currentLives);
final cannot be assigned a new value after it has been
initialized. The statement
MAX_LIVES = MAX_LIVES - 1;
attempts to assign a new value to a final variable, causing a compilation error.name?String is a reference type, not one of the three primitive data types used in
AP Computer Science A. A variable of a reference type refers to an object.
The primitive types used in the course are int, double, and
boolean.int b = a;
a = 10;
System.out.println(b);
int b = a; executes, the current value of a, which is
5, is assigned to b. Changing a later does not change
the value stored in the separate primitive variable b.System.out.println(x + y);
int x, y = 5; declares both variables as integers, but only
y is initialized. The local variable x has not been assigned a value,
so using it in x + y causes a compilation error.int q = 8;
int temp = p;
p = q;
q = temp;
System.out.println(p + ” ” + q);
temp stores the value of p, which is 2.
Then p receives the value of q, which is 8.
Finally, q receives the value stored in temp.
Therefore, the output is 8 2.boolean variable can hold only the Boolean values true or
false. In option C, "true" is enclosed in quotation marks, so it is a
String literal rather than the Boolean value true. A String cannot be assigned to a
primitive boolean variable.Unit 1.2 Variables and Data Types Practice Complete!
Why Variables and Data Types Matter for AP CSA
Variables and data types are fundamental to reading and writing Java code on the AP Computer Science A exam. Understanding what type of value a variable can store, when a variable must be initialized, and when Java allows one type of value to be assigned to another will help you analyze code accurately.
Ready for the Next Topic?
Now that you have practiced variables and data types, continue with Topic 1.3: Expressions and Output to practice Java expressions, operators, evaluation, and output.