Which of the following method call gives the position of the first occurrence of x in the string 51?

This section of our 1000+ Java MCQs focuses on searching and modifying a string of Java Programming Language.

1. Which of this method of class String is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

2. What will s2 contain after following lines of Java code?

 String s1 = "one";
String s2 = s1.concat("two")

a) one
b) two
c) onetwo
d) twoone
View Answer

Answer: c
Explanation: Two strings can be concatenated by using concat() method.

3. Which of these method of class String is used to remove leading and trailing whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()
View Answer

Answer: b
Explanation: None.

4. What is the value returned by function compareTo() if the invoking string is greater than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
View Answer

Answer: c
Explanation:

 if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.

5. Which of the following statement is correct?
a) replace() method replaces all occurrences of one character in invoking string with another character
b) replace() method replaces only first occurrences of a character in invoking string with another character
c) replace() method replaces all the characters in invoking string with another character
d) replace() replace() method replaces last occurrence of a character in invoking string with another character
View Answer

Answer: a
Explanation: replace() method replaces all occurrences of one character in invoking string with another character.

6. What will be the output of the following Java program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String c = "  Hello World  ";
  6.            String s = c.trim();
  7.            System.out.println("\""+s+"\"");
  8.         }
  9.     }

a) “”Hello World””
b) “”Hello World”
c) “Hello World”
d) Hello world
View Answer

Answer: c
Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
Output:

$ javac output.java
$ java output
"Hello World"

7. What will be the output of the following Java program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "one";
  6.            String s2 = s1 + " two";
  7.            System.out.println(s2);
  8.         }
  9.     }

a) one
b) two
c) one two
d) compilation error
View Answer

Answer: c
Explanation: None.
Output:

$ javac output.java
$ java output
one two

8. What will be the output of the following Java program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "Hello";
  6.            String s2 = s1.replace('l','w');
  7.            System.out.println(s2);
  8.         }
  9.     }

a) hello
b) helwo
c) hewlo
d) hewwo
View Answer

Answer: d
Explanation: replace() method replaces all occurrences of one character in invoking string with another character. s1.replace(‘l’,’w’) replaces every occurrence of ‘l’ in hello by ‘w’, giving hewwo.
Output:

$ javac output.java
$ java output
hewwo

9. What will be the output of the following Java program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            String s1 = "Hello World";
  6.            String s2 = s1.substring(0 , 4);
  7.            System.out.println(s2);
  8.         }
  9.    }

a) Hell
b) Hello
c) Worl
d) World
View Answer

Answer: a
Explanation: substring(0,4) returns the character from 0 th position to 3 rd position.
output:

$ javac output.java
$ java output 
Hell

10. What will be the output of the following Java program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {             String s = "Hello World";
  5.              int i = s.indexOf('o');
  6.              int j = s.lastIndexOf('l');
  7.              System.out.print(i + " " + j);
  8.  
  9.         }
  10.    }

a) 4 8
b) 5 9
c) 4 9
d) 5 8
View Answer

Answer: c
Explanation: indexOf() method returns the index of first occurrence of the character where as lastIndexOf() returns the index of last occurrence of the character.
output:

$ javac output.java
$ java output  
4 9

Sanfoundry Global Education & Learning Series – Java Programming Language.

Next Steps:

  • Get Free Certificate of Merit in Java Programming
  • Participate in Java Programming Certification Contest
  • Become a Top Ranker in Java Programming
  • Take Java Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Which of the following method call gives the position of the first occurrence of x in the string 51?

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Which of the following method call gives the position of the first occurrence of x in the string?

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 method call gives the position of the first occurrence of x in the string s1 A s1 indexOf x B s1 indexOf x N C s1 Index x 1 D s1 index x N?

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

What is the use of charAt () method Mcq?

Explanation: charAt() is a method of class String which gives the character specified by the index. obj.

Which of the following methods will create string in Java Mcq?

The intern() and toString() methods are of String class. Hence, the correct answer is option (c).