What will be the output of the following Java code? [code] class output { public static void main(String args[])

What will be the output of the following Java code?
Code:
    class output 
    {
        public static void main(String args[])
        {
            String a = "hello i love java";
            System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));
        }
    }
A. 6 4 6 9
B. 5 4 5 9
C. 7 8 8 9
D. 4 3 6 9
 
Answer: a
Explanation: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:
Code:
$ javac output.java
$ java output
6 4 6 9
 

Tìm kiếm

Back
Top