Expressions, Assignment, Casting, and Compound Operators
Free AP Computer Science A multiple-choice practice for topics 1.3–1.6, Unit 1 (Using Objects and Methods). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 1: Using Objects and Methods. Topics 1.3–1.6. This set has 55 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 1.3 Expressions and Output
- 1.4 Assignment Statements and Input
- 1.5 Casting and Range of Variables
- 1.6 Compound Assignment Operators
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 55 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 1.3: Expressions and Output
1.3.A — Develop code to generate output and determine the result that would be displayed.
- 1.3.A.1 System.out.print and System.out.println display information on the computer display. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.
1.3.B — Develop code to utilize string literals and determine the result of using string literals.
- 1.3.B.1 A literal is the code representation of a fixed value.
- 1.3.B.2 A string literal is a sequence of characters enclosed in double quotes.
- 1.3.B.3 Escape sequences are special sequences of characters that can be included in a string. They start with a \ and have a special meaning in Java. Escape sequences used in this course include double quote \", backslash \\, and newline \n.
1.3.C — Develop code for arithmetic expressions and determine the result of these expressions.
- 1.3.C.1 Arithmetic expressions, which consist of numeric values, variables, and operators, include expressions of type int and double.
- 1.3.C.2 The arithmetic operators consist of addition +, subtraction -, multiplication *, division /, and remainder %. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value.
- 1.3.C.3 When dividing numeric values that are both int values, the result is only the integer portion of the quotient. When dividing numeric values that use at least one double value, the result is the quotient.
- 1.3.C.4 The remainder operator % is used to compute the remainder when one number a is divided by another number b.
- 1.3.C.5 Operators can be used to construct compound expressions. At compile time, numeric values are associated with operators according to operator precedence to determine how they are grouped. Parentheses can be used to modify operator precedence. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
- 1.3.C.6 An attempt to divide an integer by the integer zero will result in an ArithmeticException.
Topic 1.4: Assignment Statements and Input
1.4.A — Develop code for assignment statements with expressions and determine the value that is stored in the variable as a result of these statements.
- 1.4.A.1 Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type. A variable is initialized the first time it is assigned a value. Reference types can be assigned a new object or null if there is no object. The literal null is a special value used to indicate that a reference is not associated with any object.
- 1.4.A.2 The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.
- 1.4.A.3 During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the evaluation of the expression.
1.4.B — Develop code to read input.
- 1.4.B.1 Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard.
Topic 1.5: Casting and Range of Variables
1.5.A — Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.
- 1.5.A.1 The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa).
- 1.5.A.2 Casting a double value to an int value causes the digits to the right of the decimal point to be truncated.
- 1.5.A.3 Some code causes int values to be automatically cast (widened) to double values.
- 1.5.A.4 Values of type double can be rounded to the nearest integer by (int) (x + 0.5) for non-negative numbers or (int)(x - 0.5) for negative numbers.
1.5.B — Describe conditions when an integer expression evaluates to a value out of range.
- 1.5.B.1 The constant Integer.MAX_VALUE holds the value of the largest possible int value. The constant Integer.MIN_VALUE holds the value of the smallest possible int value.
- 1.5.B.2 Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
- 1.5.B.3 If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. The result is an int value in the allowed range but not necessarily the value expected.
1.5.C — Describe conditions that limit accuracy of expressions.
- 1.5.C.1 Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory, a round-off error occurs. The result will be rounded to the representable value. To avoid rounding errors that naturally occur, use int values.
Topic 1.6: Compound Assignment Operators
1.6.A — Develop code for assignment statements with compound assignment operators and determine the value that is stored in the variable as a result.
- 1.6.A.1 Compound assignment operators +=, −=, *=, /=, and %= can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the value on the left and the value on the right and then assigns the result to the variable on the left.
- 1.6.A.2 The post-increment operator ++ and post-decrement operator -- are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.