Topics Index

StringTokenizer and while

If you need explanation Read this topic

If you need Answer Take test on this topic

StringTokenizer and while
What will be the output of the following program?
import java.util.*;
public class DivideAndRule {
    public static void main(String[] args) {
        String team = "Pawn Elephant Pawn Knight Pawn Bishop Pawn King Pawn Minister Pawn Bishop Pawn Knight Pawn Elephant";
        StringTokenizer enemy = new StringTokenizer(team);
        while (enemy.hasMoreTokens()) {
            String initial = enemy.nextToken().substring(0, ((String) enemy.nextElement()).length() / 2);
            System.out.print(initial + " ");

A. Pawn Elephant Pawn Knight Pawn Bishop Pawn King
B. Pawn Elephant Pawn Knight Pawn Bishop Pawn King Pawn Minister Pawn Bishop Pawn Knight Pawn Elephant
C. Pa Elep Pa Kni Pa Bis Pa Ki Pa Mini Pa Bis Pa Kni Pa Elep
D. Pawn Paw Paw Pa Pawn Paw Paw Pawn
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 since we are taking the substring values and that too the length of half of it .we consider till we find no mre tokens in the String Tokenizer

Posted by Karteek Paruchuri    2013-06-25 12:36:13

Ans is D... The defaults delimiter of StringTokenizer is space and nextToken() method returns next element as String type where as nextElement() returns the next element as Object type..In this program we are printing few characters of String tokens (which are half the next token characters) at odd positions.

Posted by Krishna Sai    2013-06-25 12:49:31

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

Posted by Merit Campus    2013-06-25 16:30:39

got the RC thankyou

Posted by Karteek Paruchuri    2013-06-29 06:04:43

© meritcampus 2019

All Rights Reserved.

Open In App