8

I am studying for the Java OCP certificate. I am taking mock exams to prepare.

Example program:

public class Quetico {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        while (m.find()) {
            System.out.println(m.start() + " ");
        }
        System.out.println("");
    }
}

the authors of the OCA/OCP Jave SE 7 Study Guide maintain that the execution:

java Quetico "\B" "^23 *$76 bc"

will produce the output

0 2 4 8

However, when I run the code from Eclipse or test it on an outside source, I get

0 2 4 5 7 10

Am I missing something here, or is it a mistake by the authors of the study guide?

I am adding the actual question from the book below for reference.

Question from the Book

OCP Java 7 Self Test Question 8.3

Answer

enter image description here

sdc
  • 2,603
  • 1
  • 27
  • 40
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
  • ya, 2 one is the correct one. Does `"\B"` would be passed as `\\B` ? – Avinash Raj Jul 25 '15 at 18:31
  • @AvinashRaj Command-line input does not have to be escaped, so `"\B"` is equal to a Java String `"\\B"` passed in directly in the code. – Stefan van den Akker Jul 25 '15 at 18:34
  • `mistake by the authors of the study guide` If you run the code and get the same result, it must be considered the claim is in error. Regex engine primitives don't change, in fact `\B` means not-word boundary on probably every engine. So, is it possible the claim is in error ? I'd say so.. –  Jul 25 '15 at 18:56
  • @sln Thanks. These kind of exams have a lot of trick questions, so you start to doubt everything you see. My only hope is they have thoroughly debugged the actual exam questions. – Stefan van den Akker Jul 25 '15 at 18:59
  • I have an exam for your exam makers. They should send all their questions to me to check their work. –  Jul 25 '15 at 19:15

1 Answers1

6

The book is correct (when executing over a Unix machine with the usual shells). It is a combination of shell behaviour and java (in my opinion, off-topic to a course of Java). Remember "$" in shell means replacement. So, if you call the program as:

java Quetico "\B" "^23 *$76 bc"

the string that is matched over regex is (you can add a println for args[1] to verify it):

^23 *6 bc

with the result given by the book "0 2 4 8".

You can compare the result with the one of:

java Quetico "\B" '^23 *$76 bc'

that disables shell substitution.

pasaba por aqui
  • 3,446
  • 16
  • 40
  • You are completely right! Why would they put something like that in the exam? It's trick questions like these that I hate, because you're not really testing someone's knowledge of Java. Thanks for figuring this one out! – Stefan van den Akker Jul 25 '15 at 19:59
  • @Neftas: my hypothesis is authors have included the "$" in the question, executed the program and copied the answer to the book, without see the "special" meaning of this symbol. Well, at least, they have tested the examples, a lot of books doesn't ;-) – pasaba por aqui Jul 25 '15 at 20:16
  • Yes, and I was testing the code using eclim (which uses a headless Eclipse instance), so I bypassed the shell while passing arguments to the program... It all figures, mystery solved :) – Stefan van den Akker Jul 25 '15 at 20:22