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 Variables and primitive types
- 1.2 Arithmetic expressions and operators
- 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
Which of the following is a primitive type in Java?
- String
- ArrayList
- double
- Integer
Answer: C. double The AP subset's primitive types are int, double and boolean. String, ArrayList and Integer are classes (reference types).
What is the value of 17 / 5 in Java?
- 3.4
- 4
- 3
- 2
Answer: C. 3 Dividing two ints performs integer division, which truncates the decimal part.
After int x = 5; x += 3; x *= 2; what is the value of x?
- 13
- 11
- 10
- 16
Answer: D. 16 x becomes 8 after += 3, then 16 after *= 2.
Which declaration correctly stores the value 3.75?
- double price = 3.75;
- int price = 3.75;
- boolean price = 3.75;
- 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.
What is the value of 17 % 5?
- 3
- 2
- 3.4
- 0
Answer: B. 2 The remainder operator % gives the remainder of 17 ÷ 5, which is 2.
After int count = 4; count++; count--; count++; what is count?
- 4
- 6
- 5
- 3
Answer: C. 5 ++ adds 1 and −− subtracts 1: 4 → 5 → 4 → 5.
A variable declared with the keyword final
- can't be assigned a new value after it is initialized
- can only hold whole numbers, such as int or long values
- is automatically set to 0 when it is declared
- 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.
What is the value of 7.0 / 2?
- 3
- 3.0
- 4
- 3.5
Answer: D. 3.5 If either operand is a double, the division is floating-point.
What is the value of (int) 7.9?
- 7
- 8
- 7.9
- 7.0
Answer: A. 7 Casting a double to int truncates toward zero.
What is the value of Integer.MAX_VALUE + 1 when evaluated as an int?
- Integer.MAX_VALUE, because the value is capped
- A compile-time error, because the value is too big
- Integer.MIN_VALUE, because of integer overflow
- 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.
More AP Computer Science A
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 3: Boolean Expressions and if Statements
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 6: Arrays
- Unit 7: ArrayList
- Unit 8: 2D Arrays
- Unit 9: Recursion, Searching, and Sorting
Studying for a test in your own class? Make a practice test from your class notes.