Java MCQ – Multiple Choice Questions and Answers – Strings – Part 2
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Strings”.
1. Which of the following constructors is used to create an empty String object?
A String()
B String(null)
C String(” “)
D None of the above
2. What is the output of the following code?
public class Q2 { public static void main(String []args) { char chars[] = {'b', 'o', 'b'}; String str = new String(chars); System.out.println(str); } }
A b
B o
C bob
D Error
3. What is the output of the following code?
public class Q3 { public static void main(String []args) { String str = "I LOVE JAVA"; System.out.println(str.charAt(4)); } }
A O
B V
C E
D L
4. What is the output of the following code?
public class Q4 { public static void main(String []args) { String str = "I LOVE JAVA"; System.out.println(str.length()); } }
A 11
B 10
C 12
D 9
5. What is the output of the following code?
public class Q5 { public static void main(String []args) { String str1 = "Emily"; String str2 = "Alex"; System.out.println(str1.charAt(0) > str2.charAt(0)); } }
A true
B false
C 0
D 1
6. What is the output of the following code?
public class Q6 { public static void main(String []args) { String a = "lorem"; int b = 8; System.out.println(a += b); } }
A 8lorem
B lorem8
C Compilation error
D Will throw an exception
7. The String method compareTo() returns _______.
A 1
B -1
C false
D true
E Int value
8. What is the output of the following code?
public class Q8 { public static void main(String []args) { String str = "hello"; System.out.println(str.substring(1, 4)); } }
A hel
B ell
C llo
D hell
9. What is the output of the following code?
public class Q9 { public static void main(String []args) { String str = "Hello"; System.out.println(str.indexOf('a')); } }
A 0
B -1
C false
D Compilation error
10. What is the output of the following code?
public class Q10 { public static void main(String []args) { String s1 = "Stack"; String s2 = "HowTo"; System.out.println(s1.concat(s2)); } }
A Stack
B HowTo
C StackHowTo
D Compilation error