Java MCQs 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? Answer: a 2. What will s2 contain after following lines of Java code? a) one Answer: c 3. Which of these method of class String is used to remove leading and trailing whitespaces? Answer: b 4. What is the value returned by function compareTo() if the invoking string is greater than the string compared? 5. Which of the following statement is correct? Answer: a 6. What will be the output of the following Java program? a) “”Hello World”” 7. What will be the output of the following Java program? a) one 8. What will be the output of the following Java program? a) hello 9. What will be the output of the following Java program? a) Hell 10. What will be the output of the following Java program? a) 4 8
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Clarification: None. String s1 = "one";
String s2 = s1.concat("two")
b) two
c) onetwo
d) twoone
Clarification: Two strings can be concatenated by using concat() method.
a) startsWith()
b) trim()
c) Trim()
d) doTrim()
Clarification: None.
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
Clarification:
if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.
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
Clarification: replace() method replaces all occurrences of one character in invoking string with another character.
class output
{
public static void main(String args[])
{
String c = " Hello World ";
String s = c.trim();
System.out.println("""+s+""");
}
}
b) “”Hello World”
c) “Hello World”
d) Hello world
Clarification: trim() method is used to remove leading and trailing whitespaces in a string.
Output:
$ javac output.java
$ java output
"Hello World"
class output
{
public static void main(String args[])
{
String s1 = "one";
String s2 = s1 + " two";
System.out.println(s2);
}
}
b) two
c) one two
d) compilation error
Clarification: None.
Output:
$ javac output.java
$ java output
one two
class output
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}
b) helwo
c) hewlo
d) hewwo
Clarification: 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
class output
{
public static void main(String args[])
{
String s1 = "Hello World";
String s2 = s1.substring(0 , 4);
System.out.println(s2);
}
}
b) Hello
c) Worl
d) World
Clarification: substring(0,4) returns the character from 0 th position to 3 rd position.
output:
$ javac output.java
$ java output
Hell
class output
{
public static void main(String args[])
{ String s = "Hello World";
int i = s.indexOf('o');
int j = s.lastIndexOf('l');
System.out.print(i + " " + j);
}
}
b) 5 9
c) 4 9
d) 5 8
Clarification: 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