Only $35.99/year

AP Computer Science

Terms in this set (86)

A store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7.

double len1;
double len2;
double len3;
double total = len1 + len2 + len3;
int minLength = (int) (total + 0.5);
System.out.print(minLength);

Which of the following best describes the behavior of the code segment?

a. The code segment works as intended for all nonnegative values of len1, len2, and len3.
b. The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.
c. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength.
d. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by declaring minLength as a double and casting it to an int in the System.out.print statement.
e. The code segment does not work as intended for any values of len1, len2, and len3. It returns the sum of 0.5 and the three lengths for all values of len1, len2, and len3.
Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea".

I.
if (str.indexOf("pea") >= 0)
{
System.out.println("pea");
}
else if (str.indexOf("pear") >= 0)
{
System.out.println("pear");
}
else if (str.indexOf("pearl") >= 0)
{
System.out.println("pearl");
}

II.
if (str.indexOf("pearl") >= 0)
{
System.out.println("pearl");
}
else if (str.indexOf("pear") >= 0)
{
System.out.println("pear");
}
else if (str.indexOf("pea") >= 0)
{
System.out.println("pea");
}

Which of the following best describes the output produced by code segment I and code segment II?

a. Both code segment I and code segment II produce correct output for all values of str.
b. Neither code segment I nor code segment II produce correct output for all values of str.
c. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl".
d. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl".
e. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".