AP CSA Unit 1: Primitive Types, Variables, and Expressions

273 multiple-choice questions on AP Computer Science A Unit 1, Primitive Types, Variables, and Expressions, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 35 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 1.1 Variables and primitive types
  2. 1.2 Arithmetic expressions and operators
  3. 1.3 Compound assignment, increment, and casting

Key terms

1.1 Variables and primitive types

Declaration
A statement that introduces a variable by giving its type and name, such as int score;
Initialization
Giving a variable its first value, either in the declaration or in a later assignment
Identifier
The name a programmer gives a variable, method, or class
Reserved word
A word with a fixed meaning in Java, such as int, double, class, or final, that can't be used as an identifier
Data type
The kind of value a variable holds, which decides how it is stored and which operations work on it
Uninitialized variable
A local variable that has been declared but not yet assigned a value
Assignment operator (=)
Stores the value of the expression on its right in the variable on its left
Compile-time error
An error the compiler finds before the program runs, such as a type mismatch or a missing semicolon
Integer.MAX_VALUE
The largest value an int can hold: 2147483647
Integer.MIN_VALUE
The smallest value an int can hold: −2147483648
Named constant
A final variable that gives a meaningful name to a value that never changes
print vs println
System.out.print leaves the cursor on the same line; System.out.println moves to a new line after printing
Primitive type
A basic data type that stores a value directly: int, double, boolean
final
Keyword that makes a variable constant after initialization

1.2 Arithmetic expressions and operators

Operator precedence
The rules that decide which operators in an expression are applied first: *, /, and % before + and −
Left-to-right associativity
Operators with the same precedence, such as * and /, are applied in order from left to right
Arithmetic expression
A combination of values, variables, and operators such as +, −, *, /, and % that evaluates to a number
Operand
A value or variable that an operator acts on
ArithmeticException
The run-time exception thrown when an int is divided by zero with / or %
Mixed-type expression
An operation with one int operand and one double operand; the int is promoted and the result is a double
Remainder
The amount left over after int division, computed with %; for a nonnegative dividend it is between 0 and the divisor − 1
Unary minus
The − operator applied to a single operand to negate it; it binds more tightly than *, /, and %
Integer division
Dividing two ints, which truncates the result toward zero
Modulus operator (%)
Gives the remainder of integer division

1.3 Compound assignment, increment, and casting

Casting
Converting a value to another type, such as (int) 3.9
Integer overflow
When an int calculation exceeds Integer.MAX_VALUE or goes below Integer.MIN_VALUE and wraps around
Compound assignment
Operators such as +=, −=, *=, /=, %= that update a variable
Widening conversion
An automatic conversion to a type that can hold every value of the original, such as int to double
Narrowing conversion
A conversion that may lose information, such as double to int; Java requires an explicit cast
Truncation
Dropping the fractional part of a number instead of rounding it
Increment operator (++)
Adds 1 to a numeric variable; in the AP subset it is used as a standalone statement, such as count++;
Decrement operator (−−)
Subtracts 1 from a numeric variable, used as a standalone statement such as lives--;
Rounding by casting
Adding 0.5 to a positive double, or subtracting 0.5 from a negative one, before casting to int, to round to the nearest whole number
Floating-point imprecision
Small errors in double results because many decimal fractions, such as 0.1, can't be stored exactly in binary
Cast operator
A type name in parentheses, such as (int) or (double), that converts the value immediately after it

Sample practice questions

  1. Which of the following is a primitive type in Java?

    1. String
    2. ArrayList
    3. double
    4. Integer

    Answer: C. double The AP subset's primitive types are int, double and boolean. String, ArrayList and Integer are classes (reference types).

  2. What is the value of 17 / 5 in Java?

    1. 3.4
    2. 4
    3. 3
    4. 2

    Answer: C. 3 Dividing two ints performs integer division, which truncates the decimal part.

  3. After int x = 5; x += 3; x *= 2; what is the value of x?

    1. 13
    2. 11
    3. 10
    4. 16

    Answer: D. 16 x becomes 8 after += 3, then 16 after *= 2.

  4. Which declaration correctly stores the value 3.75?

    1. double price = 3.75;
    2. int price = 3.75;
    3. boolean price = 3.75;
    4. String price = 3.75;

    Answer: A. double price = 3.75; A decimal value needs a double. Assigning 3.75 to an int is a compile-time error.

  5. What is the value of 17 % 5?

    1. 3
    2. 2
    3. 3.4
    4. 0

    Answer: B. 2 The remainder operator % gives the remainder of 17 ÷ 5, which is 2.

  6. After int count = 4; count++; count--; count++; what is count?

    1. 4
    2. 6
    3. 5
    4. 3

    Answer: C. 5 ++ adds 1 and −− subtracts 1: 4 → 5 → 4 → 5.

  7. A variable declared with the keyword final

    1. can't be assigned a new value after it is initialized
    2. can only hold whole numbers, such as int or long values
    3. is automatically set to 0 when it is declared
    4. can be changed only inside the loop that declares it

    Answer: A. can't be assigned a new value after it is initialized final makes a constant; any later assignment is a compile-time error.

  8. What is the value of 7.0 / 2?

    1. 3
    2. 3.0
    3. 4
    4. 3.5

    Answer: D. 3.5 If either operand is a double, the division is floating-point.

  9. What is the value of (int) 7.9?

    1. 7
    2. 8
    3. 7.9
    4. 7.0

    Answer: A. 7 Casting a double to int truncates toward zero.

  10. What is the value of Integer.MAX_VALUE + 1 when evaluated as an int?

    1. Integer.MAX_VALUE, because the value is capped
    2. A compile-time error, because the value is too big
    3. Integer.MIN_VALUE, because of integer overflow
    4. 2147483648, because Java widens the result to long

    Answer: C. Integer.MIN_VALUE, because of integer overflow int arithmetic wraps around. Adding 1 to the largest int overflows to the smallest int, −2147483648.

Practice all 273 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.