Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Functions | Question 7

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict the output of the following program.




class Test
{
    public void demo(String str)
    {
        String[] arr = str.split(";");
        for (String s : arr)
        {
            System.out.println(s);
        }
    }
  
    public static void main(String[] args)
    {
        char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' '
                        'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};
        String str = new String(array);
        Test obj = new Test();
        obj.demo(str);
    }
}

(A)

ab cd
ef gh
ij kl

(B)

ab
cd;ef
gh;ij
kl

(C) Compilation error


Answer: (A)

Explanation:
Class String has a inbuilt parameterized constructor String(character_array) which initializes string ‘str’ with the values stored in character array.
The split() method splits the string based on the given regular expression or delimiter passed it as parameter and returns an array.


Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads