AP Computer Science A • Unit 1.4

Assignment and Input

Detailed answer explanations for the Unit 1.4 multiple-choice practice questions.

Q1C
Q2B
Q3D
Q4D
Q5B
Q6A
Q7D
Q8A
Q9C
Q10B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

int level = 6;
level = level + 4;
level = level * 3;
System.out.println(level);
A6
B10
C30
D18
Correct answer: C. Each assignment replaces the value stored before it, so 6 becomes 10 and then 30 before printing.
Explanation

Java evaluates the right side of each assignment using the value the variable holds at that moment, then stores the result back in that variable. The second statement uses 6 to produce 10, and the third uses 10 to produce 30.

  • A is not correct: 6 is only the value stored before the second statement runs, and any later assignment replaces the current value.
  • B is not correct: 10 is the value held after the second statement, but the third statement runs and replaces it before anything is printed.
  • D is not correct: this value would come from multiplying the original 6 by 3, but the third statement uses the current value 10.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

int score = 7;
int bonus = 3;
score = score + bonus;
bonus = 10;
System.out.println(score);
A7
B10
C17
D20
Correct answer: B. The third statement stores 10 in score, and changing bonus afterwards does not recalculate score.
Explanation

Assignment stores a value, not a permanent formula. Once score receives the result of the right-hand expression, a later assignment to bonus does not update it. Another assignment to score would be required.

  • A is not correct: 7 is the value score holds before the third statement, which evaluates the sum and replaces that value.
  • C is not correct: this treats the final assignment to bonus as though it changed score again, but score keeps the value already stored.
  • D is not correct: no statement stores this value, because the sum was evaluated once using the value bonus held at that moment.
Q3

Consider the following code segment. Which of the following best describes this code?

double average = 6.4;
int wholeAverage = average;
AIt compiles and stores 6 in wholeAverage.
BIt compiles and stores 7 in wholeAverage.
CIt does not compile because double variables cannot store decimal values.
DIt does not compile because a double is not automatically stored in an int.
Correct answer: D. A double value is not automatically stored in an int, so the second line does not compile.
Explanation

After evaluating the right side, Java checks whether the resulting value can be stored in the type declared on the left. A double may contain a fractional part that an int cannot store, so Java refuses the assignment instead of silently discarding information.

  • A is not correct: the assignment is refused rather than compiled, because storing the value that way would mean silently discarding information.
  • B is not correct: the code does not compile, and Java does not adjust the value to a nearby whole number in order to make it fit.
  • C is not correct: a double is the type that can contain a fractional part; the difficulty is the direction of this assignment, from double to int.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

double price = 4.5;
double quantity = 3.0;
double total = price * quantity;
System.out.println(total);
A7.5
B12.0
CThe code does not compile.
D13.5
Correct answer: D. Java evaluates price * quantity first, producing 13.5, and then stores that result in total.
Explanation

Every assignment has a destination on the left and an expression on the right, and the right-hand expression may be a calculation made from values and operators. Java completes that calculation before it changes the variable on the left.

  • A is not correct: this is the value that adding the two numbers would give, but the right side price * quantity multiplies them instead.
  • B is not correct: this would require price to be treated as 4.0, but Java uses the value each variable currently stores.
  • C is not correct: both variables are declared as double, and the result of the calculation is compatible with the type of total.
Q5

Consider the following code segment. What is printed as a result of executing the code segment?

int guests = 6;
int total = guests + 2;
guests = 9;
System.out.println(total);
A6
B8
C62
DThe code does not compile.
Correct answer: B. total receives 8 when the second statement runs, and the later change to guests does not update it.
Explanation

Assignment stores a value, not a permanent formula. The right side is evaluated once, using the value guests holds at that moment. Changing guests afterwards would require another assignment before total would change.

  • A is not correct: 6 is the value first stored in guests, while the variable being printed is total, which holds the sum.
  • C is not correct: the right side is a calculation whose result is a single number, so the digits of the two operands are not placed side by side.
  • D is not correct: every variable here is assigned a value before it is read, and each assigned value is compatible with its declared type.
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

String destination = null;
destination = "Rome";
System.out.println(destination);
ARome
Bnull
C"null"
DThe code does not compile.
Correct answer: A. The second statement replaces null with a reference to an object whose text is Rome.
Explanation

A reference variable can store null if it does not currently refer to an object. Any later assignment replaces the current value, so destination ends up referring to a String object instead of no object.

  • B is not correct: null is only the first value stored, and the following assignment replaces it before the value is printed.
  • C is not correct: the variable is never assigned the literal "null", which would be four characters of actual text rather than the value assigned here.
  • D is not correct: a reference variable may be assigned null and may later be assigned a value of its own type, so both statements are allowed.
Q7

Consider the following code segment. Which statement best describes the code segment?

int visitors;
visitors = visitors + 1;
System.out.println(visitors);
AIt prints 0.
BIt prints 1.
CIt prints the word visitors.
DIt does not compile.
Correct answer: D. visitors is read on the right side before it has been assigned any value, so the program does not compile.
Explanation

A local variable must be assigned a value before it is read or used in an expression. A declaration alone does not make a local variable ready to read, and the right-hand expression here reads the variable.

  • A is not correct: a declaration alone does not supply a value, so the local variable does not begin holding 0.
  • B is not correct: the addition never runs, because the compiler checks the statements in execution order and rejects the program before it runs.
  • C is not correct: a variable name is used to find the value stored in the variable, so the name itself is not printed as text.
Q8

Consider the following code segment. What is printed as a result of executing the code segment?

int width = 4;
int area = width * width;
width = 7;
System.out.println(area);
A16
B28
C49
DThe code does not compile.
Correct answer: A. area stores 16 when the second statement runs, and the later change to width does not recalculate it.
Explanation

Assignment stores the value produced by the right-hand expression, not a permanent formula. Changing width later would not automatically recalculate area; another assignment to area would be required.

  • B is not correct: this mixes the earlier and later values of width, but the multiplication used the same stored value for both operands.
  • C is not correct: this would require the multiplication to run again after width changes, and no later assignment to area appears.
  • D is not correct: each variable is assigned a value before it is read, and both stored values are compatible with the declared types.
Q9

Consider the following code segment. What is printed as a result of executing the code segment?

int length = 4;
double perimeter = length * 4;
System.out.println(perimeter);
A4
B16
C16.0
DThe code does not compile.
Correct answer: C. The right side produces 16, and an int value stored in a double variable becomes 16.0.
Explanation

Java finishes the right-hand expression first and then checks whether the resulting value can be stored in the type on the left. This direction is allowed, in the same way that distance = meters; leaves distance storing 12.0.

  • A is not correct: 4 is the value stored in length, while the variable being printed holds the result of the multiplication.
  • B is not correct: the result is stored in a variable declared as double, so the value kept there is not the plain whole number.
  • D is not correct: this assignment runs from int to double, which is the direction Java allows without an explicit cast.
Q10

Which statement best describes input?

AInput must be written as a literal before the program runs.
BInput is data a program receives while it is running.
CInput can come only from a keyboard.
DInput must always be stored as a String.
Correct answer: B. Input is data that a program receives while it is running, rather than a value already written in the source code.
Explanation

Source code is written before a program runs, so it cannot contain every value that a future user may provide. A literal works when the programmer already knows the value; otherwise the program must receive the value while running.

  • A is not correct: a literal only carries a value the programmer already knows, and values such as a user's age or a price may not be known in advance.
  • C is not correct: input may arrive as text, sound, touch, an image, or another form of data, so a keyboard is only one possible source.
  • D is not correct: an input value may have other types, as when input.nextInt() supplies an int value for a variable declared as int.