Menu
Question Index
...

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";
    }
}


Number of names =  3
S.No 3 - None
S.No 2 - Kumar
S.No 1 - Kiran
Number of names =  3
S.No 3 - None
S.No 3 - None
S.No 3 - None
Number of names =  3
S.No 2 - Kumar
S.No 1 - Kiran
S.No 0 - Oleti
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java String Arrays - String Arrays In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App