AP Computer Science A • Unit 1.3

Expressions and Output

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

Q1B
Q2B
Q3C
Q4A
Q5A
Q6A
Q7C
Q8A
Q9C
Q10C
Q11B
Q12C
Q13C
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. Which choice shows the output?

System.out.println("A\n\"B\"\\C");
AA\n\"B\"\\C
BA
"B"\C
CA
B C
DThe code does not compile.
Correct answer: B. Each two-character escape sequence produces one character, so \n starts a new line and the quotation marks and backslash appear.
Explanation

An escape sequence begins with a backslash and gives the next character a special meaning. It is written with two source-code characters but represents one character in the resulting text, so the printed result is shorter than the literal.

  • A is not correct: this choice repeats the source characters instead of the resulting text, but each escape sequence is two source-code characters that represent one character.
  • C is not correct: the quotation marks and the backslash are dropped here, while \" places a quotation mark and \\ places one backslash in the text.
  • D is not correct: the code compiles, because each backslash begins one of the valid escape sequences rather than an invalid one.
Q2

Consider the following code segment. Which choice shows the output?

System.out.print("A");
System.out.println("B");
System.out.print("C");
AA
B
C
BAB
C
CABC
DA
BC
Correct answer: B. The first print stays on the line, the println ends it, and the last print follows on the next line.
Explanation

The print method stays on the current line. The println method moves to a new line after displaying its value. Neither method inserts spaces automatically, so the characters run together until a println ends the line.

  • A is not correct: this treats every output statement as ending its line, but print stays on the current line rather than moving to a new one.
  • C is not correct: this ignores the println in the middle, which moves to a new line after displaying its value.
  • D is not correct: this places the line break before B instead of after it, but the break comes from the statement that displays B.
Q3

Consider the following source-code line. What happens when Java processes this line?

System.out.print("Folder: C:\Users");
AIt prints the path exactly as shown.
BIt prints the path without backslashes.
CIt produces a syntax error because \U is not a valid escape.
DIt produces integer division by zero.
Correct answer: C. A backslash begins an escape sequence, and \U is not a valid one, so the line does not compile.
Explanation

A backslash in a String literal gives the next character a special meaning. Only certain escape sequences are valid, and an invalid one is a syntax error. Placing one backslash in the text requires \\.

  • A is not correct: the backslash is not printed as itself, because it begins an escape sequence, and this one is invalid, so nothing is printed at all.
  • B is not correct: the line never runs and prints nothing, since an invalid escape sequence is a syntax error rather than a silent removal.
  • D is not correct: an ArithmeticException is a run-time error caused by integer division by 0, and this line contains no division.
Q4

Consider the following code segment. Which choice shows the output?

System.out.println((6 + 2) * 3);
System.out.println(6 + 2 * 3);
A24
12
B24
24
C18
12
D8
12
Correct answer: A. Parentheses group 6 + 2 first and give 24, while the second line multiplies before adding and gives 12.
Explanation

Parentheses explicitly group an expression. Multiplication, division, and remainder bind more tightly than addition and subtraction. First determine the grouping from parentheses and precedence, then evaluate the grouped expression to reach the printed value.

  • B is not correct: the second line has no parentheses, so multiplication binds more tightly than addition and the sum is not formed first.
  • C is not correct: neither line produces 18, because the parenthesized sum 6 + 2 is multiplied by 3 and gives 24.
  • D is not correct: the value 8 is only the parenthesized sum 6 + 2, and the multiplication by 3 still has to be applied.
Q5

Which statement best describes an expression?

AIt is code that evaluates to a value.
BIt must always print a value.
CIt is another name for a class.
DIt can contain only one literal.
Correct answer: A. An expression is code that Java evaluates to produce a value, which is exactly what this choice describes.
Explanation

An arithmetic expression may contain numeric values, variable names, operators, and parentheses. Producing a value does not automatically display it, so an expression and the statement that prints its value are different things.

  • B is not correct: producing a value does not automatically display it, and a program must use an output statement for the value to be seen.
  • C is not correct: an expression is code that evaluates to a value, which is not what a class is in a Java program.
  • D is not correct: an arithmetic expression may contain numeric values, variable names, operators, and parentheses, so it is not limited to a single literal.
Q6

In the expression 8 - 3, which statement is correct?

AThe minus sign is an operator; 8 and 3 are its operands.
BThe minus sign is an operand; 8 is the operator.
CThe expression contains no operator.
DThe minus sign starts a String literal.
Correct answer: A. The operator tells Java which operation to perform, and the operands are the values on which that operator acts.
Explanation

In 7 + 3 the operator is + and the operands are 7 and 3. The same reading applies to any arithmetic expression, where the symbol names the operation and the surrounding values are acted on.

  • B is not correct: this reverses the two terms, because the symbol is the operator and the numeric values are the operands it acts on.
  • C is not correct: the minus sign is an operator, so the expression does contain one and produces a value from its two operands.
  • D is not correct: a String literal is a sequence of characters enclosed in double quotation marks, not something that a minus sign begins.
Q7

What is the value of 30 / 5 * 2?

A2
B6
C12
D30
Correct answer: C. / and * share a precedence level, so 30 / 5 * 2 groups from the left and divides before multiplying.
Explanation

Operators on the same level are evaluated from left to right. The division happens first, and its result is then multiplied. Grouping from the right instead is valid only when explicit parentheses are written.

  • A is not correct: this value comes from grouping the expression from the right, which is valid only when explicit parentheses are written around the last two operands.
  • B is not correct: this is only the result of the division, and the multiplication on the same precedence level still has to be applied.
  • D is not correct: this is the left operand by itself, and both the division and the multiplication still have to be evaluated.
Q8

What is the value of 11 / 4 when both operands are int values?

A2
B2.75
C3
DIt does not compile.
Correct answer: A. Both operands of / are int values, so Java discards the fractional part and the result is 2.
Explanation

Java performs integer division when both operands of / are int values. The result is an int, and any fractional part is discarded rather than rounded, so the result truncates toward zero.

  • B is not correct: the value 2.75 keeps the fractional part, which requires at least one operand to be a double rather than two int values.
  • C is not correct: the value 3 would be the rounded result, but integer division discards the fractional part instead of rounding it.
  • D is not correct: the code compiles, because dividing one int by another is ordinary integer division and is not an error.
Q9

What is the value of 11 / 4.0?

A2
B2.0
C2.75
DIt causes an ArithmeticException.
Correct answer: C. One operand is a double, so Java uses floating-point arithmetic and the division keeps its fractional part.
Explanation

If at least one operand is a double, Java uses floating-point arithmetic and the division keeps its fractional part. Only when both operands are int values is the fractional part discarded.

  • A is not correct: the value 2 is the integer-division result, which happens only when both operands are int values rather than one double.
  • B is not correct: the value 2.0 discards the fractional part that floating-point arithmetic keeps, so it is not what this expression produces.
  • D is not correct: an ArithmeticException is caused by integer division by 0, and neither operand here is 0.
Q10

For nonnegative integers, what is the value of 8 % 13?

A0
B5
C8
D13
Correct answer: C. The left operand is smaller than the positive right operand, so the remainder after division is the left operand itself.
Explanation

The % operator produces the remainder after division. For nonnegative integers, when the left operand is smaller than a positive right operand, no complete group fits and the whole left operand is left over.

  • A is not correct: a remainder of 0 would mean the division comes out evenly, but 8 is smaller than 13 and is left over entirely.
  • B is not correct: the value 5 is the amount by which 13 exceeds 8, not the remainder that division leaves behind.
  • D is not correct: the value 13 is the right operand, and a remainder is what is left over after division by that operand.
Q11

What is the value of 18 - 8 / 2?

A5
B14
C10
D2
Correct answer: B. Division binds more tightly than subtraction, so 8 / 2 is evaluated first and its result is subtracted from 18.
Explanation

Multiplication, division, and remainder bind more tightly than addition and subtraction. First determine the grouping from precedence, then evaluate. Here the division is grouped by itself, and only its result takes part in the subtraction.

  • A is not correct: this comes from subtracting before dividing, but precedence groups the division first unless explicit parentheses say otherwise.
  • C is not correct: this is only the subtraction 18 - 8, and it ignores the division that precedence requires to be evaluated first.
  • D is not correct: this is the right operand of the division by itself, and the subtraction from 18 still has to happen.
Q12

What does \" represent inside a String literal?

AA newline
BA tab
CA quotation mark in the text
DTwo backslashes
Correct answer: C. The escape sequence \" places a quotation mark inside the text of a String literal.
Explanation

A String literal is a sequence of characters enclosed in double quotation marks, so a quotation mark inside the text needs an escape sequence. The two source-code characters represent one character in the resulting text.

  • A is not correct: a new line in the text comes from \n, which is a different escape sequence from the one shown here.
  • B is not correct: the escape sequences in this topic place a quotation mark, one backslash, or a new line in the text.
  • D is not correct: the sequence \\ places one backslash in the text, and two source-code characters represent a single character rather than two.
Q13

What happens when Java evaluates integer division by the int value 0?

AThe expression produces 0.
BThe compiler rejects the division operator.
CExecution is interrupted by an ArithmeticException.
DJava silently changes the divisor to 1.
Correct answer: C. Integer division by 0 causes a run-time error named ArithmeticException, and it interrupts execution.
Explanation

Integer division by 0 is not reported when the program is compiled. It happens while the program runs, and the resulting ArithmeticException interrupts execution rather than producing a value.

  • A is not correct: the expression produces no value at all, because execution is interrupted instead of returning 0 to the output statement.
  • B is not correct: the compiler accepts the division operator, and the problem appears while the program runs rather than when it is compiled.
  • D is not correct: Java does not replace the divisor with 1, because integer division by 0 causes an ArithmeticException instead.