161k views
1 vote
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");}

1. Which of the following best describes the output produced by code segment I and code segment II?O Both code segment I and code segment II produce correct output for all values of str.O Neither code segment I nor code segment II produce correct output for all values of str.O 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".O 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".O 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".

User Ggrr
by
5.1k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

What is printed as a result of executing this code segment?

true

If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing?

AD

Which of the following correctly compares the outputs of the two code segments?

Code segment I and code segment II produce the same output for all values of x and y.

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

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".

User Mujo Osmanovic
by
5.0k points