Menu
Question Index
...

What will be the output of following program?

public class LastIndex {
    public static void main(String args[]) {
        String str = "Now is the time for all men to come to improve our country";
        int i = 0;
        while (true) {
            int first = str.indexOf("Now", i);
            if (first == -1)
                break;
            int start = first + 3;
            int end = str.lastIndexOf(" ", start);
            System.out.println(end);
            i = end + 1;
        }
    }
}


6
7
8
3
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Searching Strings - Java indexOf, lastIndexOf Methods

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App