Menu
Question Index
...

What will be the output of the following program?

public class ToRecurseIsDivine {

    public static void main(String[] args) {
        print("To Iterate Is Human");
        print("To Recurse Is Divine");
    }

    private static void print(String statement) {
        int count = statement.length();
        if (count == 0) return;
        if (count % 2 == 0) {
            System.out.print(statement.charAt(0));
            print(statement.substring(1));
        } else if (count % 2 != 0) {
            System.out.print(statement.charAt(count - 1));
            print(statement.substring(0, count - 1));
        }
    }
}


nTaom uIHt esrIa teTeon iRveicDu rssIe
Tnoa mIutHe rsaIt eeTnoi vRieDc usrIs e
Some other output or throws StackOverflowError
Compilation Error

Doubts

Problems

Topic: Java String substring() method - substring In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App