Menu
Topics Index
...
`

Creating Method and passing array as a parameter


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Method and passing array as a parameter
What will be the output of the following program?
public class FindIt {
    public static void displayNames(String... names) {
        System.out.println(names.length);
        for (String word : names) {
            System.out.print(word + " ");
        }
    }
    public static void main(String args[]) {
        displayNames("Kiran", "and", "Ashok", "are", "friends.");
    }
}

A. 5
Kiran and Ashok are friends.
B. 3
Kiran and Ashok
C. Some other output
D. Compilation Error
E. Runtime Error
Topic: Java String Arrays - String Arrays 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.

Ans is D since the display names takes array of strings as input but we are giving them as predefined set of strings as arguments

Posted by Rathnakar Reddy    2013-07-03 03:24:52


Option A is answer ,..5 is displayed as number of strings in array ,.and next the strings in array are displayed as follows .output is
5
Kiran
and
Ashok
are
friends.

Posted by Bobbili Kiran Kumar Reddy    2013-07-03 03:30:27


option a is the answer in main we are sending the strings as inputs to the displyname method it will print first the lenth i.e. no. of strings and next it will print the all the strings by miaintaining a space between them

Posted by Vijay Kumar    2013-07-03 04:07:49


option a as as the array os strings being passed has 5 strings and all the strings are printed using the  for loop :)

Posted by Pramod Jain    2013-07-03 07:57:49


Congratulations Bobbili Kiran Kumar Reddy. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-03 14:28:17


© meritcampus 2019

All Rights Reserved.

Open In App