Regular Expressions Review Questions

Answer the following multiple-choice questions:

12.5 points
What does the regular expression sequence \W match?


12.5 points
What is the output for the code below?

import java.util.regex.*;

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);

		

	}
}


12.5 points
What is the output for the code below?

public class Test {
	
	public static void main(String... args) {
            
		Pattern p = Pattern.compile("a{3}b?c*");
		Matcher m = p.matcher("aaab");
		boolean b = m.matches();
		System.out.println(b);

		

	}
}



12.5 points
Which of the following strings is not matched by the regular expression (12|3*4)*?


12.5 points
Which of the following Strings would be matched by the regular expression
"[a-z_A-Z][a-z_A-Z0-9]+"?


12.5 points
Which of the following methods returns a boolean?


12.5 points
You are making a calendar and decide to write a segment of code that returns true if the string month is April, May, June, or July. Which code segment correctly implements use of regular expressions to complete this task? 


12.5 points
What is the output of the code below?

import java.util.regex.*;

public class MatcherFindStartEndExample {

    public static void main(String[] args) {

        String text = "This is it"

        String patternString = "is";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(text);

        int count = 0;
        while(matcher.find()) {
            count++;
            System.out.println("found: " + count + " : "
                    + matcher.start() + " - " + matcher.end());
        }
    }
}