NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
DATA STRUCTURES AND ALGORITHMS USING JAVA
Assignment 12
TYPE OF QUESTION: MCQ
Miscellaneous Utilities
______________________________________________________________________________
QUESTION 1:
What is the purpose of the method contains(CharSequence s) in String class?
a. Compares this string to the specified CharSequence.
b. Compares this string to the specified StringBuffer.
c. Returns true if and only if this string contains the specified sequence of char values.
d. Compares this string to the specified object.
Correct Answer: c
Detailed Solution:
The method returns true if and only if this string contains the specified sequence of char values.
For more details, refer methods of the String class.
____________________________________________________________________________
QUESTION 2:
What is the purpose of the method indexOf(int ch, int fromIndex) of String class?
a. Returns the index within this string of the first occurrence of the specified character.
b. Returns the index within this string of the first occurrence of the specified character,
starting the search at the specified index.
c. Returns the index within this string of the first occurrence of the specified substring.
d. Returns the index within this string of the first occurrence of the specified substring,
starting at the specified index.
Correct Answer: b
Detailed Solution:
This method Returns the index within this string of the first occurrence of the specified character,
starting the search at the specified index. For more details, refer methods of the String class.
___________________________________________________________________________
QUESTION 3:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Consider the following code snippet?
class Demo{
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
System.out.print(org.indexOf(search));
}
}
What will be the output of the following code snippet?
a. 4
b. 2
c. 3
d. Throws an exception
Correct Answer: b
Detailed Solution:
Search string starts at index 2.
_________________________________________________________________________
QUESTION 4:
Which of the following statement(s) is/are true?
a. String class is immutable.
b. String is slow and StringBuffer is fast.
c. StringBuffer consumes more memory when you concat strings.
d. StringBuffer is immutable.
Correct Answer: a and b
Detailed Solution:
String StringBuffer
String class is immutable. StringBuffer class is mutable.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
String is slow and consumes more memory when you StringBuffer is fast and consumes less memory when
concat too many strings because every time it creates you concat strings.
new instance.
_________________________________________________________________________
QUESTION 5:
Consider the following code snippet?
class Demo{
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
System.out.print(sb.length() + ",");
System.out.print(sb.capacity());
}
}
What will be the output of the following code snippet?
a. 5, 21
b. 21, 5
c. Throws an exception
d. Run time error
Correct Answer: a
Detailed Solution:
Since sb is initialized with the string "Hello" when it is created, its length is 5. Its capacity is 21
because room for 16 additional characters is automatically added.
________________________________________________________________________
QUESTION 6:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Consider the following code snippet?
class Main {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println(sb);
}
}
What will be the output of the following code snippet?
a. This was a test.
b. This is a test.
c. This was is a test.
d. This is was a test.
Correct Answer: a
Detailed Solution:
We can replace one set of characters with another set inside a StringBuffer object by calling
replace( ).
___________________________________________________________________________
QUESTION 7:
Which of the following are the correct implementation(s) of the Date class constructor?
a. Date()
b. Date(long date)
c. Date(int year, int month, int date)
d. Date(String s)
Correct Answer: a, b, c, and d
Detailed Solution:
Constructors defined by class Date
Constructor Description
Date() Creates date object representing current date and time.
Date(long date) Allocates a Date object and initializes it to represent the specified number
of milliseconds since the standard base time known as "the epoch", namely
January 1, 1970, 00:00:00 GMT.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Date(int year, int month, int date) Create a date with the specific data as yyyy/mm/dd.
Date(int year, int month, int date, Create a date with the specific data with time.
int hrs, int min)
Date(int year, int month, int date, Create a date with the specific data with detailed time.
int hrs, int min, int sec)
Date(String s) Create a date with the specific data as a string.
___________________________________________________________________________
QUESTION 8:
Consider the following class definition:
class Student extends String {
}
Which of the following statement(s) is/ are TRUE?
a. Code will not compile because the body is not defined.
b. Code will not compile because the class is not declared as public.
c. Code will not compile because of the super class String.
d. Code will compile successfully.
Correct Answer: c
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
QUESTION 9:
Which of the following method(s) belong(s) to the String class?
a. length()
b. compareTo()
c. equals()
d. substring()
Correct Answer: a, b, c, and d
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
______________________________________________________________________________
QUESTION 10:
In which of the following package the class System is defined?
a. java.io
b. java.lang
c. java.util
d. java.net
Correct Answer: b
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
QUESTION 11:
Which of the following is not a valid String literal(s)?
a. Java
b. “Java”
c. ‘J’
d. 123.45
Correct Answer: b
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
QUESTION 12:
Which of the following statement(s) is(are) true?
a. append() is a valid method of class String.
b. append() is a valid method class StringBuffer.
c. reverse() is a valid method of class String.
d. reverse() is a valid method class StringBuffer.
Correct Answer: b and d
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
_____________________________________________________________________________
QUESTION 13:
Which of the following statement(s) is(are) true?
a. Anything written within double quotes is treated as a string object.
b. An object of type String cannot be changed, that is, it is immutatble.
c. Anything in Java can be converted into String and vice-versa.
d. A String object can be of infinite length.
Correct Answer: a, b, and c
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
QUESTION 14:
Which of the following is (are) valid method(s) in the class String ?
a. isEmpty()
b. equals()
c. length()
d. replace()
Correct Answer: a, b, c, and d
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
QUESTION 15:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Consider the following piece of code.
public class StringTest3 {
public static void main(String args[]){
String text1 = "DATA STRUCTURE WITH JAVA";
String text2 = "data structures with java";
int output = text1.compareTo(text2);
System.out.println(output);
}
}
What output the above program will produce?
a. 0
b. 7
c. 1
d. -32
Correct Answer: d
Detailed Solution:
Please see the discussion at https://cse.iitkgp.ac.in/~dsamanta/javads/index.htm
______________________________________________________________________________
************END************