Multiple choice

What will be the output of the given code?

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

  1. o

  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 the correct answer. Substring method takes two parameters (BeginIndex, EndIndex).Substring(int Begin, int End). It returns the string from the beginning to end – 1. But if the BeginIndex is equal to the EndIndex, then an empty String with length 0 will be returned. So, here substring(4, 4) will return an empty string of length 0.