Topics Index

String Search

If you need explanation Read this topic

If you need Answer Take test on this topic

String Search
What will be the output of the following program?
class StringSearch {
    public static void main(String[] args)
        String s = "Banana Panama Vikrama Akram Dwakra";
        print(s.indexOf("na", s.indexOf("na")));
        print(s.lastIndexOf("akr", s.lastIndexOf("akr")));
        System.out.println(s.substring(s.indexOf("ik")-1, s.lastIndexOf("wa")-s.indexOf("Pana")-1));

    private static void print(int value)
        System.out.print(value + "~");

A. 2~4~9~30~-1~Vikrama
B. 2~4~9~30~30~Vikrama
C. 2~2~9~30~30~ikrama
D. 2~2~9~30~22~ikrama
E. None of the above
Topic: Java String substring() method - substring 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.

here option E
excution start from main in main we created a string  s = "Banana Panama Vikrama Akram Dwakra"; some  methods are given
1.print(s.indexOf("na")); s.indexOf()=>it Returns the index within this string of the first occurrence of the specified substring.
                      means we given "na" its will check from index 0 to end of string we get index 2
              after tha value will pass as argument to print function it will that value=>2~

2.print(s.indexOf("na", s.indexOf("na")));here we here we using indexof method with two arguments means first argument will give substring  to check index  and second will for check starting tha specfied number as index in string.
                             first we internal indexof method index  value  2 that  will be argument for outer index we get index is print(2)=>2~
3.print(s.lastIndexOf("na")); it has same has indexof method it will check from last index to first index it give =>print(30)=>9~

4.print(s.lastIndexOf("akr"));here "akr" there at index 30 in string =>its give index 30=>print(30)=>30~

5.print(s.lastIndexOf("akr", s.lastIndexOf("akr"))); lastindexof("",value) will check from last to first  and first argument is for substring toi check in string S and second argument number wil give where we have start checking index in  string S
                         here in index value is 30=> print(30)=>30~
6.System.out.println(s.substring(s.indexOf("ik")-1, s.lastIndexOf("wa")-s.indexOf("Pana")-1));=>
                    we using another string method called substring() a method with return a substring in given string  substring()
                        Ex=> s="abcdef" we using s.subString(2,5) means=>"cde"
but in substring method we some method s.substring(s.indexOf("ik")-1, s.lastIndexOf("wa")-s.indexOf("Pana")-1))=>s.substring(15-1,29-7-1)=>s.substring(14,21)=>it give output=>vikrama
here total result is=>2~2~9~30~30~Vikrama but this answer not there in option thats why option E.(none of the above)

Posted by Goutham Singarapu    2015-03-12 14:35:38

ANS is E

Posted by Mânïshå Mùlchåndânï    2015-03-12 17:41:45

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Goutham Singarapu,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Ramya Reddy Addulla. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-13 01:27:14

© meritcampus 2019

All Rights Reserved.

Open In App