Menu
Topics Index
...
`

Squirrels Chirping


If you need explanation Read this topic

If you need Answer Take test on this topic

Squirrels Chirping
What will be the output of the following program?
public class Squirrels {

    public static void main(String[] args) {
        String sounds[] = {"chittering", "chirping", "barking", "squealing"};

        int i = 0;
        int startIndex = 0;

        int endIndex = sounds[i].indexOf('t');
        do {
            int length = sounds[i].length();
            if (startIndex >= 0 && endIndex < length)
                System.out.print(sounds[i].substring(startIndex, endIndex + 1) + "#");
            else if (endIndex >= length)
                System.out.print(sounds[i].substring(startIndex, 3) + "$");
            endIndex = sounds[i].indexOf('i');
            i++;
        } while (i < sounds.length);
    }
}

A. ch#ch#bark#squeal#
B. chi#ch#ba#sque#
C. chit#chi#bar#squea#
D. Some other output or Compilation Error or Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

C is the correct answer...Initially endIndex=3...When i=0 in the first iteration length=10.First if executes substring(0,4) is abtained which is chit# The end index=2 In second Iteration length=8 First If executes and substring(0,3) is chi# endIndex=2...In 3rd Iteration length=7 First If executes substring(0,3) is bar# endIndex=4 In last Iteration length=9 First If executes substring(0,5) is squea#  Control comes out of the do while loop...

Posted by Bhavana Sai    2013-09-30 09:06:40


Ans is C.The substirngs is printed to the screen wth respect to the specified character positions in the string using indexof() until the specified condition in the do-while fails.

Posted by Veda Sree    2013-09-30 09:39:57


Congratulations Veda Sree. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-10-01 13:17:30


© meritcampus 2019

All Rights Reserved.

Open In App