Ask Question
6 October, 23:11

For the following code, how many times would the for loop execute? String str = ("Ben and Jerry's ice cream is great."); String[] tokens = str. split (" "); for (String s : tokens) System. out. println (s);

A. 1

B. 3

C. 5

D. 7

+3
Answers (1)
  1. 7 October, 02:57
    0
    D. 7

    Explanation:

    Given code:

    String str = ("Ben and Jerry's ice cream is great.");

    String[] tokens = str. split (" ");

    for (String s : tokens) System. out. println (s);

    Splitting the given sentence on space character yields 7 tokens, namely:

    Ben, and, Jerry's, ice, cream, is, great.

    So the for loop will execute 7 times, once for each token because the loop variable s is assigned an individual token on each iteration.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “For the following code, how many times would the for loop execute? String str = ("Ben and Jerry's ice cream is great."); String[] tokens = ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers