🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Learn and Master Java Programming: Learn Java Programming with Examples
Learn everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
The answer and explanation of each question have given at the 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));
}
}
a) This program will result in a compiler error.
b) This program will throw a NullPointerException.
c) This program will print 10 in the console.
d) This program will print null in the console.
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);
}
}
a) 11511c) 115110
d) 27
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;
}
}
}
b) This program results in a compiler error in statement #2.
c) This program results in throwing a NullPointerException.
d) This program prints the following: null string.
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
}
}
b) null NullPointerException
c) NullPointerException NullPointerException
d) None
Q6 - 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);
}
}
b) true
c) None
Q7 - 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));
}
}
b) true
c) ClassCastException at runtime
d) Compile-time error
Q8 - What is the output of the following code snippet?
public class Main {
public static void main(String[] args) {
String str = "Hello";
str += " World!";
System.out.println(str.length());
}
}b) 11
c) 12
d) Compile-time error
Q9 - What is the output of the following code snippet?
public class Main {
public static void main(String[] args) {
String str = "Java";
str.concat(" Programming");
System.out.println(str);
}
}b) Java Programming
c) Programming
d) Compile-time error
Q10 - What is the output of the following code snippet?
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println(str.indexOf("o"));
}
}b) 5
c) 7
d) -1
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
b) true
Q7
a) false
Q8
Q9
Answer:a) Java
The concat() method returns a new string resulting from concatenating the specified string to the original string. However, in this code, the result of concat() is not assigned back to the str variable. Therefore, the original string "Java" remains unchanged, and the output is "Java".
Q10
Conclusion
Learn and Master Java Programming: Learn Java Programming with Examples
Learn everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment