377). 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
Show Answer
Correct Answer: substring()
Notes:
Answer: a
Explanation: None.
378). 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
Show Answer
Correct Answer: onetwo
Notes:
Answer: c
Explanation: Two strings can be concatenated by using concat() method.
379). Which of these method of class String is used to remove leading and trailing whitespaces?
String s1 = "one";
String s2 = s1.concat("two")
[A]startsWith()
[B]trim()
[C]Trim()
[D]doTrim()
Show Answer
Correct Answer: trim()
Notes:
Answer: b
Explanation: None.
380). What is the value returned by function compareTo() if the invoking string is greater than the string compared?
String s1 = "one";
String s2 = s1.concat("two")
[A]zero
[B]value less than zero
[C]value greater than zero
[D] none of the mentioned
Show Answer
Correct Answer: value greater than zero
Notes:
Answer: c
Explanation:
if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.
381). Which of the following statement is correct?
String s1 = "one";
String s2 = s1.concat("two")
[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
Show Answer
Correct Answer: replace() method replaces all occurrences of one character in invoking string with another character
Notes:
Answer: a
Explanation: replace() method replaces all occurrences of one character in invoking string with another character.
382). What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String c = " Hello World ";
String s = c.trim();
System.out.println("\""+s+"\"");
}
}
[A]“”Hello World””
[B]“”Hello World”
[C]“Hello World”
[D]Hello world
Show Answer
Correct Answer: “Hello World”
Notes:
Answer: c
Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
Output:
$ javac output.java
$ java output
"Hello World"
383). What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String s1 = "one";
String s2 = s1 + " two";
System.out.println(s2);
}
}
[A]one
[B]two
[C]one two
[D]compilation error
Show Answer
Correct Answer: one two
Notes:
Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
one two
384). What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}
[A]hello
[B]helwo
[C]hewlo
[D]hewwo
Show Answer
Correct Answer: hewwo
Notes:
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
385). What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String s1 = "Hello World";
String s2 = s1.substring(0 , 4);
System.out.println(s2);
}
}
[A]Hell
[B]Hello
[C]Worl
[D]World
Show Answer
Correct Answer: Hell
Notes:
Answer: a
Explanation: substring(0,4) returns the character from 0 th position to 3 rd position.
output:
$ javac output.java
$ java output
Hell
386). What will be the output of the following Java program?
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);
}
}
[A]4 8
[B]5 9
[C]4 9
[D]5 8
Show Answer
Correct Answer: 4 9
Notes:
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