Question 291
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

