Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Question - Im in Doubt

 
Frederico Werly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was studing about String and i cant understand why some characters have changed to Uppercase and other to LowCase.


Here the code :
----------------------------------------------------------------------------
public class StringsDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
StringBuilder dest = new StringBuilder(len);
for (int i = (len - 1); i >= 0; i--) {
dest.append(palindrome.charAt(i));
}
System.out.println(dest.toString());
}
}
----------------------------------------------------------------------------
The output from this program is:

doT saw I was toD
----------------------------------------------------------------------------

WHY NOT "Dot saw I was Tod" ?

Thanks,
Fred.
[ June 15, 2005: Message edited by: Frederico Werly ]
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at what you are doing. Your program does not change the case of any of the characters, it simply reverses the order.
 
Frederico Werly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH my god !!! is true !!!

Thanks .. eheheheh .. LOL !!
 
karthik Madhesan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Bell:
Look at what you are doing. Your program does not change the case of any of the characters, it simply reverses the order.


good catch up steven...
keep it up..

Thanks
Karthik_Techie
SCJP1.4,ICAD 285,Prep SCJA.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these type of questions being asked in Sun exams?
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arulkumar Gopalan:
Are these type of questions being asked in Sun exams?


Yes, to get a feel for what are the questions that may be asked in exam take a look at some mock exams in each topic. As an example, lets look at http://www.examulator.com/phezam/question.php Question No=36 ( The question changes daily, so you may not get the same question when u visit. I am mentioning the original URL as part of JavaRanch question posting policy ).

Question : What will happen when you attempt to compile and run the following code?



  • 1 Compile time error, StringBuilder cannot be cast to a String.
  • 2 Compilation but runtime exception where the code attempts to cast StringBuilder to String.
  • 3 Compilation and output of "JohnSmith" at runtime.
  • 4 Compilation but blank output at runtime.


  • The Correct Answer is
    1) Compile time error, StringBuilder cannot be cast to a String.

    When I read thru the program, the first thing that stuck me was that main( String[] ) method calls constructor, but constructor does not call output( StringBuffer ) method. There is a possibility that I might not focus on the compilation error.

    In code related questions, there may be code which distracts the candidate from the real answer. There would be no questions that deliberately confuse you, like for example declaring a variable called "l1" ( the lowercase L char follwed by number 1 = one ) and some other place use a variable "ll" ( two lowercase L chars in sequence ) and ask if the code will compile.

    But you might expect a question which tests a concept like a main method signature like main( String args ) - Note that though the variable is called args, but its not a String array. A class with this main method will compile fine, but not run because jvm will try to find the main( String[] ).

    <Bottomline>Look thru some mock exams</Bottomline>

    Visit The JavaRanch mock exam @ http://www.javaranch.com/roundup.jsp[/URL]
     
    Mahesh Kumaraguru
    Ranch Hand
    Posts: 109
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The confusing variable names depends on font - for example lowercase L and number 1 look clearly distinct above, but "rn" lowercase RN and "m" lowercase M may look more similar.
     
    Arulkumar Gopalan
    Ranch Hand
    Posts: 104
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    From what I understand, these kinds of questions are like reviewing the written code. So, it is more like testing ´┐ŻReview Effectiveness´┐Ż of a person in the exam. A person might have good technical knowledge, might not be a good reviewer. Whatz your thought? :roll:
     
    Mahesh Kumaraguru
    Ranch Hand
    Posts: 109
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Arulkumar,

    Review effectiveness questions like clear naming of variables are not part of exam, but a question asking will a class with method "public static void main( String args )" compile ? run ? is testing the java programming language basics. There will be no attempt to confuse the candidate, but the candidate is expected to be clear of language fundamentals.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic