Menu
Topics Index
...
`

Happy Birthday


If you need explanation Read this topic

If you need Answer Take test on this topic

Happy Birthday
What will be the output of the following program?
import java.util.*;
public class HappyBirthday {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Set<String> set = new TreeSet<String>();
        Map<String, String> map = new TreeMap<String, String>();
        String s = "HappyBirthday";
        int length = s.length();
        for (int i = 0; i < length; i++) {
            String c = s.charAt(i) + "";
            String o = s.charAt(length - i) + "";
            list.add(c);
            set.add(c);
            map.put(c, o);
        }
        System.out.println(list + "\n" + set + "\n" + map);
    }
}

A. [H, a, p, p, y, B, i, r, t, h, d, a, y]
[d, t, r, B, p, a, H, h, y, i]
{B=r, H=y, a=a, d=p, h=p, i=i, p=h, r=B, t=y, y=H}
B. [y, a, d, h, t, r, i, B, y, p, p, a, H]
[d, t, r, B, p, a, H, h, y, i]
{d=p, t=y, r=B, B=r, p=h, a=a, H=y, h=p, y=H, i=i}
C. [H, a, p, p, y, B, i, r, t, h, d, a, y]
[B, H, a, d, h, i, p, r, t, y]
{B=r, H=y, a=a, d=p, h=p, i=i, p=h, r=B, t=y, y=H}
D. Some other output
E. Compilation Error or Runtime Error
Topic: TreeMap In Java - java.util.TreeMap

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.

E.runtime error bcz StringIndexOutOfBounds Exception happen to ariase.so E is the asn.

Posted by Shabaaz Shaikz    2014-04-16 12:56:40


Ans is E. Runtime error i.e., StringIndexOutOfBounds Exception occurs since for the first iteration we are referring to an index value of 13 ( length - i)(length of string = 13 and for first iteration i = 0) so index 13 does not exists ( 0 to 12).

Posted by Nikhil Sulibhavi    2014-04-16 15:51:37


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

Posted by Merit Campus    2014-04-17 04:22:42


THANQ MC

Posted by Shabaaz Shaikz    2014-04-17 12:00:22


© meritcampus 2019

All Rights Reserved.

Open In App