• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Collections

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

This is my first post in this forum.

Source:Ethumare


import java.util.*;
public class TestClass
{
static String[] sa = { "a", "aa", "aaa", "aaaa"};
static
{
Arrays.sort(sa);
}
public static void main(String[] args)
{
String search = "";
if(args.length != 0) search = args[0];
System.out.println(Arrays.binarySearch(sa, search));
}
}

Here the search is applied on "".O/p should be -1.I have tried i am getting -1.

But the source is saying

Any number from -5 to 3;

Thanks


[ May 30, 2007: Message edited by: Narsingh Rao ]
[ May 30, 2007: Message edited by: Narsingh Rao ]
 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here the search is applied on "".



The default value of search is "".So the output is -1.

But i don't understand what your means of:

But the source is saying

Any number from -5 to 3;

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting -1 with ""!!!
 
Narsingh Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

1)Any number from -5 to 5.
2)Any number from -5 to 5 except -1
3)Any number from -5 to 3.
4)Any number from -5 to 3 except -1.
5)One of a, aa, aaa, or aaaa.
6)One of a, aa, aaa, aaaa, or null.


These are options of that question

They are saying the correct option is 3.

Please explain me this.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- If the elements was found, you would get an index between 0 and 3 (because there are only 4 elements in the array)
- If the element was not found, you would get a negative number.
- According to the API, the negative is : (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list.
- So the biggest index were the element can be inserted is 4 (at the tail). So the returned value is : (-(4) -1) = -5


I have tried but i am getting 0.


I don't understand that. Can you show us your command line ?
 
Narsingh Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks

Satou


I have updated that post .Sorry for the mistake.

Thanks.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narasingh,
Welcome to javaranch. I agree with satou in this.

If we have got the answer -1 in the list,then that whould have been the aswer of this question.

As that is not the case, i alternate answer is 3.

Good Luck.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasad Tamirisa:
Hi Narasingh,
Welcome to javaranch. I agree with satou in this.

If we have got the answer -1 in the list,then that whould have been the aswer of this question.

As that is not the case, i alternate answer is 3.

Good Luck.



If that is the logic of this question, then it is not testing our knowledge of the Java Collections.binarySearch method. If the question is about the possible results of the binarySearch method for a general input, then it is correct. As it stands we do not know the exact question as asked by the mock exam.
 
Are we home yet? Wait, did we forget the tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic