Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
Which meta character matches any single character except the newline character?
A
*
B
.
C
?
D
+
Question 2
What does the method Pattern.compile(String regex) do?
A
It compiles the given regular expression into a Pattern object.
B
It splits a string based on the given regex.
C
It creates a Matcher object directly from the regex.
D
It checks if the regex is valid and returns a boolean.
Consider the following code snippet:
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test {
    public static void main(String[] args) {
        String text = "Hello World!";
        Pattern pattern = Pattern.compile("World");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            System.out.println("Found at index: " + matcher.start());
        } else {
            System.out.println("Not found");
        }
    }
}
What will be printed?
A
Found at index: 0
B
Found at index: 6
C
Not found
D
Found at index: 11

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next