Menu
Topics Index
...
`

Strings and foreach loop


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings and foreach loop
What will be the output of the following program?
public class Created {
    static String lines = "Hai now you are answering a dose which is created.\r\nBy MeritCampus.";
    public static void main(String[] args) {
        String sp[] = lines.split(" ");
        lines = "";
        for (String element : sp) {
            if (!element.equalsIgnoreCase("a")) {
                element.replace("a", "");
                System.out.print(element + " ");
                lines += " " + element;
            }
        }
        System.out.println("\n" + sysout());
    }
    static String str = lines;
    private static String sysout() {
        return toString(str);
    }
    public static String toString(String str) {
        return str;
    }
}

A. Hi now you re nswering dose which is creted.
By MeritCmpus. 
Hai now you are answering a dose which is created.
By MeritCampus.
B. Hai now you are answering dose which is created.
By MeritCampus. 
Hai now you are answering a dose which is created.
By MeritCampus.
C. Hai now you are answering a dose which is created.
By MeritCampus.
Hai now you are answering a dose which is created.
By MeritCampus.
D. Some other output
E. Compilation Error or Runtime Error
Topic: Additional String Methods 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 B since we are checking for word which contains only 'a' and replacing with empty character.in the second sys.out.println we are trying to print str which stores the unmodified value of linew=s since static vars are initialised at runtime

Posted by Shashanka Mogaliraju    2014-09-03 12:35:55


ans is B,,, iby spliting it contains "a" as a word,, and by if loop it remove it,,

Posted by Raju VarshnEy    2014-09-03 12:40:01


B is ans.as "a" is replace with " " in first print..

Posted by Shabaaz Shaikz    2014-09-03 12:40:57


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

Posted by Merit Campus    2014-09-04 04:12:07


© meritcampus 2019

All Rights Reserved.

Open In App