Menu
Topics Index
...
`

For loop999


If you need explanation Read this topic

If you need Answer Take test on this topic

For loop999
What will be the output of the following program?
class Tokens
{
    public static void main(String[] args)
    {
        StringTokenizer st = new StringTokenizer("A~B#C~#D##E#~F~#G#H~ ~", "~#");
        while (st.hasMoreTokens())
        {
            if (st.nextToken().startsWith("A") || st.nextToken().startsWith("D"))
            {
                System.out.print(st.nextToken() + " ");
            }
        }
    }
}

A. A~B#C D##E#~F G#H~ ~ 
B. B E 
C. A~B#C D##E#~F 
D. A D 
E. None of the above or it throws a run-time 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.

answer is c... as ~# is the delimitter...only A~B#C and D##E#~F are the two tokens which satisfy the given condition in if clause..thus the result would be C...

Posted by Sneha Rapool    2014-06-16 13:33:44


option a is the answer..as #~ are the delimiters

Posted by Vijay Kumar    2014-06-16 13:53:05


Ans is B,
Here there 3 things to consider:
1: "~#" is not one delimeter but two delimeters ~ or #
2: Each occurence of st.nextToken() will give next token(if available)
3: In if statement, if first condition is true, next statements are skipped
Since,
A satisfies 1st condition, next token B is printed.
Later, D satisfies 2nd condition, next token E is printed.
Hence solution is B E
Finally I am using postpaid. Therefore I dont need any recharge. :P

Posted by Vallab Srinath    2014-06-16 17:09:31


answer is c  ..the condition is satsified........

Posted by Vijaya Vijaya    2014-06-17 05:30:39


Congratulations Vallab Srinath. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-18 04:04:09


© meritcampus 2019

All Rights Reserved.

Open In App