Menu
Topics Index
...
`

Collections2244


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections2244
What will be the output of the following program?
import java.util.*;
public class HappyNewYear {
    public static void main(String args[]) {
        List<String> words = new ArrayList<String>();
        words.add("Happy");
        words.add("New");
        words.add("Year");
        int count = 0;
        for (String word : words) {
            System.out.print(word + " ");
            switch (word) {
                case "Year" :
                    System.out.print(words.indexOf(word) + "0");
                    break;
                default :
                    count++;
            }
            count += word.length();
        }
        System.out.print(count);
    }
}

A. Happy New Year 2012
B. Happy New Year 2016
C. HappyNewYear2014
D. Happy New Year 2014
E. Compilation Error or Runtime Error
Topic: Collection Framework 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.

Ans:D. The words Happy New year are printed and when word becomes year the index of year i.e., 2 and 0 are printed. The count variable has the values 6,10 and 14. Hence the value of count 14 is printed after the loop terminates

Posted by Priyanka Madiraju    2014-01-01 13:11:37


Ans is: D , it prints Happy New Year 2014. First 3 words are printed generally, in this process "count" becomes 14. after 3 words it prints 2014. in this 2014,  2 is indicates index of "year" of list then add "0" at last prints "14".. becomes 2014.

Posted by Mallikarjuna Rao    2014-01-01 13:29:56


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

Posted by Merit Campus    2014-01-02 04:14:34


Thank you Merit Campus :) got the recharge!

Posted by Priyanka Madiraju    2014-01-06 09:10:11


© meritcampus 2019

All Rights Reserved.

Open In App