About Question enthuware.ocpjp.v7.2.1737 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

About Question enthuware.ocpjp.v7.2.1737 :

Post by Mark7777 »

Lost me on this one and the API doesn't help much. After the first reset, and then it prints B a second time, how do you arrive at a third B? Don't you need an immediate reset to get back to right before B again which will be called a third time? Doesn't reset act like an endless loop where you keep getting sent back to the first mark? How come D and E don't get printed? I obviously don't understand the basics, and none of this was covered in my over-priced books. Maybe I'm thinking too much in terms of transactions and savepoints and rollbacks. Oh, and what exactly does it mean, according to the API, to "reset the stream?" Does that just mean move the pointer back to the mark? How do you jump up to the second call to reset without printing D and E. Or is this one of those questions: "What MAY be included when printed out?" (Not asking all that may be printed out.)

Dazed and confused.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1737 :

Post by admin »

Not sure what you find confusing here. reset takes you to the place where you set the mark. So,

Code: Select all

BufferedReader in = (BufferedReader) r; 
System.out.print(in.readLine());  //prints A

in.mark(100); //set the mark here, right after A i.e. before B

System.out.print(in.readLine());//prints B  <============== First B
System.out.print(in.readLine()); //prints C

in.reset();  // Going back to the mark i.e. right after A 
System.out.print(in.readLine()); //prints B  <============== Second B 

in.reset();  // Going back to the mark i.e. right after A 
System.out.println(in.readLine());//prints B  <============== Third B
You can call reset anytime and any number of times after setting the mark. As soon as you call it, the file cursor will simply go back to that same spot where you set the mark. D E is not printed because the code never tried to read those lines.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1737 :

Post by Mark7777 »

But why doesn't the code try to read the lines that print D and E? Doesn't the logic flow down to the bottom?

jacks446
Posts: 1
Joined: Wed Dec 23, 2020 6:48 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1737 :

Post by jacks446 »

Hello,

Hoping to get some clarification in my understanding regarding the use of markSupported(). In all of the reference documentation I've read it seems to only say to check if markSupported() == true before continuing, which is exactly what the code in this problem does. But in the explanation to the problem it says:

BufferedReader does provide this facility, therefore r.markSupported() returns true.

Specifically related to which types support or do not support markSupported(), I haven't seen a definitive statement like this in reference material, although it's possible I missed it. Which general types should I keep a look out for when seeing markSupported() to know it always returns true?

Thank you,

J

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1737 :

Post by admin »

You have to see the API documentation to know which Reader supports marks. All BufferedReaders support mark.
https://docs.oracle.com/javase/8/docs/a ... upported--
Tells whether this stream supports the mark() operation, which it does.
It makes sense because it buffers the data and so, it can go back. Some streams (such as network streams) simply fetch the data from the network and unless they buffer the data, they can't go back to read the data which has already been read.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 51 guests