Menu
Topics Index
...
`

Pattern and While


If you need explanation Read this topic

If you need Answer Take test on this topic

Pattern and While
What will be the output of the following program?
import java.util.regex.*;
public class Function {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("pqp");
        Matcher m = p.matcher("pqpqppqpqpppqqpqppqpqppppqp");
        boolean b = false;
        while (b = m.find()) {
            System.out.print(m.start() + " ");
        }
    }
}

A. 0 3 6 9 16 19 22 27
B. 0 3 6 15 18 25
C. 0 5 14 17 24
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.

Answer c.
find() is usd to get stating loc of "pqp" pattrn frm the givn string matchr m. m.strt() prints strtng indx value
0 5 14 17 24

Posted by NagaRaj Nataraj    2014-07-02 08:44:32


C

Posted by Maheshwari Natarajan    2014-07-02 08:46:54


Ans is E...because condition requires a comparison operator in while loop which generates an error

Posted by Badam Swathi    2014-07-02 08:48:28


ans is c  find() is check wether same pttrn is prsnt or not in matcher m instance if not it trminate while otherwise strt() prnt the index

Posted by Dinesh Saini    2014-07-02 10:58:04


E

Posted by Vijay Raichura    2014-07-02 12:58:00


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

Posted by Merit Campus    2014-07-03 04:10:35


© meritcampus 2019

All Rights Reserved.

Open In App