182k views
1 vote
Consider the Java segment:

String line1 = new String(""c = 1 + 2 + 3"") ;
StringTokenizer tok = new StringTokenizer(line1);
int count = tok.countTokens();
The value of count is ________.

a. 8.
b. 7.
c. 13.
d. 4.

1 Answer

0 votes

Final answer:

The Java code snippet provided uses StringTokenizer to break the string into tokens using whitespace as the default delimiter. The countTokens() method returns the number of tokens in the string "c = 1 + 2 + 3", which totals 8. Hence, the value of count is 8.

Step-by-step explanation:

The given Java code snippet creates a StringTokenizer object with the string "c = 1 + 2 + 3". The StringTokenizer class is used to break a string into tokens. By default, this class takes whitespace as the delimiter. The countTokens() method is used to get the number of tokens available in the string. In the provided string, there are five words and three operators, separated by spaces, which amount to eight tokens in total.

Therefore, the value of count after executing int count = tok.countTokens(); is 8.

The correct answer is: a. 8.

User Paula Fleck
by
8.1k points