Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!

Elaine Byrne

Ranch Hand
+ Follow
since Jul 07, 2017
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
8
Received in last 30 days
1
Total given
59
Given in last 30 days
7
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Elaine Byrne

That's interesting about the online option, Jeanne - though, as you describe in your blog post, it's very restrictive.

Jeanne Boyarsky wrote:... Do not choose this option as it does not permit a bathroom break. ...


Unless you take the exam on a toilet    

Hello Peter, I've done 3 Java exams at Pearson Vue centres (in a European counrty), and while I don't think I went out of the room, I was told I would be able to by pressing a button or walking out and letting them know. I don't think the clock would be stopped, though (not sure), so one could lose a few minutes. As for the photo, (and I always detest mine!), I assume they are just taken to help verify identity, especially if you come back for more exams in the future. As far as I know, display of the certification to third parties, including (prospective) employers, does not include your grade or photo - I can access a page with my own full results and photo with a password, but don't think that's accessible to any one else (outside Oracle /Pearson Vue). I assume the marking is automated, as it's just multiple choice; of course, I can't rule out that an Oracle/PV employee could decide to mess with such data, but I assume that would be illicit, as would release of your data or image (and if I found my image in a commercial I would sue them to high hell!). You will be signing a form, so can view the conditions there, though to be honest I probably did no more than scan mine. They didn't do a palm scan at the centre I went to.
Q1: Java 8 Stream methods (OCP)

Which statement is true of the following code snippet (choose one):

(A) A list of the numbers is always printed
(B) An exception is thrown
(C) It does not compile
(D) None of the above

I don’t see an option for spoiler tags, so I’m writing the answer/comment in white text three lines below to make it less easily viewable 'by accident', i.e. without mouseover selection /copy-paste; if this doesn’t work well enough, I’ll rearrange.



Answer: D. Assuming the stream is processed in parallel (split between two or more threads), as it usually will be on multi-core systems, I assume, a nested list of lists will be generated. E.g. I see   [1, [2], [3, [4, [5]]]]   on my system. This is because the List method add(...) was used as third arg to collect(...) instead of addAll(...). Of course, this is unlikely to ever be done in a real program, where one would usually want to generate a simple list of the Integers by allowing parallel-processed lists to be combined to [1, 2, 3, 4, 5] by dumping their contents together using addAll(...), the stream is parallel, the order of the Integers could be different, though I always happen to get them in order. And of course, if a properly generically-typed list reference is used, i.e. List<Integer>, the statement does not even compile, as the combiner cannot add List<Integer> to List<Integer>; by contrast, the combiner can add one ‘raw’ List (of Objects) to another, as the added List simply goes in as an Object, generating a nested Lists of Lists.
While studying for OCP 8 certification (upgrade from OCP 7), I was exploring some “what if’s” and came across something that I haven’t seen among the mock exam questions I took and that might be of mild amusement to some (though veterans will probably just think “Well duh, that’s obvious!”). Since I’m unlikely to ever come up with more than a few questions I can offer the world, and the one below would probably not be used in a real exam anyway, I’m starting this thread for myself and anyone else who wants to deposit similar trivia. I couldn’t see a similar thread or related section in which to put this, but mods please feel free to move it if there is, or e.g. to the main Java section if more appropriate. Also, corrections of any misinterpretations I may post are welcome  

Update: After I posted, the system alerted me to a series of posts from 12 years ago with a similar tack, started by Valentin Crettaz, which can be found by searching for "SCJP Brainteaser"
Ah, thanks Ron, I didn't think to look at the documentation for the implemented interfaces (I think inherited methods are sometimes listed in these docs, and I had assumed it woud be the case here too)
I saw this a couple of weeks ago and decided to try it out before reading further - fiddling with it sporadically, I eventually got a not-very-concise solution (of unknown efficiency)  and signed up with HackerRank to submit it, link = https://www.hackerrank.com/challenges/two-characters/submissions/code/113330918 .  I note that I can access Stephan's link when signed in (though I haven't gone through it properly yet to compare with my code).

I thought it would be fun (it was), but it took me embarrassingly long. False start with an approach that first considered character frequencies, and might correlate with some comments I later read on the problem's Discussion page, but where I got bogged down for some reason. Then switched to brute-force checks on all character-pair-combination as per salvin's summary (post #2), but with an initial iterative removal of chars that already had 'runs' in the string; I though the initial process (which may be similar to what Stephan mentions in his first paragraph, but iterative) would be optional, and maybe improve efficiency on average, but removing  it exposed bugs in the ability of the rest of my code to handle all strings, and I gave up trying to untangle them. Finally I replaced the offending logic aspect with something more straightforward and confirmed that it worked with or without the optional initial section (which I didn’t include in my upload).

On a tangent, I didn't realise before seeing salvin's second post (#4 in thread) that there was a "chars()" method in String. I see now it's offered on a string by my IDE, but it doesn't seem to be listed in https://docs.oracle.com/javase/8/docs/api/java/lang/String.html (unless I'm missing something obvious) - that seems odd?
Congratulations, Jeanne!
1 month ago
As Adrian says, running the code from learning materials or mock exams and tweaking it to explore any further questions /tangents that might occur to you is a key activity. I usually keep these explorations, including any comments I make about what I discover, and any titbits I pick up looking online to help clarify points about the language, to look back at - it's frightening how I can forget about them after a while. The only other notes I make are the occasional summary or mnemonic diagram to try to clarify or make memorable pesky bits I have mental blocks about remembering. Having said that, I have to go over much of the book(s) I use numerous times to hammer things into my long-term memory - as soon as I finish one section I've at least temporarily forgotten other aspects!

Sabrina Cux wrote:

E Byrne wrote:Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?


I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.


As Dave implies, this would be the result of printing an array object, i.e. and I don't know where you're doing that. Maybe you could post the latest version of your code? What I meant was that lines 19 and 20 would be:                    
2 months ago

Sabrina Cux wrote:

E Byrne wrote:I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)


Yes, I noticed that too. But I soon found out that was because of on line 10, I removed it and it's working fine now.


Ah, yes - I should have noticed that myself!
2 months ago
I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)
2 months ago
Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?

PS - A minor comment: You don't actually need to make an object of the class on which to call the method, as it is static, i.e Lines 5 and 6 can be replaced by
2 months ago
Maybe another option is to make a Jar file, which can be run by the JVM on double clicking its icon. Here's what I do in the NetBeans IDE to obtain the file:
1. Click on the Clean And Build icon
2. Open the "dist" folder (same level as the "src" folder in the project folder for the app) and copy the file represented by the java icon (on mouseover it says "Executable Jar fie")
(Then send/paste it wherever you want that has access to the JVM and double click to run)

(+Welcome to the ranch   )
2 months ago