• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java String printing  RSS feed

 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is asked in my interview

Suppose that i have a string name
String str="Sudhir Kumar Ojha";
Now i want to print only SKO w ithout using methods
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Kumar Ojha wrote:The following question is asked in my interview

Suppose that i have a string name
String str="Sudhir Kumar Ojha";
Now i want to print only SKO w ithout using methods


without using String methods?
 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Exactly

mainly charAt(int index); method
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Kumar Ojha wrote:
mainly charAt(int index); method


Pretty straightforward question. How did you answer it during the interview?

Henry
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Kumar Ojha wrote:Yes Exactly

mainly charAt(int index); method


Alternatively, you can use split and then substring to get Initial alphabet of each word. java.lang.String
 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can  you write a code
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Kumar Ojha wrote:Can  you write a code


Below is the code.


 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Kumar Ojha wrote:Now i want to print only SKO w ithout using methods
Not even a signle method?  like we can do that in C language using only loop?
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Khalid wrote:without using String methods?

Sudhir Kumar Ojha wrote:Yes Exactly
mainly charAt(int index); method

M Khalid wrote:
Don't you think, you still used .split() and .substring() methods of String, what do you say?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Don't you think, you still used .split() and .substring() methods of String, what do you say?


Well, M Khalid did say "alternatively", so this is a side discussion, and not an answer to the interview question... which only allowed the charAt() method.

Henry
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:which only allowed the charAt() method.
Ok, you mean to say we are allowed to use charAt() method ?
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:
Henry Wong wrote:which only allowed the charAt() method.
Ok, you mean to say we are allowed to use charAt() method ?

Sudhir Kumar Ojha wrote:
Yes Exactly
mainly charAt(int index); method



You are not allowed to user charAt method primarily.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... good point. I think I may have misread the question.... here's a few cows for the confusion that I caused.

Henry
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me that the question is ambiguous. What does it mean "without using methods" Does that mean "Without using any methods, of any class, period?" if so, the question is ridiculous. You can't do it, since you have to use a method to print anything.

Does it mean without using ANY method of String? Then you can create a StringBuffer and proceed from there. Or does it mean "you can't use charAt(), but you can use other methods?" I would have asked the interviewer to clarify the question.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Interviewer might intentionally ask you not even using single method of String Or StringBufffer Or StringBuilder and try to confuse you and make you use logic like we do in C where String is an array of chars which is not in case of Java.
  • If allows few methods then, this is what so far I tried, there could be more ways to do so..
  • Output:
    Using charAt method only
    SKO
    Using getChars() and length() methods
    SKO
    Using split(\s) method only
    SKO
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You all taking wrong i mean to say that not use charAt() method in this program.



    And i dont have any answer at time of interview so i simly skiped it
     
    Daniel Cox
    Ranch Hand
    Posts: 231
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If we can't use String's charAt() method, can we use StringBuilder's charAt() method? If so, we can use the String literal to create a StringBuilder.
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Daniel Cox


    In interview HR ask like this


    You have a string like this
    String strr="SUdhir Kumar Ojha";
    Now you have to print only SKO without using methods specially charAt() method of String class.

    I hope now you fully understand this question.
     
    Fred Kleinschmidt
    Bartender
    Posts: 572
    9
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This interview question is one of those of the form "How would you do this in a way other than the correct way?" I would be tempted to answer it "I wouldn't. I would do it the correct way." Presumably it is meant to test the applicant's knowledge of other ways of doing things. In that case, I would answer "I would look up the documented API to see if there was another and possibly better way."

    No one should be expected to be intimately familiar with all methods of all classes in the Java libraries. Asking an applicant how to do something in a roundabout way is just silly, and demonstrates that that company is clueless when it comes to software development.
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well thanks guys and specially Fred Kleinschmidt because i also reply same in my interview and i say him i can not do it without using methods.
     
    Daniel Cox
    Ranch Hand
    Posts: 231
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I hope that that was the question that was asked. I find it strange that a sensible company asked such a silly question. You can't do anything without calling a method. At the very least, you must call the main method.
     
    salvin francis
    Bartender
    Posts: 1664
    37
    Eclipse IDE Google Web Toolkit Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    HR asked you a coding question ?
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know off topic   but couldn't stop praising someone's art, I visited Salvin.in It was just awesome. Great work Salvin!
     
    M Khalid
    Ranch Hand
    Posts: 57
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is silly question, but i guess the interviewer didn't convey his/her question properly.
    I guess it would be like without using String methods directly or different ways to get the result.
    because in my early interviews i was also asked questions like
    "How to reverse a String without using String methods directly"
    For Eg: String is "Java" so we can use StringBuilder method reverse to directly reverse the string in a single line of code.
    but instead interviewer expectation was ( IMO ) the alternative ways , like storing string into char array and then looping backwards to get the reverse string.
    Hope that's the case here.

     
    M Khalid
    Ranch Hand
    Posts: 57
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:
  • Interviewer might intentionally ask you not even using single method of String Or StringBufffer Or StringBuilder and try to confuse you and make you use logic like we do in C where String is an array of chars which is not in case of Java.
  • If allows few methods then, this is what so far I tried, there could be more ways to do so..
  • Output:
    Using charAt method only
    SKO
    Using getChars() and length() methods
    SKO
    Using split(\s) method only
    SKO


    Exactly , interviewer needs alternative methods. Your second method (above) is what i guess was expected in such interview questions.
     
    Daniel Cox
    Ranch Hand
    Posts: 231
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    M Khalid wrote:Exactly , interviewer needs alternative methods.

    I think Sudhir Kumar Ojha made it clear that no methods are allowed. Not even the main method.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In college days, I really adored such programs dealing with Strings which is an array of chars in C language but in Java there are so many already built in methods for that so I would have much appreciated interviewer If he/she had asked question regarding String literal pool concepts Or Why Strings are so special Or immutable in Java...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!