• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Marcus Green's exam

 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
1) Bic
2) ic
3) icy
4) error: no method matching substring(int,char)
The answer is (4). Does the char value always get converted to int, even in the case of substring function?
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Geeta,
I think the answer 4 is wrong. If you run the
code you will see that it produces answer 2: ic
Gian Franco
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi geeta, the answer is 2 and not 4 as you pointed out. The 'char' datatype is implicitly casted to 'int' since 16-bit fits perfectly in 32-bit.
The signatures for the substring method are:
1) substring (int beginIndex)
2) substring (int beginIndex, int endIndex)
Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic