?

AP® Computer Science Principles

Free Version

Upgrade subject to access all content

Moderate

Substring, Join, and Length Methods

APCSP-1LXU3U

Assume that the SUBSTRING(a, b) command can be used to produce a substring of a String where a is the index of the first letter in the substring and b is the index of the last letter in the substring. Also assume that the index of the first letter in a string is 1. Here’s an example of how SUBSTRING can be used:

q ← “My good opinion once lost is lost forever.”
q ← q SUBSTRING (4, 7)

After the above code fragment, q would be set to “good”.

Also assume that the JOIN(a, b) command can be used to join several Strings together. Here’s an example:

x ← “Mr.”
y ← “Darcy”
z ← JOIN(x, y)

After the above code fragment, z would be set to “Mr.Darcy”. Note that JOIN can take any number of parameters.

Also assume that the LENGTH(a) method returns the length of a String. For example,:

x ← “Elizabeth”
y ← LENGTH(x)

After the above code fragment, y would be set to 9 because there are nine letters in “Elizabeth”.

Assume that q is set to “My good opinion once lost is lost forever.”.

Which of the following would set z to an even number?

A
LENGTH( q SUBSTRING(1, 3)) + LENGTH( q SUBSTRING(5, 8))
B
LENGTH( JOIN( q SUBSTRING(1, 12), q SUBSTRING(1, 7))) MOD 2
C
LENGTH( JOIN( q SUBSTRING(2, 10), q SUBSTRING(2, 9)))
D
LENGTH( JOIN( q SUBSTRING(3, 10), q SUBSTRING(1, 2)))