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.compile time errorb.falsec.Runtime Errord.trueClear my choice
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.compile time errorb.falsec.Runtime Errord.trueClear my choice
Solution
The output of the given Java code will be "true".
Here's the step by step explanation:
-
The
Pattern.compile(".ech")method compiles the given regular expression into a pattern. The "." in the regular expression is a special character that matches any character. -
The
matcher("tech")method creates a matcher that will match the given input against the pattern. In this case, it's trying to match the string "tech" with the pattern. -
The
matches()method attempts to match the entire region against the pattern. If the match succeeds, it returns true. -
The
System.out.println(b)prints the result of the match. In this case, it will print "true" because "tech" matches the pattern ".ech" (where "." can be any character).
So, the correct answer is d. true.
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
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 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
Predict the output of following Java programclass Test extends Exception {}class Main { public static void main(String args[]) { try { throw new Test(); } catch (Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } }}OptionsGot the Test ExceptionGot the Test Exception Inside finally blockInside finally blockCompiler Error
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.