Multiple choice

What will be the answer of the given code?

String s = “Welcome To Java” s.substring(4 , 4 );

  1. 0

  2. c

  3. Empty string

  4. Compile time error

  5. Run time error

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

This is wrong. Substring method takes two parameters (beginIndex,EndIndex).Substring( int Begin , int End). It returns the string from begin to end – 1. But, if the begin index is equal to the end index then an empty String with length 0 will be returned. So, here substring(4,4) will return an empty string of length 0. Hence, this is the correct answer.