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?
import java.util.regex.*;

public class Matches {
    public static void main(String args[]) {
    public static void reusePatternMethodExample() {
        Pattern pat = Pattern.compile("\\d");
        Matcher m = pat.matcher("5");
        boolean yes = m.matches();
        System.out.println("original pattern matches " + yes);
        String temp = pat.pattern();
        Pattern pat2 = Pattern.compile(temp);
        m = pat.matcher("5");
        yes = m.matches();
        System.out.println("second pattern matches " + yes);

A. true
B. true
C. false
D. false
E. Some other output
F. 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 : E,we will get
Original pattern matches true
Second pattern matches true
Becoz in both the cases we are trying to match the pattern for a Number (0-9) and checking this pattern via pat.matcher("5") which is being supplied as "5"

Posted by Deepak Kumar Yadav    2014-05-30 12:56:25

ans e. originl pattern matches true
second pattern matches true
pattrn   checkd matcher if macher contain integer value thn matches method returns true.

Posted by Maheshwari Natarajan    2014-05-30 13:10:48

Pattern.compile(..) returns a compiled representation of regular repression
which matches digits b/w 0-9
pat.matcher(..) creates a matcher object so as to interpret the expression
later m.matches() matches with the text
pattern() method gives the actual form of the compiled version
here in this case results in true in both the cases..
option E

Posted by Kittu Manoj    2014-05-30 13:15:35

Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-02 06:56:49

© meritcampus 2019

All Rights Reserved.

Open In App