Knowee
Questions
Features
Study Tools

Predict the output of the below code :import java.util.regex.*;  public class TestRegEx{        public static void main(String args[])  {               Pattern p = Pattern.compile(".ech");             Matcher m = p.matcher("tech");               boolean b = m.matches();               System.out.println(b);      }}Select one:a.Runtime Errorb.truec.falsed.compile time error

Question

Predict the output of the below code :import java.util.regex.*;  public class TestRegEx{        public static void main(String args[])  {               Pattern p = Pattern.compile(".ech");             Matcher m = p.matcher("tech");               boolean b = m.matches();               System.out.println(b);      }}Select one:a.Runtime Errorb.truec.falsed.compile time error

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given Java code will be "true".

Here's the step by step explanation:

  1. The Pattern.compile(".ech") method compiles the given regular expression into a pattern. In this case, the regular expression is ".ech". The dot (.) in regular expression is a metacharacter that matches any character except a line terminator unless the DOTALL flag is specified.

  2. The matcher("tech") method creates a matcher that will match the given input against the pattern. In this case, the input string is "tech".

  3. The matches() method attempts to match the entire region against the pattern. If the match succeeds, it returns true.

  4. The System.out.println(b) prints the result of the match. In this case, it will print "true" because "tech" matches the pattern ".ech".

So, the correct answer is (b) true.

This problem has been solved

Similar Questions

Java Regex Finder Example

Find the output of the below Java program?public class Test {  public static void main(String[] args) {     Test t1 = new Test();     Test t2 = t1;     System.out.println(t1.equals(t2));  }}A) trueB) falseC) nullD) error

Predict the output of the code snippet given alongside.public class Test{public static void main(String[] args){Teacher t = new Student();System.out.println(t.name);}}class Teacher{String name="Sam";}class Student{String name="Kevin";} SamKevinCompilation errorRun-time error

rrect answerWhat will be the output of the following Java code? class Name {     public static void main(String args[]) {          try {             int ct1 = 0;             int ct2 = 5;             int ct3 = ct2 / ct1;             System.out.print("Hello");         }         catch(Exception e) {             System.out.print("CodeTantra");         }      } }

Predict the output of following Java Programfinal class Complex {     private final double re;    private final double im;     public Complex(double re, double im) {        this.re = re;        this.im = im;    }     public String toString() {        return "(" + re + " + " + im + "i)";    }} class Main {  public static void main(String args[])  {       Complex c = new Complex(5, 10);       System.out.println("Complex number is " + c);  }         }OptionsCompiler ErrorComplex number is (5.0 + 10.0i)Complex number is SOME_GARBAGEComplex number is Complex@8e2fb5

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.