Java MCQ – Multiple Choice Questions and Answers – Strings – Part 4
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Strings”.
1. String class belongs to __________ package
A java.awt
B java.lang
C java.applet
D java.string
2. How many constructors in the String class?
A 1
B 2
C 13
D 11
3. Which of the following methods of the “StringBuffer” class is used to find the length of a String?
A length()
B Length()
C Capacity()
D capacity()
4. What is the output of the following code?
public class Main{
public static void main(String args[]){
String str = "Bob";
System.out.println(str.indexOf('s'));
}
}
A true
B false
C 0
D 1
E -1
5. What is the output of the following code?
public class Main{
public static void main(String args[]){
StringBuffer s = new StringBuffer("Bob");
s.deleteCharAt(0);
System.out.println(s);
}
}
A Bo
B ob
C Bob
D B
6. Which of the following statements is correct?
A reverse() method reverses all characters.
B reverseall() method reverses all characters.
C replace() method replaces the first occurrence of a character in a string with another character.
D replace() method replaces the last occurrence of a character in a string with another character.
7. Which of the following is an incorrect form of StringBuffer constructor?
A StringBuffer()
B StringBuffer(int size)
C StringBuffer(String str)
D StringBuffer(int size , String str)
8. What is the output of the following code?
public class Main{
public static void main(String args[]){
String str = "toto".replace('t', 's');
System.out.println(str);
}
}
A The first occurrence of ‘t’ is replaced by ‘s’.
B All characters ‘t’ are replaced by ‘s’.
C All characters ‘s’ are replaced by ‘t’.
D None of the above
9. What is the output of the following code?
public class Main{
public static void main (String[] args){
String str = "x1y2z3";
String[] arr = str.split("\\d");
for(String s: arr)
System.out.print(s);
}
}
A x1y2z3
B 123
C xyz
D None of the above
10. What is the output of the following code?
public class Main{
public static void main(String args[]){
String s = null;
if(s == null){
System.out.print("A");
}
else if(s.length() == 0){
System.out.print("B");
}
else{
System.out.print("C");
}
}
}
A A
B B
C C
D ABC


