File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Few Interview questions I was not sure of

 
Justin Howard
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A look at the java.lang.String class API will actually allow you to find a Comparator<String>
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think String and the wrapper classes have always implemented Comparable.
 
Prateek Parekh
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct about the <String> bit.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you found the Comparator<String> in the String class yet?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic