AP CSA Unit 1 Day 27: Tracing Multiple Ops

Unit 1 Foundation (Cycle 1) Day 27 of 28 Foundation

Tracing Expressions with Multiple Operations

Section Mixed — Review: All Unit 1

Key Concept

Comprehensive review problems test your ability to identify which concept is being assessed and apply the correct rule. Common AP exam patterns include: integer division disguised by variable names, casting applied at unexpected positions, Math.random() range calculations, and String index boundaries. The most effective strategy is to trace code line by line, writing down the value of every variable after each statement executes. Never skip steps or calculate in your head — the exam rewards careful, systematic tracing.

Consider the following code segment.

String name = "COMPUTER SCIENCE"; int space = name.indexOf(" "); String first = name.substring(0, space); String second = name.substring(space + 1); int total = first.length() + second.length(); System.out.println(first.substring(0, 1) + second.substring(0, 1) + total);

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

Answer: (A) CS15

Trace step by step:

space: indexOf(" ") finds the space at index 8.

first: substring(0, 8) = "COMPUTER" (8 characters).

second: substring(9) = "SCIENCE" (7 characters).

total: 8 + 7 = 15.

Output: "C" + "S" + 15 = "CS15". Since "C" starts the chain, all + is concatenation.

Why Not the Others?

(B) 16 would be the full length including the space. But first.length() is 8 and second.length() is 7, totaling 15. The space character is not part of either substring.

(C) second.substring(0, 1) gets the first character of "SCIENCE", which is "S", not "O". "O" is the second character of "COMPUTER".

(D) There is no space in the output. The concatenation of "C" + "S" + 15 produces "CS15" with no space between S and 15.

Common Mistake

When splitting a String at a space, substring(0, spaceIndex) gets everything before the space, and substring(spaceIndex + 1) gets everything after it. The space itself is excluded from both halves.

AP Exam Tip

Splitting Strings at a delimiter is a very common AP exam pattern. The key steps: (1) find the delimiter with indexOf, (2) extract before with substring(0, idx), (3) extract after with substring(idx + 1). Practice this until it is automatic.

Review this topic: Section Mixed — Review: All Unit 1 • Unit 1 Study Guide

More Practice

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.