This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What will be the output of the following code?
public class Demo {
public static void main(String[] args) {
String str = “Hello”;
str = str+”World”;
str = str.substring(5,10);
System.out.println(str);
}
}
Option D
The initial string str is assigned the value "Hello". Then, str is concatenated with "World" resulting in the string "HelloWorld". Next, the substring(5, 10) method extracts characters starting at index 5 to index 9 (10 is excluded).
The string str = "HelloWorld" has indices:
| Index | str[0] | str[1] | str[2] | str[3] | str[4] | str[5] | str[6] | str[7] | str[8] | str[9] |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | H | e | l | l | o | W | o | r | l | d |
So, in "HelloWorld", index 5 corresponds to the character 'W' and index 9 corresponds to 'd'. Thus, the substring "World" is extracted and printed.
Q: 2What will be the result of following Java code?
public class MyString {
public static void main(String[] args) {
String s1 = “Program”;
String s2 = “Program”;
System.out.println(s1==s2);
}
}
Option A
In Java, string literals are stored in a special memory area called the String Constant Pool (SCP). When we create two strings using literals like
Java checks the pool first. Since the literal "Program" already exists, s2 does not create a new object and both s1 and s2 point to the same memory location.
The expression s1 == s2 compares references, not string content. Because both variables refer to the same object in the pool, the result is true.
If the strings were created using new String("Program"), then == would return false because two different objects would be created.
Q: 3What is the output of below given code?
String s1 = new String("Nirmohi");
String s2 = new String("Nirmohi");
System.out.println(s1==s2);
Option B
In Java, the == operator compares object references, not the actual contents of String objects. In the given code, both strings are created explicitly using the new String() constructor.
Each use of new String("Nirmohi") creates a new and separate String object in memory. Therefore, s1 and s2 refer to two different objects, even though both objects contain the same text.
The objects contain the same characters, but their references are different. Therefore, s1==s2 evaluates to false.
E.g.:
class StringDemo
{
public static void main(String args[])
{
String s1 = new String("Nirmohi");
String s2 = new String("Nirmohi");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
OUTPUT
false
true
Q: 4Which method compares the contents of two strings?
Option D
In Java, the equals() method is used to compare the contents of two String objects. It checks whether both strings contain the same sequence of characters.
E.g.:
class StringDemo
{
public static void main(String args[])
{
String s1 = new String("Banjara");
String s2 = new String("Banjara");
System.out.println(s1.equals(s2));
}
}
OUTPUT
true
Although s1 and s2 are two different String objects, their contents are the same, so equals() returns true.
Note:
It is important to distinguish equals() from ==. The == operator compares the references of objects, whereas equals() compares the contents of String objects.
E.g.:
class StringDemo
{
public static void main(String args[])
{
String s1 = new String("Banjara");
String s2 = new String("Banjara");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
OUTPUT
false
true
Q: 5Which of the following methods will create string in Java?
(i) String S = “Hello Java”;
(ii) String S2 = New string(“Hello Java”);
Option A
In Java, strings can be created in two ways. The first method is by using string literals, for example: String s = "Hello Java";. In this case, the string is stored in the String Constant Pool (SCP), which allows Java to reuse memory efficiently.
The second method is by using the new keyword, for example: String s2 = new String("Hello Java");. This approach creates a new String object in heap memory, separate from the SCP, even if an identical string already exists in the pool.
However, in the given question, option (ii) is written as String S2 = New string("Hello Java");. This is incorrect due to Java’s case sensitivity:
Because of these errors, option (ii) will cause a Compile-Time Error (CTE), and only option (i) is valid as written.
Q: 6What will be the value of arr[2] in the following code?
String[] arr = new String[3];
arr[0] = “Maths”;
arr[1] = “Science”;
Option A
In the given code, a string array of size 3 is created using new String[3]. In Java, when an array of objects is created, all elements are automatically initialized to null unless explicitly assigned a value.
Here, only arr[0] and arr[1] are assigned the values "Maths" and "Science" respectively. The third element, arr[2], is never given any value. Therefore, it retains its default initialization, which is null.
Q: 7What is the output of below given code?
String s1 = "Banjara";
String s2 = "Banjara";
System.out.println(s1== s2);
Option A
In Java, a String is an object, and there are two important ways to compare strings, == and .equals().
The == operator checks whether two references point to the same object, whereas the .equals() method checks whether two strings contain the same sequence of characters.
In the given code, both "Banjara" values are string literals. Java stores string literals in a special area called the String Pool or String Constant Pool (SCP). When the same string literal is used again, Java can reuse the existing String object instead of creating another identical object.
Therefore, both s1 and s2 refer to the same pooled String object. So, the expression s1==s2 compares the two references and finds that they refer to the same object. Hence, it produces true.
E.g.: 1
class StringDemo
{
public static void main(String args[])
{
String s1 = "Banjara";
String s2 = "Banjara";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
OUTPUT
true
true
E.g.: 2
class StringDemo
{
public static void main(String args[])
{
String s1 = new String("Banjara");
String s2 = new String("Banjara");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
OUTPUT
false
true
| Comparison | What is Compared? |
|---|---|
| s1==s2 | Object References |
| s1.equals(s2) | String Contents |
Q: 8What is the output of below piece of code?
String str = "Banjara";
str.concat(" Nirmohi");
System.out.println(str);
Option B
In Java, String objects are immutable, which means that their contents cannot be changed after the String object is created. Methods such as concat() do not modify the original String. Instead, they create and return a new String object containing the combined text.
When str.concat(" Nirmohi"); statement is executed, a new String "Banjara Nirmohi" is created, but its returned value is not assigned to str. Therefore, str continues to refer to the original "Banjara" String.

Q: 9Which statement is correct?
Option C
In Java, String objects are immutable, which means that once a String object is created, its contents cannot be changed. If an operation appears to modify a String, Java actually creates a new String object instead of changing the existing one.
E.g.:
class StringDemo
{
public static void main(String args[])
{
String s = "Banjara";
s = s.concat(" Nirmohi");
System.out.println(s);
}
}
OUTPUT
Banjara Nirmohi
The original "Banjara" String is not modified. The concat() operation creates a new String containing "Banjara Nirmohi", and s is then made to refer to that new object.
In contrast, StringBuilder and StringBuffer are mutable. Their contents can be changed without creating a new object for every modification.
E.g.:
class StringBuilderDemo
{
public static void main(String args[])
{
StringBuilder s = new StringBuilder("Banjara");
s.append(" Nirmohi");
System.out.println(s);
}
}
OUTPUT
Banjara Nirmohi
Here, the existing StringBuilder object is modified.
Q: 10Consider the following class definition:
public class Test {
int var;
String myString;
public Test() {
this(0, “Default”); }
public Test(int var, String myString) {
this.var = var;
this.myString = myString;
}
}
What will the following code do?
Test test = new Test();
Option A
The class Test contains two constructors, a zero argument constructor and a parameterized constructor. When the zero argument constructor is called, it does not directly initialize the variables. Instead, it uses the keyword this(0, "Default") to explicitly call the parameterized constructor. This means the execution is transferred to the constructor Test(int var, String myString), which assigns the values var = 0 and myString = "Default" to the object.
As a result, the object is successfully created, and both instance variables receive valid initial values. Therefore, the object test is created with the expected initialized properties.
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.