Multiple choice technology programming languages

If you run the code below, what gets printed out? 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)

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

The substring method in Java extracts characters from index begin (inclusive) to end (exclusive). With s='Bicycle', begin=1, end=3, it extracts characters at positions 1 and 2, which are 'i' and 'c', resulting in 'ic'. Note that iEnd is char but gets promoted to int.

AI explanation

In Java, char widens automatically to int in a method call, so substring(int iBegin, char iEnd) resolves fine to substring(1, 3) without any compile error. substring(beginIndex, endIndex) returns characters starting at index 1 up to (but not including) index 3 of "Bicycle" (indices: B=0, i=1, c=2, y=3...), giving "ic".