Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCA/OCP Java SE 8 Programmer Practice Tests

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm studying the book OCA/OCP Java SE 8 Programmer Practice Tests, by Scott Selikoff and Jeanne Boyarsky. The question 46 at chapter 9 states the following:


46. Which of the following types can you pass as a parameter to the replace() method on the String class?
I. char
II. String
III. StringBuilder


And the answer is the following:


D.
There are two signatures for the replace() method. One takes two char parameters. The other signature takes a CharSequence. Both String and StringBuilder implement this interface. This makes all three alternatives correct, and Option D is correct.


The question asks for the types that can be passed for replace() method on the String class. The String class only have the following versions of the replace method:
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
So far so good, this is contemplated by the answer. However, why String and StringBuider are considered valid, once there isn't a method that takes them as parameter. I couldn't really understand what "Both String and StringBuilder implement this interface" has to do with it.
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OUTPUT-> GAR


Hope this could help
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

cleverson teo wrote:However, why String and StringBuider are considered valid, once there isn't a method that takes them as parameter. I couldn't really understand what "Both String and StringBuilder implement this interface" has to do with it.


If classes String and StringBuilder implement interface CharSequence, would these classes not be valid parameters for the second overloaded version of the replace method?

If you check the javadoc of the CharSequence interface, you'll find a nice overview of all known implementing classes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic