Menu
Topics Index
...
`

Possible Ways


If you need explanation Read this topic

If you need Answer Take test on this topic

Possible Ways
What will be the output of the following program?
import java.util.*;
public class Ways {
    public static Set<String> permutationFinder(String str) {
        Set<String> permutation = new HashSet<String>();
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            permutation.add("");
            return permutation;
        }
        char initial = str.charAt(0);
        String rem = str.substring(1);
        Set<String> words = permutationFinder(rem);
        for (String letter : words) {
            for (int i = 0; i <= letter.length(); i++) {
                permutation.add(charInsert(letter, initial, i));
            }
        }
        return permutation;
    }
    public static String charInsert(String str, char c, int j) {
        String a = str.substring(0, j);
        String b = str.substring(j);
        return a + b;
    }
    public static void main(String[] args) {
        String s = "AAC";
        String s1 = "ABC";
        System.out.println(permutationFinder(s));
        System.out.println(permutationFinder(s1));
    }
}

A. [AAC, ACA, CAA]
[ACB, ABC, BCA, CBA, CAB, BAC]
B. [AA, A, CA, CAA]
[A, CBA, CA, BA]
C. [AA, A, CA, CAA]
[A, CBA, CA, BA]
D. Some other output
E. Compilation Error or Runtime Error
Topic: Collection Framework In Java

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.

D some other output because evertime permutation.add("") adds nothing to set

Posted by Akash Kolariya    2014-02-06 13:42:03


ans is D Given string will be printed again before making for loop it is calling the method again and else if statement will closes the method so it will print the given strings only

Posted by Ashok Reddi    2014-02-06 15:55:00


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

Posted by Merit Campus    2014-02-07 09:05:35


© meritcampus 2019

All Rights Reserved.

Open In App