NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
DATA STRUCTURES AND ALGORITHMS USING JAVA
Assignment 12
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10×1 = 10
______________________________________________________________________________
QUESTION 1:
You are given the following code.
public class Test {
public static void main(String[] args) {
String a = "hello";
String b = new String("hello");
System.out.println(a == b);
}
}
What does the program print?
a. true
b. false
c. Compilation error
d. Runtime error
Correct Answer: b
Detailed Solution:
a refers to a string in the String pool, while b is a new object in heap memory. Hence, == compares
references and returns false.
______________________________________________________________________________
QUESTION 2:
Which of the following statements is true regarding the String class?
a. String is a primitive type in Java and does not belong to any package
b. String objects are mutable and defined in the java.util package
c. Java defines the String class in the java.lang package, allowing creation and manipulation
of string objects
d. String is only used for input/output and cannot be manipulated programmatically
Correct Answer: c
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The String class is part of the java.lang package, which is automatically imported. It allows the creation of
string literals and manipulation using methods like .concat(), .substring(), .replace(), etc. It is not
primitive and not mutable.
______________________________________________________________________________
QUESTION 3:
Which of the following Java statements is valid for creating a string literal?
a. String s = 'Java';
b. String s = "Java";
c. String s = Java;
d. String s = <Java>;
Correct Answer: b
Detailed Solution:
Java requires string literals to be enclosed in double quotes. Statement B is valid.
______________________________________________________________________________
QUESTION 4:
You are given the following code.
public class Test {
public static void main(String[] args) {
String s = "Hello";
s.toLowerCase();
System.out.println(s);
}
}
What does the program print?
a. hello
b. HELLO
c. Hello
d. Error
Correct Answer: c
Detailed Solution:
String is immutable; toLowerCase() returns a new string. Since it's not stored, s remains "Hello".
______________________________________________________________________________
QUESTION 5:
Consider the following code.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
public class Test {
public static void main(String[] args) {
String s = "abcabc";
s = s.replace('a', 'x').concat("y");
System.out.println(s);
}
}
What does the program print?
a. xbcxby
b. abcabc
c. xbcabcy
d. xbcxbcy
Correct Answer: d
Detailed Solution:
replace('a','x') → "xbcxbc", then concat("y") → "xbcxbcy"
______________________________________________________________________________
QUESTION 6:
You are given the following code.
public class Test {
public static void main(String[] args) {
String text = "DataStructuresAndAlgorithms";
String keyword = "Algo";
System.out.println(text.contains(keyword) &&
text.indexOf(keyword) > 10);
}
}
What does the program print?
a. true
b. false
c. Compilation error
d. Exception at runtime
Correct Answer: a
Detailed Solution:
text.contains("Algo") returns true because "Algorithms" contains "Algo".
"Algo" starts at index 18, so text.indexOf("Algo") > 10 is also true.
Hence, the combined condition is true.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Thus, option A is correct
______________________________________________________________________________
QUESTION 7:
Which of the following methods is used in Java to convert a String to a primitive data type
(e.g., int, double)?
a. String.valueOf()
b. Integer.parseInt()
c. String.toPrimitive()
d. parse.String()
Correct Answer: b
Detailed Solution:
Java provides wrapper class methods like Integer.parseInt(String) and
Double.parseDouble(String) to convert strings into primitive types.
______________________________________________________________________________
QUESTION 8:
You are given the following code.
public class Test {
public static void main(String[] args) {
String input = "level";
StringBuffer sb = new StringBuffer(input);
System.out.println(input.equals(sb.reverse().toString()));
}
}
What does the program print?
a. Reverses the string
b. Checks if the string is a palindrome
c. Compares two different strings
d. Converts string to uppercase
Correct Answer: b
Detailed Solution:
The code creates a StringBuffer from the input string, reverses it, and compares the reversed version with
the original using .equals(). This effectively checks if the string is a palindrome, though the word
"palindrome" is never mentioned in the code — making the intent non-obvious at first glance.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Which of the following statements about the StringBuffer class in Java is true?
a. StringBuffer is immutable like String
b. StringBuffer is not thread-safe
c. StringBuffer allows modification of strings and is synchronized
d. StringBuffer belongs to the java.io package
Correct Answer: c
Detailed Solution:
StringBuffer is a mutable, thread-safe class in the java.lang package used for modifying character
sequences. Unlike String, it allows in-place changes using methods like append(), insert(), and
reverse(). Its synchronization makes it suitable for multi-threaded use, though it may be slower
than StringBuilder in single-threaded scenarios.
______________________________________________________________________________
QUESTION 10:
Consider the following code.
public class Test {
public static void main(String[] args) {
String s = "banana";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.indexOf(s.charAt(i)) == i) {
count++;
}
}
System.out.println(count);
}
}
What will be the output of the above code?
a. 3
b. 6
c. 1
d. 0
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: a
Detailed Solution:
The code counts the number of unique characters by checking if the current index is the first occurrence of
that character using s.indexOf(char) == i.
For "banana":
'b' → index 0
'a' → index 1
'n' → index 2
Subsequent duplicates are skipped → count = 3.
______________________________________________________________________________
************END************