This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Array of String sorting

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks. I am studying  OCA/OCP Java SE8 (SCOTT SELIKOFF and JEANNE BOYARSKY) and  doing practice  test. In chapter 4  I am confused doing question 17,29,32.
Q17. String[] os = new String[] {"Mac", "Linux","Windows"};
Arrays.sort(os);
System.out.println(Arrays.binarySearch(os,"MAc"));


What strategy do you apply or your way ? Thank in advance.
 
Sheriff
Posts: 4673
308
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not everybody has a copy of the book to hand, perhaps you can include the questions?
 
author & internet detective
Posts: 39528
776
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I, of course, do have a copy of the book. The question is what does the code output.

This question is testing if you know the default sort order and what gets returned by binarySearch(). Can you elaborate on what you are confused about?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

public class Program
{
   public static void main(String[] args) {
       String[] os = new String[] {"Mac", "Linux", "Windows"};
       printArray(os);
       Arrays.sort(os);
       printArray(os); System.out.println(Arrays.binarySearch(os,"Mac"));
   }
   
   public static void printArray(String[] a){
       String result = "[";
       for(int i = 0; i < a.length; i++){
           result += a[i] + ",";
       }
       result = result.substring(0,result.length() - 2) + "]";
       System.out.println(result);
   }
}

Please check the code above to understand the function.
 
Rancher
Posts: 3369
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the question have a binarySearch for "MAc" or for "Mac"?  Those will have very different answers.  If it's "MAc" you would also need to know whether "A" comes before or after "a" in the ASCII table.  That seems a little obscure for a Java test.
 
Master Rancher
Posts: 259
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel, please wrap your code in code tags. That makes it easier to read and understand the code.

I believe that the OP is in the earlier stages of learning Java. Could you please add some explanation to your post to clarify what you mean?
 
Brecht Geeraerts
Master Rancher
Posts: 259
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book says "Mac".

Q17 from chapter 4 (see Jeanne and Scott's book on OCA/OCP practice tests) states the following:

What does the following output?

What binarySearch() does is it tells you if a certain element is present in your array or not.
- If yes, it returns the index of where this element is stored.
- If not, then it returns the negative of the index where the element would belong, minus one.
See also the JavaDocs for more information.

However, one crucial point is that the array MUST be sorted, otherwise the result returned by the search function is undefined.

So, knowing this, what would the output be?

 
Brecht Geeraerts
Master Rancher
Posts: 259
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:That seems a little obscure for a Java test.



I'm sure Oracle would be capable of asking such questions...  
 
Jeanne Boyarsky
author & internet detective
Posts: 39528
776
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:you would also need to know whether "A" comes before or after "a" in the ASCII table.  That seems a little obscure for a Java test.


Agreed. It's something I memorize every time I study for the cert and then promptly forget.
 
Manuel Prochnow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the hint. Here is my revised contribution.



the results are:
[Mac,Linux,Window]  (the array bevor sort)
[Linux,Mac,Window]  (the array after sort)
1                             (the result of binarySearch with the correct spelling)
-2                            (the result of binarySearch with the erroneous spelling (MAc instead of Mac) )
 
Shiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!