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: 3Which 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: 4What 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: 5Consider 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.