Which of this method of class String is used to extract a substring from a String object Mcq?

Which of these method of class String is used to extract a single character from a String object?

(a) CHARAT()

(b) chatat()

(c) charAt()

(d) ChatAt()

I have been asked this question during an internship interview.

Origin of the question is String Handling Basics topic in section String Handling of Java

Select the correct answer from above options

Java questions and answers, Java questions pdf, Java question bank, Java questions and answers pdf, mcq on Java pdf, Java questions and solutions, Java mcq Test , Interview Java questions, Java Questions for Interview, Java MCQ (Multiple Choice Questions),Core Java Questions, Core Java MCQ,core java interview questions for experiencedcore java interview questions for 3 years experience,java programming questions,core java interview questions for freshers,java interview questions and answers for freshers, java programming questions and answers pdf, java interview questions for beginners

The Java String class substring() method returns a part of the string.

We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive. In other words, the beginIndex starts from 0, whereas the endIndex starts from 1.

There are two types of substring methods in Java string.

Signature

If we don't specify endIndex, the method will return all the characters from startIndex.

Parameters

startIndex : starting index is inclusive

endIndex : ending index is exclusive

Returns

specified string

Exception Throws

StringIndexOutOfBoundsException is thrown when any one of the following conditions is met.

  • if the start index is negative value
  • end index is lower than starting index.
  • Either starting or ending index is greater than the total number of characters present in the string.

Internal implementation substring(int beginIndex)

Internal implementation substring(int beginIndex, int endIndex)

Java String substring() method example

FileName: SubstringExample.java

Test it Now

Output:

Java String substring() Method Example 2

FileName: SubstringExample2.java

Output:

Javatpoint
point
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10

Applications of substring() Method

1) The substring() method can be used to do some prefix or suffix extraction. For example, we can have a list of names, and it is required to filter out names with surname as "singh". The following program shows the same.

FileName: SubstringExample3.java

Output:

Yuvraj Singh
Harbhajan Singh
Gurjit Singh
Sandeep Singh
Milkha Singh

2) The substring() method can also be used to check whether a string is a palindrome or not.

FileName: SubstringExample4.java

Output:

madam is a palindrome.
rock is not a palindrome.
eye is a palindrome.
noon is a palindrome.
kill is not a palindrome.


This is the java programming questions and answers section on "Java Strings" with an explanation for various interview, competitive examination and entrance test. Solved examples with detailed answer description, explanation are given and it would be easy to understand.

Java Strings Questions

Online Test Name Java Strings
Exam Type Multiple Choice Questions
Category Computer Science Engineering Quiz
Number of Questions 26

11. Which of these data type value is returned by equals() method of String class?

  • A. char
  • B. int
  • C. boolean
  • D. All of the mentioned
  • View Answer
  • Workspace
  • Report
  • Discuss

Answer & Explanation

Answer: Option C

Explanation:

equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.

Workspace

Report Error

12. Which of these method of class String is used to extract a substring from a String object?

  • A. substring()
  • B. Substring()
  • C. SubString()
  • D. None of the mentioned
  • View Answer
  • Workspace
  • Report
  • Discuss

Workspace

Report Error

13.

What will s2 contain after following lines of code?

 String s1 = "one;

 String s2 = s1.concat("two)

  • A. one
  • B. two
  • C. onetwo
  • D. twoone
  • View Answer
  • Workspace
  • Report
  • Discuss

Answer & Explanation

Answer: Option C

Explanation:

Two strings can be concatenated by using concat() method.

Workspace

Report Error

15. What is the value returned by function compareTo() if the invoking string is less than the string compared?

  • A. zero
  • B. value less than zero
  • C. value greater than zero
  • D. None of the mentioned
  • View Answer
  • Workspace
  • Report
  • Discuss

Answer & Explanation

Answer: Option B

Explanation:

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

Workspace

Report Error

Mock Tests & Online Quizzes

Which methods of class string is used to extract a substring from a string object?

You can extract a substring from a string using the slice() method.

Which of these methods of class string is used to extract a substring from a string object in Java?

The substring(int start, int end) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to the index end-1 of this sequence.

Which of this method of class 10th is used to extract a substring from a string object?

split() method: The split() method of String class can be used to extract a substring from a sentence.

Which method is used to extract a part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters.