📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. What will be the output of the below 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");
}
}
}
Answer
s1 and s2 not equal
s1 and s3 equal
JVM sets a constant pool in which it stores all the string constants used in the type. If two references are declared with a constant, then both refer to the same constant object.2. What will be the output of the below program?
public class Test {
public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}
Answer
3. What will be the output of the below program?
public class Test { public static void main(String[] args) { String str = null; System.out.println(str.valueOf(10)); } }
This program will print 10 in the console.
4. 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));
}
}
Answer
false
5. What is the output of the below program?
public class A {
public static void main(String[] args) {
String s1 = new String("javaguides");
String s2 = new String("javaguides");
System.out.println(s1 = s2);
}
}
6. 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);
}
}
Answer
true
7. How many objects will be created in the following code and where they will be stored in the memory?
String s1 = "javaguides";
String s2 = "javaguides";
Answer
Only one object will be created and this object will be stored in the string constant pool.
8. How many objects will be created in the following code and where they will be stored?
String s1 = new String("javaguides");
String s2 = "javaguides";
Answer
Here, two string objects will be created. An object created using a new operator(s1) will be stored in the heap memory. The object created using a string literal(s2) is stored in the string constant pool.
9. What is the output of the below code snippet?
String s1 = new String("javaguides");
String s2 = new String("javaguides");
System.out.println(s1 == s2);
10. What is the output of the below code snippet?
String s1 = "javaguides"
String s2 = "javaguides";
System.out.println(s1 == s2);
Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Comments
Post a Comment
Leave Comment