Menu
Topics Index
...
`

Strings1974


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings1974
What will be the output of the following program?
import java.util.regex.*;
public class Match {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("(aa|a|aab?)+");
        int count = 0;
        for (String s = ""; s.length() < 200; s += "a")
            if (p.matcher(s).matches())
                count++;
        System.out.println(count);
    }
}

A. 99
B. 100
C. 199
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Regular Expression

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.

C is the answer.because string is matched for length<200 .

Posted by Asad Ahmed    2014-05-16 04:26:18


ans d.0 bcz each tim  matcher checks match betwn "a " and pattrn sequenc ..

Posted by Maheshwari Natarajan    2014-05-16 04:35:16


Answer : C = 199
> we have a R.E which  checks for a String aa|a|aab matches 0 or 1 occureences of these expressions
> we are always appending the String with "a" and comparing it < 200 times and checking it with the PATTERN ( R.E )

Posted by Deepak Kumar Yadav    2014-05-16 05:33:43


No winner for this dose. Correct option is 'C' :(. Deepak Kumar Yadav was already won in this week.

Posted by Merit Campus    2014-05-19 05:59:49


© meritcampus 2019

All Rights Reserved.

Open In App