Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:18:26+00:00 2026-06-05T19:18:26+00:00

Hopefully someone knows the answer to this Java-Certification question: public static void main(String[] args)

  • 0

Hopefully someone knows the answer to this Java-Certification question:

public static void main(String[] args) {
 String[] sa = {"d", "c", "a", "b" };
 int x = Arrays.binarySearch(sa, "b");
 Arrays.sort(sa);
 int y = Arrays.binarySearch(sa, "b");
 System.out.println(x + " " + y);
}

Which two results are possible? (Choose two.)
A) 7 0
B) 7 1
C) 7 3
D) -1 0
E) -1 1
F) -1 3

The only true answer is E) -1 1, because if you play through the binary-search-algorithm this is the only possible output. But they want me to choose two…
So the second one have to be B) 7 1 then, because the second binarySearch in the sorted array will always return 1.

So my Question is, why is B) 7 1 a possible result?
More specific: How is it possible, that the first binarySearch in the unsorted array returns 7?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:18:28+00:00Added an answer on June 5, 2026 at 7:18 pm

    This is a trick question. The results of binary search on an unsorted array are undefined, as per documentation:

    The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined.

    This means in particular that any number, including seven, is fair game.

    The results on a sorted one are well-defined: you’ll get a 1. As if by magic, there are only two pairs that end in 1 on the list of answers, so B and E is the choice the examiners expect you to make.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully a nice easy question to which I hope someone knows the answer. I
Hopefully someone can help with this, I can't find the answer anywhere! I'm currently
Hopefully this should be an easy answer for someone out there (and possibly a
This could be a very basic question, but hopefully someone will be able to
This is pretty much a duplicate of this unanswered question , but hopefully someone
Hopefully someone familiar with wget & wput on windows knows about this... wput -v
This is my first question on stackoverflow, hopefully there is someone who can help
Quick question hopefully someone can help out here. I'm trying to copy and paste
This is a weird one, but hopefully someone can give me an idea here.
I'm not really sure how to word this exactly, so hopefully someone can make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.