AP® Computer Science A Practice Questions
Work through AP Computer Science A practice questions unit by unit, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 512
Question 513
Question 514
String s = "programming";
System.out.println(s.substring(3, 7) + "-" + s.indexOf("m"));
What is printed when the code segment executes?Main Solution
Answer
Answer: D. Indices: p=0, r=1, o=2, g=3, r=4, a=5, m=6, m=7, i=8, n=9, g=10. substring(3, 7) = "gram". indexOf("m") returns the first occurrence: index 6 (not 7).
Answer: Correct: D
Answer images
Question 515
Question 516
Question 517
Question 518
Question 519
Question 520
int x = 3;
int y = 4;
System.out.println("score: " + x + y);
System.out.println("score: " + (x + y));
What is printed when the code segment executes?Main Solution
Answer
Answer: B. The + operator is left-associative and evaluates left to right. In the first line, "score: " + 3 is a String, so the next + 4 is string concatenation, producing "score: 34". In the second line, parentheses force the integer addition 3 + 4 = 7 to happen first, so the concatenated output is "score: 7".
Answer: Correct: B
A
score: 7
score: 7B
score: 34
score: 7C
score: 7
score: 34D
score: 34
score: 34Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

