Before the bell
You are responsible for all of this before the end of class.
- The discussion form in the I Do deck. All three sections. If you are here, this should be done.
- Your classwork. This form on the right. Section 1 before the exit ticket pops up; Section 2 past the floor.
- Today's Bench Basics activity: Inside a String. It opens later in class and lives on slide 4 of this deck.
- The exit ticket. It opens 15 minutes before the bell and lives on the last slide of this deck.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
Inside a String: substring and indexOf
This reading shows how Java numbers the characters in a String and how two String methods use those numbers.
Positions Counted from 0
Every character in a String has a position, called its index, and the first character is at index 0. A space is a character too, so it takes up a position in exactly the same way that a letter or a digit does. Because counting starts at 0, the last index of any String is always one less than its length, as the AP course description states. The table below gives the positions in "New Orleans", which has 11 characters, so its last index is 10.
Character N e w O r l e a n s Index 0 1 2 3 4 5 6 7 8 9 10
The method `substring(start, end)` returns the characters from `start` up to but not including `end`, so it returns `end - start` characters. The method `substring(start)` begins at `start` and runs to the end of the String. The method `indexOf` returns the index of the first match, or -1 when there is no match. Both methods, including the two forms of `substring`, are on the Java Quick Reference, which students are given during the AP exam.
One Worked Snippet
1String city = "New Orleans"; 2System.out.println(city.substring(4, 7)); 3System.out.println(city.indexOf("O")); 4System.out.println(city.substring(city.indexOf(" ") + 1));
Orl 4 Orleans
Line 1 stores "New Orleans" in `city`, and line 2 takes the characters at index 4, 5 and 6, which are O, r and l, stopping before index 7. Line 2 therefore prints Orl, which is 7 - 4, or 3 characters. Line 3 prints 4, because the first O in the String is at index 4. Line 4 is worked from the inside out, starting with the method call inside the parentheses. First, `city.indexOf(" ")` finds the space at index 3, and then 3 + 1 is 4. Finally, `substring(4)` runs from index 4 to the end of the String, so line 4 prints Orleans.
The Same Snippet as an Exam Question
On the exam, this snippet would be a question that asks what is printed as a result of executing the code segment. Four choices could be (A) Orl, 4, Orleans; (B) Orle, 4, Orleans; (C) Orl, 5, Orleans; and (D) Orl, 4, and then Orleans with a space in front of it. Choice A matches every line of the worked example, so it is the answer, and each of the other three choices comes from a real mistake. Choice B includes the character at `end`, and choice C counts from 1 instead of 0. Choice D forgets to add 1, so the method becomes `substring(3)` and the printed text begins with the space. Counting from 1 and including the character at `end` are the two mistakes that bench B24 catches.
Sources: Oracle, Class String, Java SE 21 API (2023); College Board, Java Quick Reference (2025); College Board, AP Computer Science A Course and Exam Description (2025); Bench Basics, B24 Inside a String (2026).
Answering a "What Is Printed?" Question
This section works through one exam-style Java question in three steps and then uses Code Coach to check the answer.
How the Exam Asks About Code
The AP Computer Science A Exam has a multiple-choice section of 42 questions, each with four choices. Several sample questions in the College Board's course description begin with the words "Consider the following code segment." The question shows a few lines of Java and then asks, "What is printed as a result of executing this code segment?", followed by four possible answers. The course description names five computational thinking practices and gives each one an approximate weighting for the multiple-choice section. Practice 3, Analyze Code, has the highest weighting of the five, so reading code is a large part of what that section asks. Part B of this course's final exam uses the same format. Figure 1 shows the worked item below laid out the way it appears on paper.
1int score = 17; 2int bonus = score % 5; 3if (score + bonus > 20) 4{ 5 System.out.println("high"); 6} 7else 8{ 9 System.out.println("low"); 10} 11System.out.println(score + bonus);
The item asks what is printed, and its four choices are (A) `high` then `19`, (B) `low` then `19`, (C) `low` then `17`, and (D) `high` then `22`. A student who reads the choices first can pick one that looks right without checking it, so the routine below starts with the code.
Tracing the Code in a Table
Step 1 is a trace table, drawn on scratch paper, with one column for each variable and one column for output, which is the layout the course description suggests for tracing code, plus a column for the line number. The student fills one row for each line that runs, in the order the lines run, and leaves out the lines that are skipped.
Line score bonus Output 1 17 2 17 2 3 17 2 9 17 2 low 11 17 2 19
Line 1 stores 17 in `score`, and line 2 stores 2 in `bonus`, because 17 divided by 5 is 3 with a remainder of 2, and the remainder operator keeps only the remainder. Storing the 2 is not output, because the 2 stays inside the program. Output is only what a print statement shows on the screen, so the Output column stays empty until a print statement runs.
Following the Condition and Checking Each Choice
Step 2 follows the path that the code takes, which depends on the condition on line 3, `score + bonus > 20`. The condition is the true-or-false expression in the parentheses after `if`. With the values in the table, the condition becomes 19 > 20, which is false. When the condition is false, the `else` block runs and the `if` block is skipped, so line 9 prints `low` and line 5 never runs. The print statements inside the braces are not the condition, because they are the code that the condition chooses between. The last line runs either way, because it sits after both blocks, so line 11 prints `19`.
Step 3 checks each choice against the table, starting with choices A and D, which print `high` and would need the condition to be true. Choice C prints 17, the value of `score` alone, which is the result of forgetting to add `bonus`. Choice D also adds 5, the number being divided by, instead of the remainder. Only choice B matches every row of the table, so B is the answer. Each wrong choice matches a real mistake, and checking all four choices can catch a mistake in the student's own table.
Code Coach as a Check After the Prediction
On the bench, Ask Code Coach opens only after a student checks an answer, so the student predicts first. A student who predicted B can then ask Code Coach what line 3 does. On October 19, 2026, that question was sent to Code Coach five times, with the code sent as numbered lines, the way the bench shows it. All five explanations were about line 3, the condition, and one of them said:
Line 3 checks if the sum of `score` and `bonus` is greater than 20. - `score` is 17 (from line 1). - `bonus` is `score % 5`, which is 17 % 5 = 2 (from line 2). So, it checks if 17 + 2 > 20, which is false.
This explanation agrees with the trace table, because a false condition sends the code to the `else` block. Code Coach's explanation is still not a test, because it is generated text and not the program's real output. Agreeing with a table-mate is not a test either, because neither student has compared the prediction with what the program actually printed. The test is running the real code, with the prediction as the expected output, and comparing the two. Real Java printed `low` and then `19`, which matches choice B and agrees with Code Coach's explanation. On the exam there is no Code Coach and no Run button, so the trace table is the test that a student can run on paper.
Summary
A "What is printed?" question is answered in three steps: trace the code in a table, follow the condition to the block that runs, and check every choice against the table. A stored value is not output until a print statement shows it. Code Coach explained line 3 correctly in all five saved runs, but only running the real code and comparing it with the prediction is a test.
Sources: College Board, AP Computer Science A Course and Exam Description (2025); Oracle, The if-then and if-then-else Statements (n.d.); Oracle, Assignment, Arithmetic, and Unary Operators (n.d.); Code Coach README and captures, mistral-small-2603 (2026); class site source code, coach.tsx (2026); course passages for Oct 26, Nov 3 and Nov 4 (2026).
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
MC 1. Consider the following code segment. What is printed as a result of executing this code segment?
1int a = 9; 2int b = a / 2; 3int c = a % 2; 4System.out.println(b);
- 4
- 4, then 1
- 1
- 4.5
MC 2. Consider the following code segment. What is printed as a result of executing this code segment?
1int t = 8; 2if (t * 2 >= 16) 3{ 4 System.out.println("yes"); 5} 6else 7{ 8 System.out.println("no"); 9} 10System.out.println(t);
- yes, then 8
- no, then 8
- yes, then no
- yes, then 16
MC 3. Consider the following code segment. What is printed as a result of executing this code segment?
1int n = 5; 2n = n + 3; 3System.out.println(n); 4n = n * 2; 5System.out.println(n);
- 8, then 16
- 5, then 10
- 16
- 8, then 10
MC 4. Consider the following code segment. What is printed as a result of executing this code segment?
1int x = 14; 2int y = x % 4; 3if (y == 2 && x > 10) 4{ 5 System.out.print("A"); 6} 7else 8{ 9 System.out.print("B"); 10} 11System.out.println(y);
- A2
- A on one line, then 2 on the next line
- B2
- A3
MC 5. Consider the following code segment. What is printed as a result of executing this code segment?
1String w = "Gumbo"; 2System.out.println(w.substring(3, 4));
- b
- m
- bo
- o
MC 6. Consider the following code segment. What is printed as a result of executing this code segment?
1String s = "streetcar"; 2System.out.println(s.substring(1, 4));
- tre
- str
- tree
- ree
MC 7. Consider the following code segment. What is printed as a result of executing this code segment?
1String park = "Audubon"; 2System.out.println(park.substring(3));
- ubon
- dubon
- Aud
- bon
MC 8. A String named `s` has at least ten characters. How many characters does `s.substring(2, 6)` return?
- 4
- 5
- 6
- 2
MC 9. Consider the following code segment. What is printed as a result of executing this code segment?
1String p = "parade"; 2System.out.println(p.indexOf("a"));
- 1
- 3
- 2
- 0
MC 10. Consider the following code segment. What is printed as a result of executing this code segment?
1String p = "parade"; 2System.out.println(p.indexOf("z"));
- -1
- 0
- 6
- Nothing; the program stops with an error
CR 1. The String `food` holds "jambalaya", a nine-letter word. Write the index under each letter, starting from 0, the way Reading 2 does for "New Orleans". Then give the result of `food.substring(0, 3)` and the result of `food.substring(5)`.
Write your answer in your lab notebook, labeled CR 1.
CR 2. In two sentences, explain why `city.substring(4, 7)` returns three characters.
Write your answer in your lab notebook, labeled CR 2.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
CR 3. Trace the code below in a table with columns for Line, `steps`, `left` and Output, and give its output. Then say which part of the condition made it true.
1int steps = 10; 2int left = steps % 3; 3if (left != 0 || steps < 5) 4{ 5 System.out.println("uneven " + left); 6} 7else 8{ 9 System.out.println("even"); 10}
Write your answer in your lab notebook, labeled CR 3.
CR 4. Write a new wrong choice for the passage's worked item, one that is not A, C or D. Then explain the mistake a student would have to make to pick it.
Write your answer in your lab notebook, labeled CR 4.
Inside a String
Open the bench, do its six jobs, then paste your credit code into the credit-code form. You can turn in up to three codes per bench; your best score is your grade.
Bench Basics opens during your class period.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
This opens during your class period.