Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default What will be output by the following code? using trim method.

    What will be output by the following code?

    String s = " Hello World ";
    s.trim();
    System.out.println("#" + s + "#");

    A. # Hello World #
    B. #Hello World #
    C. # Hello World#
    D. #Hello World#
    E. ##

    I put A. is it correct ?

  2. #2
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Why dont you try it? And you are wrong i think. What do you think trim() does?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ocean View Post
    Why dont you try it? And you are wrong i think. What do you think trim() does?
    Strings are immuatable; the trim() method returns the modified String which is ignored; the correct answer is A.

    kind regards,

    Jos

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Had to try it mySelf, and yes it is A that is correct =)

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I can't believe I fell for that and thought D lol. *facepalm*

    But ya like Jos said it's A because strings are immutable. D would be the correct answer if you had s = s.trim() though.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 AM
  2. Question about File renameTo() method : Abnormal output
    By suvojit168 in forum New To Java
    Replies: 1
    Last Post: 07-25-2009, 08:22 PM
  3. How to trim the value in jrxml code
    By Deepa in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 08:27 AM
  4. String trim
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:35 PM
  5. invoking method and output questions
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •