Java MCQ – Multiple Choice Questions and Answers – Strings – Part 3
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Strings”.
1. What is the output of the following code?
public class Main{ public static void main(String args[]){ String str = "Welcome to StackHowTo"; System.out.println(str.startsWith("welcome")); } }
A true
B false
C 0
D 1
2. Which of the following classes can be used to create a mutable String?
A String()
B StringBuffer()
C Both A and B are true.
D None of the above
3. Which of these methods of StringBuffer class is used to concatenate a String at the end of another String?
A concat()
B append()
C join()
D concatenate()
5. Which of these methods of the String class is used to remove leading and trailing spaces?
A startsWith()
B empty()
C Trim()
D trim()
6. What is the output of the following code?
public class Main{ public static void main(String args[]){ String s1 = "s1"; String s2 = s1.concat("s2"); System.out.println(s2); } }
A s1
B s2
C s1s2
D s1s1
7. Which of these methods is used to extract a substring from a string?
A substring()
B Substring()
C SubString()
D None of the above
8. What is the output of the following code?
public class Main{ public static void main(String args[]){ String str = "StackHowTo"; System.out.println(str.substring(3, 5)); } }
A Sta
B ckH
C ck
D HowTo
9. Which of the following statements is correct?
A replace() method replaces all occurrences of a character in a string with another character.
B replace() method replaces only the first occurrences of a character in a string with another character.
C replace() method replaces all characters in a string with another character.
D replace() method replaces the last occurrence of a character in a string with another character.
10. The method compareTo() returns _______
A 1
B -1
C true
D false
E An integer value