• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Important Question From MarcusGreen

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a Question From MarcusGreen Mock Exam-1.
What will be the Out Put if the following code is written in Main Method ?
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3; //seeThis is char and Not int
System.out.println(s.substring(iBegin,iEnd));
1) Bic
2) ic
3) icy
4) error: no method matching substring(int,char)
Ans:2
See for More Questions this Link www.jchq.net/mockexams/exam1.htm
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Well the answer is right and the extract from JLS is produced below for reference.
20.12.32 public String substring(int beginIndex, int endIndex)
throws IndexOutOfBoundsException

The result is a newly created String object that represents a subsequence of the character sequence represented by this String object; this subsequence begins with the character at position beginIndex and ends with the character at position endIndex-1 . Thus, the length of the subsequence is endIndex-beginIndex.
If beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex, then this method throws an IndexOutOfBoundsException.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Hope this helps
Thanks
Ajay


[This message has been edited by Ajay Kumar (edited May 08, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bidyut,
As far as I know, this is the ONLY strange method to which we have to be careful about the 2nd index. All other methods from String class adhere to indexes (0,1,2,3..) pattern. In other words .charAt(3) means it is char at 0..1..2..(3) , which means the forth char.
regds
maha anna
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, My q is does the char get promoted to int? So that means its legal to define a variable char as
char myInt = 10;
I have compiled this and it compiles perfectly fine.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. All 'int' literals are assignable to lesser range primitive types char/short/byte provided the value of the 'int' literal is within the range of the left hand side var type.
Here char range is from 0 to (2^16)-1. So value 10 is within this range. So this is valid statement.
At the same time this is NOT valid.

byte b = 1000; // Not valid
byte b1 = 100; //Valid
byte b2 = 127; //valid

Also note that this rule is NOT APPLICABLE for method invocation.
For example there is a method like the foll

void m1(byte b, char c, short s) { }

The you CAN NOT call the above method like this

m1(10,20,30); // NOT VALID

Here the 'int' literals are treated as 'int' type and you have to specifically cast them to byte/char/short.

m1((byte)10,(char)20,(short)30); // VALID

regds
maha anna
[This message has been edited by maha anna (edited May 08, 2000).]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha,
one small correction!
That rule works for method invocation too!!
I tried this program and it works!!
class TestProgram
{
public static void main(String[] args)
{
byte a = 100;
char c = 67;
testMethod(a, c);
}
static void testMethod(byte b, char d)
{
System.out.println("byte = " + b + " char = " + d);
}
}
The output is:
byte = 100 char = C

Hope it helps. - suresh.
 
Suresh
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey sorry for my previous post!!
That was wrong!! That works only when you send those variables!!
It doesn't work when you pass the literals as testMethod(100, 67).
Sorry again!! - suresh.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's ok Suresh. This rule is for 'int' literals.
regds
maha anna
 
reply
    Bookmark Topic Watch Topic
  • New Topic