Java OCA OCP Practice Question 129

Question

Given the following code:

1. String scanMe = "aeiou9876543210AEIOU"; 
2. Scanner scanner = new Scanner(scanMe); 
3. String delim = ?????; // WHAT GOES HERE? 
4. scanner.useDelimiter(delim); 
5. while (scanner.hasNext()) 
6.     System.out.println(scanner.next()); 

What code at line 3 produces the following output?

aeiou 
AEIOU 
  • A. String delim = "d+";
  • B. String delim = "\d+";
  • C. String delim = "\\d+";
  • D. String delim = "d*";
  • E. String delim = "\d*";
  • F. String delim = "\\d*";


C.

Note

The goal is to create a regular expression that matches the run of digits in the middle of scanMe.

The "d" in a regular expression indicates "digit."

Two escaping backslashes are necessary: one is consumed by the Java compiler when it compiles the literal string.

The second tells the useDelimiter() method that the "d" is a special character.

The "+" quantifier matches one or more occurrences, which is what we want.

The "*" quantifier matches zero or more occurrences; there are zero digits between the vowels, and that is considered a "*" match, so using "\\d*" splits the string into "a", "e", "i", etc.




PreviousNext

Related