In this post, we have provided Java String multiple-choice questions to test your knowledge about String in Java.
Learn Java String at https://www.javaguides.net/p/java-string-api-guide.html
We would suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
The answer and explanation of each question have given at end of this post.
Q1 - Consider the following program:
public class StrEqual {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
if (s1 == s2) {
System.out.println("s1 and s2 equal");
} else {
System.out.println("s1 and s2 not equal");
}
if (s1 == s3) {
System.out.println("s1 and s3 equal");
} else {
System.out.println("s1 and s3 not equal");
}
}
}
s1 and s2 equal
s1 and s3 equal
s1 and s2 equal
s1 and s3 not equal
s1 and s2 not equal
s1 and s3 equal
s1 and s2 not equal
s1 and s3 not equal
Q2 - Consider the following program:
public class Test {
public static void main(String[] args) {
String str = null;
System.out.println(str.valueOf(10));
}
}
Q3 - Consider the following program and predict the output:
public class Test {
public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}
Q4 - Consider the following program and predict its output:
public class Test {
public static void main(String[] args) {
String str = null;
switch (str) { // #1
case "null":
System.out.println("null string"); // #2
break;
}
}
}
Q5 - What will be the output of the below statements?
public class Test {
public static void main(String[] args) {
String s1 = null;
System.out.println(s1); //line 2
System.out.println(s1.toString()); //line 3
}
}
Q6 - select all the classes that extend the String class.
Q7 - What is the output of the following program?
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
Q8 - What will be the output of the below statements?
public class Test {
public static void main(String[] args) {
String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));
}
}
Q9 - What is the result of the following code?
String s1 = "Java";
String s2 = "Java";
StringBuilder sb1 = new StringBuilder();
sb1.append("Ja").append("va");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(sb1.toString() == s1);
System.out.println(sb1.toString().equals(s1));
A. true is printed out exactly once.
B. true is printed out exactly twice.
C. true is printed out exactly three times.
D. true is printed out exactly four times.
E. The code does not compile.
Answers
Q1
s1 and s2 not equal
s1 and s3 equal
Q2
c) This program will print 10 in the console.
Q3
c) 115110
Q4
c) This program results in throwing a NullPointerException.
Q5
b) null NullPointerException
if (s == null) {
s = "null";
}
Q6
d) None
Q7
b) true
Q8
a) false
Q9
C. true is printed out exactly three times.
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course