Topics Index

While and StringTokenizer

If you need explanation Read this topic

If you need Answer Take test on this topic

While and StringTokenizer
What will be the output of the following program?
import java.util.*;
public class StringTokeninzerImplementer {
    public static void main(String[] args) {
        String mathExpr = "4*2+2/4";
        StringTokenizer st1 = new StringTokenizer(mathExpr, "*+/-", true);
        while (st1.hasMoreTokens()) {
            System.out.print(st1.nextToken() + " ");
        String str = "fields1,fields2,fields3,and fields4";
        StringTokenizer st2 = new StringTokenizer(str, ", ", false);
        while (st2.hasMoreTokens()) {
            System.out.print(st2.nextToken() + " ");

A. 4 2 2 4 
fields1 fields2 fields3 and fields4
B. 4 * 2 + 2 / 4 
fields1 fields2 fields3 and fields4
C. 4 2 2 4
D. 4 * 2 + 2 / 4 
fields1 , fields2 , fields3 , and   fields4
E. Some other output
F. Compilation Error or Runtime Error
Topic: StringTokenizer In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

d is the answer the *,+</_ in st1 are inserting the respective palces according to the tokens positions matching the field numbers

Posted by Karteek Paruchuri    2013-07-02 12:43:01

ans is B bcoz the string tokenizer seperates the given string into tokens using the seperatyors given

Posted by Pramod Jain    2013-07-02 12:46:36

ans is b...since the constructor of String TOkenizer separates acording to the specified string..since the boolean value is true even the delimiters are added as tokens...whereas in st2,boolean value is false so the delimiters are not taken as tokens so fields1 fields2 fields3 and fields4 is printed

Posted by Santosh Munugota    2013-07-02 12:49:45

ans is B bcoz stringtokenizer divides the string into tokens and when the false is passed in the constructor the delimiter is not considered as token and other values gets printed..

Posted by Shaik Nyamath    2013-07-02 12:52:19

Congratulations Santosh Munugota. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-03 03:19:12

got rc! :)

Posted by Santosh Munugota    2013-07-05 14:13:40

© meritcampus 2019

All Rights Reserved.

Open In App