Menu
Topics Index
...
`

Index


If you need explanation Read this topic

If you need Answer Take test on this topic

Index
What will be the output of following program?
public class Index {
    public static void main(String args[]) {
        String s = "Below method shows how to get index of a specified character or string from the given string.";
        int i = s.indexOf('s');
        int k = s.indexOf("given");
        System.out.println("First occurence of Char 's'=" + i);
        System.out.println("First occurence of String \"given\"=" + k);
    }
}

A. First occurence of Char 's'=13
First occurence of String "given"=80 
B. First occurence of Char 's'=14
First occurence of String \"given\"=79
C. First occurence of Char 's'=13
First occurence of String "given"=81
D. Compilation Error or Runtime Error
Topic: Java Searching Strings - Java indexOf, lastIndexOf Methods

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.

Outuput: option 'A' First occurence of char 's'=13 ,First occurrence of string "given"=80 because String s="Below method shows how to get index of a specified character or string from the given string"; s is the object of string and paragraph is written in it and that object is assigned to s.index('s') in which characters are read and s.indexOf("given") string is read and given as output and In sop('s'="i) display no of occurence of char and sop(\given\"="+k); displays no of occurence in string

Posted by Sivani Reddy Kakuru    2014-05-23 08:58:04


Ans: B, the indexOf ( ) method is return the index value of a particular character in a string, we declare int datatype for "i " so it returns integer value,  The index Of ("s") returns 13, as well as indexOf ("given") returns 79.

Posted by Damu Surya    2014-05-23 09:03:04


Ans:option A as it prints First oocurence of char as 13 it is assigned to indexOf and First oocurence string given is 80 i and k are declared and concatenation is done +i and +k and prints the result .

Posted by Vijaya Reddy    2014-05-23 10:56:59


Congratulations Sivani Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-26 12:43:56


© meritcampus 2019

All Rights Reserved.

Open In App