| Author |
Few Interview questions I was not sure of
|
Justin Howard
Ranch Hand
Joined: Feb 19, 2009
Posts: 162
|
|
Hi All,
Recently took an interview. I was not sure of few questions. Googling did not result in specific answers.
Any help appreciated.
1. Is there any default comparator for String, Integer, Float etc
2. What is the utility class for String functions.
3. Single method call to reverse a string.
4. Single method call to to find the largest letter in a string eg: Hello , largest is o is largest.
Thanks
|
 |
Prateek Parekh
Ranch Hand
Joined: Apr 17, 2010
Posts: 34
|
|
1. Is there any default comparator for String, Integer, Float etc
Yes. If you look at the javadoc for these classes, you'll notice that classes like String and Integer implement the Comparable<String> and Comparable<Integer> interfaces.
This was introduced in Java 1.5 with Generics. They allow you to do tasks like Sort objects on criteria (Artist) other than just plain Song name (String).
For the other questions, what do you think should the answers be? Did you take a look at http://java.sun.com/j2se/1.5.0/docs/api/index.html?overview-summary.html
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
A look at the java.lang.String class API will actually allow you to find a Comparator<String>
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
I think String and the wrapper classes have always implemented Comparable.
|
 |
Prateek Parekh
Ranch Hand
Joined: Apr 17, 2010
Posts: 34
|
|
Campbell Ritchie wrote:I think String and the wrapper classes have always implemented Comparable.
Yes. I agree they have. But without the <> until 1.5 (if my memory serves me right).
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
Yes, you are correct about the <String> bit.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
Have you found the Comparator<String> in the String class yet?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
2) String? StringBuilder? Who knows. There's no one single class.
3) StringBuilder.reverse() -- but you're not going to use it with "a single method call", given a String, because you have to construct the StringBuilder.
4) The only thing I can think of here is Arrays.sort() on the char[] from a String, followed by looking at the last character. Again, you can't do this by calling a single method, but maybe that's just semantics.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: Few Interview questions I was not sure of
|
|
|