Menu
Topics Index
...
`

Print Number of names


If you need explanation Read this topic

If you need Answer Take test on this topic

Print Number of names
What will be the output of the following program?
public class ArrayExample {
    public static void main(String[] args) {
        int numNames = getInt("Number of names = ");
        String[] names = new String[numNames];
        int i = 0;
        System.out.println();
        names[0] = getString("S.No " + (numNames - 1), numNames);
        System.out.println("- " + names[0]);
        names[1] = getString("S.No " + (numNames - 1), numNames);
        System.out.println("- " + names[1]);
        names[2] = getString("S.No " + (numNames - 1), numNames);
        System.out.println("- " + names[2]);
    }
    public static int getInt(String prompt) {
        int integer = 3;
        System.out.print(prompt + " " + integer);
        return integer;
    }
    public static String getString(String prompt, int i) {
        System.out.print(prompt + " ");
        switch (i) {
            case 0 :
                return "Oleti";
            case 1 :
                return "Kiran";
            case 2 :
                return "Kumar";
        }
        return "None";
    }
}

A. Number of names =  3
S.No 3 - None
S.No 2 - Kumar
S.No 1 - Kiran
B. Number of names =  3
S.No 3 - None
S.No 3 - None
S.No 3 - None
C. Number of names =  3
S.No 2 - Kumar
S.No 1 - Kiran
S.No 0 - Oleti
D. Some other output
E. Compilation Error or 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.

Answer : D
we will get Number Of Names = 3
Sno :2 - None
Sno : 2 -NOne
Sno : 2 - None
as  the getInt() will return 3 and this is stored in numNames and when this is used to access any of the cases in getString() it will return None as no case will match with it as it asking for a case : 3
>and also the numNames-1 will always return 2 coz we are not changing th numNames value it is alwyas 3

Posted by Deepak Kumar Yadav    2014-05-22 13:06:13


Ans is D. Output is
Number of names =  3
S.No. 2 - None
S.No. 2 - None
S.No. 2 - None
First numNames is initialized to 3 and for each getString() call int value passed is 3 so it is not in the switch case. Every time None is printed. and every call has the same String as argument i.e., S.No 2.

Posted by Nikhil Sulibhavi    2014-05-22 13:07:04


ans d. Number of names=3
           s.no 2 - none
           s no 2  - none
           s.no 2 - none
getstring method passd arg are string prompt 2, integer 3

Posted by Maheshwari Natarajan    2014-05-22 13:10:27


answer  : D, the numNames = 3, and when  getString() method is called Integer value 3 is passed , then the switch case having cases 0, 1, 2 only and there is no "case 3:" so it prints empty..

Posted by Damu Surya    2014-05-22 15:15:21


Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-23 04:50:03


© meritcampus 2019

All Rights Reserved.

Open In App