Which of the following method call gives the position of X that occurs after n th position in the string S1?

What is indexOf() Method in Java?

indexOf() Method is used to get index of the first occurrence of a criteria specified in the parameters of the IndexOf method.

A common scenario can be when a system admin wants to find the index of the ‘@’ character of the email Id of a client and then wants to get the remaining substring. In that situation, IndexOf method can be used.

Java String indexOf() Syntax

The syntax of Java String indexOf() Method is:

public int indexOf(int cha)

Java indexOf() Parameters

Below are the indexOf() Java String parameters:

char – Used to represent a single character value

str – Used to represent the string to search for

fromIndex – Used to represent the index position to start the search from

Java String indexOf() Return Value

This indexOf() Java String method returns the index within this string of the first occurrence of the specified character. It returns -1 if the character does not occur.

The Java String IndexOf method has four overloads. All the overloads return an integer type value, representing the returned index. These overloads differ in the type and number of parameters they accept.

IndexOf(char b)

This method returns the index of the character ‘b’ passed as parameter. If that character is not available in the string, the returned index would be -1.

IndexOf(char c, int startindex)

The given method would return the index of the first occurrence of character ‘c’ after the integer index passed as second parameter “startindex.” All the occurrences of character ‘c’ before the “startindex” integer index would be ignored.

IndexOf(String substring)

The above Java substring indexOf() method returns the index of the first character of the substring passed as a parameter to it. If that substring is not available in the string, the returned index would be -1.

IndexOf(String substring, int startindex)

This Java substring indexOf() method returns the index of the first character in the substring passed as the first parameter, after the “startindex” index value. If substring starts from the passed integer value of “startindex”, that substring would be ignored.

public class Sample_String {
    public static void main(String args[]) {

        String str_Sample = "This is Index of Example";
        //Character at position
        System.out.println("Index of character 'x': " + str_Sample.indexOf('x'));
        //Character at position after given index value
        System.out.println("Index of character 's' after 3 index: " + str_Sample.indexOf('s', 3));
        //Give index position for the given substring
        System.out.println("Index of substring 'is': " + str_Sample.indexOf("is"));
        //Give index position for the given substring and start index
        System.out.println("Index of substring 'is' form index:" + str_Sample.indexOf("is", 5));
    }
}

Output:

Index of character ‘x’: 12
Index of character ‘s’ after 3 index: 3
Index of substring ‘is’: 2
Index of substring ‘is’ form index:5

    11-9-1: What is printed by the following statements?

    s = "python rocks"
    print(s[1] * s.index("n"))
    

  • yyyyy
  • Correct! s[1] = y and the index of n is 5, so y * 5 prints 5 y characters.
  • 55555
  • Incorrect! s.index("n") = 5, but it is multiplying something else. Try again.
  • y5
  • Incorrect! The print statement contains multiplication. Try again.
  • TypeError
  • Incorrect! Multiplying a string by an int is allowed. Try again.

    11-9-2: What will be printed when the following executes?

    str = "His shirt is red"
    pos = str.find("is")
    print(pos)
    

  • 1
  • Correct! The find function returns the index of the first position that contains the given string.
  • 9
  • Incorrect! The find function returns the index of the FIRST position that contains the given string. Try again.
  • 2
  • Incorrect! Remember, indices start at 0, not 1. Try again.
  • pos
  • Incorrect! pos is a variable, so print will print its value. Try again.

    11-9-3: What will be printed when the following executes?

    str = "This is fun"
    str = str[5]
    print(str)
    

  • i
  • Correct! This will print the character at position 5 in the string, which is i.
  • ' '
  • Incorrect! Remember, indices start at 0. Try again.
  • is fun
  • Incorrect! str[5] equals one specific character of the larger string. Try again.
  • This is fun
  • Incorrect! Line 2 permanently changes the value that the variable str is assigned to. Try again.

    11-9-4: What is the value of s1 after the following code executes?

    s1 = "heY"
    s1 = s1.capitalize()
    s1.lower()
    

  • heY
  • Incorrect! Although strings are immutable, s1 gets reassigned to a different value than it begins with. Try again.
  • hey
  • Incorrect! Strings are immutable, so line 3 just returns a new string and doesn't modify the original. Try again.
  • HEY
  • Incorrect! The capitalize method doesn't capitalize the entire word. Try again.
  • Hey
  • Correct! The capitalize method capitalizes the first letter of the word and lowercases the rest. Then, line 3 returns a new string without modifying the original.

    11-9-5: What would the following code print?

    Mali = 5
    print("Mali" + " is " + str(Mali))
    

  • Mali is Mali
  • Incorrect! There are no quotes around the last Mali, so str() will use the value of the variable Mali. Try again.
  • Mali is 5
  • Correct! The first Mali is in quotes, so it will print the string "Mali". The second Mali is not in quotes, so it will print the value of the variable Mali.
  • 5 is Mali
  • Incorrect! The first Mali is in quotes and the second is not. Try again.
  • 5 is 5
  • Incorrect! The first Mali is in quotes, so it is a string, not a variable. Try again.

    11-9-6: What is printed by the following statements?

    s = "python rocks"
    print(s[3])
    

  • t
  • Incorrect! Indices start at 0, not 1. Try again.
  • h
  • Correct! Indices start with 0.
  • c
  • Incorrect! s[-3] would count from right to left and return c. Try again.
  • Error, you cannot use the [ ] operator with a string.
  • Incorrect! [ ] is the index operator and works with strings. Try again.

    11-9-7: What is printed by the following statements?

    s = "python is awesome"
    print(s[2] + s[-5])
    

  • te
  • Correct! The indexing operator has precedence over concatenation.
  • tw
  • Incorrect! s[-1] is the last character of the string, so what is s[-5]? Try again.
  • o
  • Incorrect! The indexing operator happens before the two strings are concatenated. Try again.
  • Error, you cannot use the [ ] operator with the + operator.
  • Incorrect! [ ] operator returns a string that can be concatenated with another string. Try again.

    11-9-8: What is printed by the following statements?

    s = "python rocks"
    print(len(s))
    

  • 11
  • Incorrect! The space counts as a character. Try again.
  • 12
  • Correct! len() returns the number of characters in the string, including spaces.
  • 1
  • Incorrect! s is a variable, not a character. Try again.
  • Error, missing quotes around s
  • Incorrect! Because s is the name of a string variable, len() can be used on it. Try again.

    11-9-9: What is printed by the following statements:

    s = "Rose"
    s[1] = "i"
    print(s)
    

  • Rose
  • Incorrect! Assignment is not allowed with strings. Try again.
  • Rise
  • Incorrect! Assignment is not allowed with strings. Try again.
  • s
  • Incorrect! Assignment is not allowed with strings. Try again.
  • TypeError
  • Correct! Strings are immutable, so you cannot change an existing string.

    11-9-10: What is printed by the following statements:

    s = "ball"
    r = ""
    for item in s:
        r = item.upper() + r
    print(r)
    

  • Ball
  • Incorrect! Each item is converted to upper case before concatenation. Try again.
  • BALL
  • Incorrect! Pay close attention to the order the characters will be in. Try again.
  • LLAB
  • Correct! The order is reversed due to the order of the concatenation.
  • TypeError
  • Incorrect! String concatenation is allowed. Try again.

    11-9-11: What is printed by the following statements?

    s = "python rocks"
    print(s[7:11] * 3)
    

  • rockrockrock
  • Correct! s[7:11] = "rock", which is then repeated 3 times.
  • rock rock rock
  • Incorrect! s[7:11] is "rock", not " rock". Try again.
  • rocksrocksrocks
  • Incorrect! Slicing will not include the character at index 11, just the characters up to it. Try again.
  • TypeError, you cannot use multiplication with slicing.
  • Incorrect! Multiplying a string by an int is okay. Try again.

    11-9-12: What is printed by the following statements?

    animal = "dog"
    print("animal" + animal)
    

  • dogdog
  • It will print "animal" first.
  • dog dog
  • There is no added space.
  • animal dog
  • There is no added space.
  • animaldog
  • Correct! It will print the characters in the string followed by the value of the variable animal without any space between.

    11-9-13: What is printed by the following statements?

    animal = "dog"
    print("animal " + "animal")
    

  • animal animal
  • Since animal is in quotes it will print those exact letters.
  • animalanimal
  • There is a space in the first string.
  • animal dog
  • Notice that the second animal is also in quotes.
  • animaldog
  • Notice that the second animal is also in quotes and there is a space after the first string.

You have attempted of activities on this page

Which of the following method call gives the position of X that occurs after n th position in the string S1 S1 Index (' x 1 S1 Index (' x N?

S1. indexOf('X', n)

Which of the following method call gives the position of the Firstoccurrence of x in the string S1?

The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.

Which of the following functions is used to find the position of the particular substring within a string?

The FIND function in Excel is used to return the position of a specific character or substring within a text string.

Which of the following is true about string?

A string does not let the user type letters numbers or words as it has already been written. Hence, it is the correct option.