• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

a question about API Contents

 
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
I hope successes to everybody!
My question is such:


1. class Mutate {
2. public static void main(String [] args) {
3. StringBuilder s = new StringBuilder("012345678 ");
4. if (s.length() == 10)
5. s.insert(10, "abcdef");
6. s.delete(3,8);
7. System.out.println(s.indexOf("c"));
8. }
9. }

What is the result?

a. -1
b. 5
c. 6
d. 7
e. Compilation fails.
f. An exception is thrown at runtime.

one of my friend said me that the result must be "d. Compilation fails." .
but when i run this program the output shows 7
Can you please which one is true?
Sincerely,
Rizvan
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rizwan, Please Quote the Source.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One of my friend said me that the result must be "d. Compilation fails."


Did you ask him why ?
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He said me that at the line of 5th incorrect...
Because the 10th index does not correspond "012345678 "

4. if (s.length() == 10)
5. s.insert(10, "abcdef");

Gratitudes at first!
Rizvan
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a " " space after the digit 8, which makes the length()==10 true.
Hope this helps..

Please Quote the Source Rizwan.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the answer is d.

Since there is a white space and the end of the original string and since we are using string builder here. That has to be the answer.

It will find the position of the value 'c' to be at 7.

 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
Thanks for your response and times...
I have take this exam test: http://www.sun.com/training/certification/assessment/055-Jintro.jsp

I know s.length() == 10 is true but s.insert(10, "abcdef") isn't...
I think symbol 10 is not existing ("012345678 ")....(for insert method)

Since there is a white space and the end of the original string and since we are using string builder here.



You want to say that this is about string builder? (d. 7 or e. Compilation fails. )

Sincerely,
Rizvan
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
insert(10,"abcdef");
This means insert abcdef at the index position 10. which is after the space.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After inserting it will look like,
012345678 abcdef
then delete(3,8)
which gives 0128 abcdef.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So now guess what is the index position of alphabet "c".
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Abhi vijay,

Thanks a lot......

I understood completely

but Forgive me that I know English a little also I use translation...therefore ...

Sincerely,
Rizvan
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats perfectly fine, Rizwan.
 
Water! People swim in water! Even tiny ads swim in water:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic