MCQ : Introduction to Java Strings
Multiple choice question based on Java program on string used in java along with its answer and explanation in detail
PROGRAMMING
3/25/20244 min read
Introduction to Java Strings
In Java, a string is a sequence of characters. It is one of the most commonly used data types and is extensively used in Java programs. Strings are immutable, which means they cannot be changed once created. In this blog post, we will cover some multiple-choice questions based on Java programs that involve string manipulation.
Question 1:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello";
str.concat(" World");
System.out.println(str);
}
}
A) Hello
B) World
C) Hello World
D) Compilation Error
Answer: A) Hello
Explanation: In Java, strings are immutable. The concat() method returns a new string that is the concatenation of the original string and the specified string. However, the original string remains unchanged. Therefore, the output of the above program is "Hello".
Question 2:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Error
Answer: A) true
Explanation: In Java, string literals are interned, which means that multiple string literals with the same value will refer to the same memory location. Therefore, str1 and str2 both refer to the same "Hello" string literal, and the == operator compares the memory addresses, which are the same. Hence, the output is true.
Question 3:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Error
Answer: B) false
Explanation: The new keyword creates a new string object in the heap memory, even if a string with the same value already exists in the string pool. Therefore, str1 and str2 refer to different memory locations, and the == operator compares the memory addresses, which are different. Hence, the output is false.
Question 4:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1.equals(str2));
}
}
A) true
B) false
C) Compilation Error
D) Runtime Error
Answer: A) true
Explanation: The equals() method compares the content of the strings, not the memory addresses. Therefore, even though str1 and str2 refer to different memory locations, the content of both strings is the same ("Hello"). Hence, the output is true.
Question 5:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello";
str = str.toUpperCase();
System.out.println(str);
}
}
A) Hello
B) HELLO
C) Compilation Error
D) Runtime Error
Answer: B) HELLO
Explanation: The toUpperCase() method converts all characters in the string to uppercase and returns a new string. In the above program, the original string "Hello" is converted to "HELLO" and assigned back to the variable str. Therefore, the output is "HELLO".
Question 6:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello";
str = str.toLowerCase();
System.out.println(str);
}
}
A) Hello
B) HELLO
C) hello
D) Compilation Error
Answer: C) hello
Explanation: The toLowerCase() method converts all characters in the string to lowercase and returns a new string. In the above program, the original string "Hello" is converted to "hello" and assigned back to the variable str. Therefore, the output is "hello".
Question 7:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.length());
}
}
A) 10
B) 11
C) 12
D) Compilation Error
Answer: C) 12
Explanation: The length() method returns the number of characters in the string. In the above program, the string "Hello World" has 12 characters, including the space. Therefore, the output is 12.
Question 8:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.substring(6));
}
}
A) Hello World
B) World
C) Compilation Error
D) Runtime Error
Answer: B) World
Explanation: The substring() method returns a new string that is a substring of the original string. In the above program, the substring starts from the 6th index (inclusive) and includes all the characters until the end of the string. Therefore, the output is "World".
Question 9:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.substring(0, 5));
}
}
A) Hello
B) World
C) Hello
D) Compilation Error
Answer: A) Hello
Explanation: The substring() method can also take two parameters: the starting index (inclusive) and the ending index (exclusive). In the above program, the substring starts from the 0th index (inclusive) and includes all the characters until the 5th index (exclusive). Therefore, the output is "Hello".
Question 10:
What is the output of the following Java program?
public class StringExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.replace("o", "a"));
}
}
A) Hello World
B) Hella Warld
C) Hella World
D) Compilation Error
Answer: C) Hella World
Explanation: The replace() method replaces all occurrences of a specified character or sequence of characters with another character or sequence of characters. In the above program, all occurrences of the letter "o" are replaced with the letter "a". Therefore, the output is "Hella World".
Conclusion
In this blog post, we covered some multiple-choice questions based on Java programs that involve string manipulation. Understanding how strings work in Java and knowing the various methods available for string manipulation is essential for developing Java programs that deal with textual data.
Contact
contact@howtodoworld.com