Page 439 - CA_Blue( J )_Class10
P. 439
7. What is the output of the following statements?
String a = "Java is a programming language\ndeveloped by\t\'James Gosling\'";
System.out.println(a);
Ans. Java is a programming language
developed by 'James Gosling'
8. Give the output of the following Java statements:
(i) "TRANSPARENT".toLowerCase() (ii) "TRANSPARENT".compareTo("TRANSITION")
Ans. (i) transparent (ii) 7
9. Write a Java statement for each to perform the following tasks:
(i) Find and display the position of the last space in a string str.
(ii) Extract the second character of the string str.
Ans. (i) System.out.println(str.lastIndexOf(' ')); (ii) char ch = str.charAt(1);
D. Assertion and Reasoning based questions.
The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
appropriate option given below:
a. Both A and R are true, and R is the correct explanation of A.
b. Both A and R are true, but R is not the correct explanation of A.
c. A is true, but R is false.
d. A is false, but R is true.
1. Assertion (A): A string is a sequence of characters enclosed between double quotes.
Reason (R): It helps to store a set of characters directly in the object of the String class.
2. Assertion (A): The equals() method is used to compare the contents of two strings.
Reason (R): The == operator compares the references of two string objects.
3. Assertion (A): The substring() method can change the original string.
Reason (R): substring() method extracts a part of a string from the original string.
Ans. 1. a 2. a 3. d
21 st
E. Case-based questions. Century #Experiential Learning
Skills
The substring() method in Java is used to extract a portion of a string. It is a part of the String class and provides a way to create
new strings based on a range of characters from an existing string.
Syntax: There are two overloaded versions of the substring() method:
a. substring(int beginIndex) : Extracts a substring starting from the specified beginIndex to the end of the string.
String str = "Hello, World!";
String sub = str.substring(7); // "World!"
b. substring(int beginIndex, int endIndex) : Extracts a substring starting from the specified beginIndex to endIndex 1.
String str = "Hello, World!";
String sub = str.substring(0, 5); // "Hello"
1. What is the purpose of the substring() method in Java?
a. To modify the original string b. To extract a portion of a string
c. To compare two strings d. To reverse a string
2. In which class is the substring() method defined?
a. StringBuffer b. StringBuilder
c. String d. Arrays
3. Which of the following is a valid use of substring(int beginIndex)?
a. String str = "Hello"; String sub = str.substring();
b. String str = "Hello"; String sub = str.substring(7, 12);
c. String str = "Hello, World!"; String sub = str.substring(7);
d. String str = "Hello, World!"; String sub = str.substring("World");
4. What will be the output of the following code?
String str = "Hello, World!";
437
String Handling 437

