AP® Computer Science A Using Objects and Methods Practice Questions
Work through AP Computer Science A practice questions on Using Objects and Methods, 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.
Subtopic: Using Objects Clear filter
Question 372
Question 373
Question 374
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
Question 375
Question 376
Question 377
String a = "java";
String b = "java";
String c = new String("java");
System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
What is printed when the code segment executes?Main Solution
Answer
Answer: C. Literal String values are interned: a and b refer to the same pooled object, so a == b is true. new String("java") creates a new String object in a different memory location; a == c compares references and is false. equals compares character contents, so a.equals(c) is true.
Answer: Correct: C
A
true true true
B
false false true
C
true false true
D
true true false
Answer images
Question 378
Question 379
String s = "alphabet";
int x = s.length() - s.indexOf("bet");
System.out.println(x);
What is printed when the code segment executes?Main Solution
Answer
Answer: A. "alphabet".length() is 8. The substring "bet" begins at index 5 (a-l-p-h-a-b places b at index 5). Thus x = 8 - 5 = 3 — which is exactly the length of "bet".
Answer: Correct: A
A
3
B
5
C
8
D
13
Answer 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.

