200k views
3 votes
Consider the Java segment:

String line1 = new String(""c = 1 + 2 + 3"") ;
StringTokenizer tok = new StringTokenizer(line1, delimArg);
For the String line1 to have 4 tokens, delimArg should be:

a. String delimArg = ""+="";
b. String delimArg = ""123""
c. String delimArg = ""c+"";
d. String delimArg = "" "";"

User Aniket Raj
by
7.7k points

1 Answer

6 votes

Final answer:

To tokenize the string "c = 1 + 2 + 3" into four tokens using StringTokenizer in Java, the correct delimiter is "+=". This will yield four separate strings: "c ", " 1 ", " 2 ", and " 3".

Step-by-step explanation:

The Java segment provided involves using a StringTokenizer to break a string into tokens. The StringTokenizer class in Java is used to divide a string into multiple tokens based on the specified delimiter. For the line "c = 1 + 2 + 3" to have 4 tokens, we need to consider what characters or sequences would separate this line into four distinct parts. If the delimiter is set as "+=" (option a), this would result in the tokens "c ", " 1 ", " 2 ", and " 3". Therefore, the correct answer is a. String delimArg = "+="; which will yield exactly four tokens.

User Fylie
by
8.4k points