Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class Replace {

    public static void main(String a[]) {
        String replaceStr = "<B>Good Day<\\B>";
        System.out.println("Before replace: " + replaceStr);
        replaceStr = replaceStr.replaceAll("[a-zA-Z<> \\]", "");
        System.out.println("After replace: " + replaceStr);

A. Before replace: <B>Good Day<\B>
After replace: Good Day
B. Before replace: <B>Good Day<\B>
After replace:
C. Before replace: <B>Good Day<\B>
After replace: BGood Day\B
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.

ans is B

Posted by Raju VarshnEy    2014-09-10 08:35:29

Answer is E) Runtime error as syntax of regex is incorrect should be \\\\ inmside regex

Posted by Shashanka Mogaliraju    2014-09-10 08:37:25

e..error: method replaceAll in class String cannot be applied to given type.

actual n formal argmnt dfr

publicString replaceAll(String regex,String replacement)

regex-- This is the regular expression to which this string is to be matched

.replacement-- This is the string to be substituted for each match.

Posted by Wajahat Khan    2014-09-10 08:42:20


java.util.regex.PatternSyntaxException: Unclosed character class near index 10[a-zA-Z<>\]

Posted by Wajahat Khan    2014-09-10 08:48:06

ans b.
\\ s escape sequence so it print single \…
replace all method replace all char with empty char.
so 2nd sop prints nothing

Posted by Maheshwari Natarajan    2014-09-10 09:29:26

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-11 04:31:19

© meritcampus 2019

All Rights Reserved.

Open In App