Menu
Topics Index
...
`

Weekend Hack (Trail Run) has these questions


Group Strings By Length

Write a program to group the string objects by length. Strings less than 10 characters should be added to the first group, Strings with exactly 10 characters should be added to the second group and Strings greater than 10 characters should be added to the third group.
Input (ArrayList containing Strings) Outputs - Less Than Group, Equals Group, Greater Than Group (All of them are ArrayLists containing Strings)
["Andhra Pradesh", "Tamil Nadu", "Maharashtra", "Madhya Pradesh", "Gujarat", "Karnataka"] Less than group = ["Gujarat", "Karnataka"]
Equals group = ["Tamil Nadu"]
Greater than group = ["Andhra Pradesh", "Maharashtra", "Madhya Pradesh"]
["Sachin Tendulkar", "Yuvraj", "Harbhajan", "Virat Kohli", "Virendar Shewag", "MS Dhoni", "Suresh Raina", "Rohit"] Less than group = ["Yuvraj", "Harbhajan", "MS Dhoni", "Rohit"]
Equals group = [EMPTY]
Greater than group = ["Sachin Tendulkar", "Virat Kohli", "Virendar Shewag", "Suresh Raina"]
class GroupStringsByLength

{    public static void main(String s[])
    {
        ArrayList input = new ArrayList();
        input.add("Andhra Pradesh");
        input.add("Tamil Nadu");
        input.add("Maharashtra");
        input.add("Madhya Pradesh");
        input.add("Gujarat");
        input.add("Karnataka");
        
        ArrayList lessThanGroup = new ArrayList();
        ArrayList equalsGroup = new ArrayList();
        ArrayList greaterThanGroup = new ArrayList();
        
        groupStringsByLength(input, lessThanGroup, equalsGroup, greaterThanGroup);
        
        System.out.println("Less Than Group = " + lessThanGroup);
        System.out.println("Equals Group = " + equalsGroup);
        System.out.println("Greater Than Group = " + greaterThanGroup);

    }


public static void groupStringsByLength(List<String> input,
List<String> lessThanGroup,
List<String> equalsGroup,
List<String> greaterThanGroup)
{
//Write code here to group the strings by length and add it to the appropriate group.
}
}



Methods Output

What will be the output of the following program.
class MethodsTest
{
    public static void main(String s[])
    {
        int a = 34;
        int b = 56;
        int c = sum(a, b);
        System.out.println("c = " + c);
    }
    
    public static int sum(int a, int b)
    {
        System.out.println("a = " + a + " b = " + b);
        return a + b;
    }
}
A. a = 34 b = 56
c = 90
B. a = 56 b = 34
c = 90
C. c = 90
D. Compilation Error



© meritcampus 2019

All Rights Reserved.

Open In App