PrepGo

AP Computer Science A Practice Quiz: Expressions and Output

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 16 questions to check your progress.

Question 1 of 16

Consider the following code segment: System.out.println("Hello"); System.out.println("World"); What is the output?

All Questions (16)

Consider the following code segment: System.out.println("Hello"); System.out.println("World"); What is the output?

A) Hello World

B) HelloWorld

C) Hello World

D) World Hello

Correct Answer: A

The `System.out.println` command displays the string and then moves the cursor to a new line. Therefore, "Hello" is printed on the first line, and "World" is printed on the second line.

Consider the following code segment: System.out.print("AP "); System.out.print("Exam"); What is the output?

A) AP Exam

B) APExam

C) AP Exam

D) Exam AP

Correct Answer: C

The `System.out.print` command displays the string but does not move the cursor to a new line. The first statement prints "AP " (including the space), and the second statement prints "Exam" immediately after it on the same line.

What is the result of the following arithmetic expression? 5 + 3 * 2

A) 16

B) 11

C) 13

D) 10

Correct Answer: B

According to the rules of operator precedence, multiplication is performed before addition. The expression is evaluated as 5 + (3 * 2), which simplifies to 5 + 6, resulting in 11.

Which of the following correctly represents a string literal based on the provided content?

A) 'Hello, World!'

B) (Hello, World!)

C) "Hello, World!"

D) Hello, World!

Correct Answer: C

The provided content states that "A string literal is a sequence of characters enclosed in double quotes."

Consider the following code segment: System.out.print("One"); System.out.println("Two"); System.out.print("Three"); What is the output?

A) OneTwo Three

B) One TwoThree

C) OneTwoThree

D) One Two Three

Correct Answer: A

`System.out.print("One")` prints "One" and leaves the cursor. `System.out.println("Two")` prints "Two" on the same line and then moves the cursor to the next line. `System.out.print("Three")` prints "Three" on this new line.

What is the result of the following arithmetic expression? 10 / 2 * 5

A) 1

B) 10

C) 25

D) 2.5

Correct Answer: C

Division and multiplication have the same precedence. Therefore, the expression is evaluated from left to right. First, 10 / 2 is calculated, which is 5. Then, 5 * 5 is calculated, which results in 25.

Consider the following code segment: System.out.println("Line 1\nLine 2"); What is the output?

A) Line 1\nLine 2

B) Line 1 Line 2

C) Line 1 Line 2

D) Line 1Line 2

Correct Answer: C

The escape sequence `\n` represents a newline character. When included in a string literal, it causes the output to move to the next line at that position.

Consider the following code segment: System.out.print("He said, \"Java is fun!\""); What is the output?

A) He said, "Java is fun!"

B) He said, \"Java is fun!\"

C) He said, Java is fun!

D) The code will produce a compilation error.

Correct Answer: A

The escape sequence `\"` is used to include a double quote character within a string literal. The backslash indicates that the following double quote is part of the string, not the end of it.

What is the result of the following arithmetic expression? 17 % 5

A) 3

B) 2

C) 3.4

D) 1

Correct Answer: B

The remainder operator (%) calculates the remainder of an integer division. When 17 is divided by 5, the result is 3 with a remainder of 2.

What is the result of the following arithmetic expression? 20 - 10 / 2 + 3 * 2

A) 11

B) 21

C) 16

D) 8

Correct Answer: B

Following operator precedence, division and multiplication are done first, from left to right. 10 / 2 is 5, and 3 * 2 is 6. The expression becomes 20 - 5 + 6. Then, addition and subtraction are done left to right. 20 - 5 is 15. Finally, 15 + 6 is 21.

Consider the following code segment: System.out.println("File path: C:\\Temp\\notes.txt"); What is the output?

A) File path: C:\\Temp\\notes.txt

B) File path: C:Tempnotes.txt

C) File path: C:\Temp\notes.txt

D) The code will produce a compilation error.

Correct Answer: C

The escape sequence `\\` is used to include a single backslash character within a string literal. Each `\\` in the code is rendered as a single `\` in the output.

According to the provided content, which statement accurately describes operator precedence?

A) Addition has precedence over subtraction.

B) Operators with the same precedence are evaluated from right to left.

C) Multiplication and division have precedence over addition and subtraction.

D) The remainder operator has the lowest precedence.

Correct Answer: C

The content explicitly states: "Multiplication, division, and remainder have precedence over addition and subtraction."

Consider the following code segment: System.out.print(10 / 5); System.out.println(1 + 4); What is the output?

A) 25

B) 2 5

C) 2 5

D) 7

Correct Answer: A

`System.out.print(10 / 5)` evaluates the expression to 2 and prints it without a new line. The cursor remains after the '2'. `System.out.println(1 + 4)` evaluates the expression to 5, prints it on the same line, and then moves the cursor to the next line. The combined output is `25`.

What is the primary functional difference between `System.out.println` and `System.out.print`?

A) `System.out.println` can only be used with string literals.

B) `System.out.print` cannot evaluate arithmetic expressions.

C) `System.out.println` moves the cursor to a new line after output, while `System.out.print` does not.

D) There is no functional difference; they are interchangeable.

Correct Answer: C

The provided content states, "System.out.println moves the cursor to a new line," which is the key distinction from `System.out.print`.

What is the result of the following arithmetic expression? 3 + 10 % 3 * 2 - 1

A) 4

B) 1

C) 6

D) 0

Correct Answer: A

Operators with higher precedence (%, *) are evaluated first, from left to right. First, 10 % 3 results in 1. The expression becomes 3 + 1 * 2 - 1. Next, 1 * 2 results in 2. The expression becomes 3 + 2 - 1. Then, operators with lower precedence (+, -) are evaluated left to right. 3 + 2 is 5. The expression becomes 5 - 1, which results in 4.

Consider the following code segment: System.out.print(1 + 2 * 3); System.out.print(10 % 4); What is the output?

A) 92

B) 72

C) 7 2

D) 9 2

Correct Answer: B

The first statement evaluates 1 + 2 * 3. Multiplication has precedence, so 2 * 3 is 6, and 1 + 6 is 7. `System.out.print` displays '7' without a newline. The second statement evaluates 10 % 4, which is 2. `System.out.print` displays '2' on the same line. The final output is '72'.