Multiple choice technology programming languages

What is the output for the below code ? import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String... args) { Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("b"); boolean b = m.matches(); System.out.println(b); } }

  1. true

  2. compile error

  3. false

  4. b

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The regex 'a*b' matches zero or more 'a' characters followed by 'b'. The input string 'b' contains zero 'a's followed by 'b', which satisfies this pattern. The matches() method returns true when the entire input matches the pattern.

AI explanation

The pattern a*b means "zero or more 'a' characters followed by a 'b'". Since * allows zero occurrences, the pattern can match the string "b" with no leading 'a's at all, so matches() returns true.