Which of these method of class stringbuffer is used to find the length of current character sequence

Which of these method of class stringbuffer is used to find the length of current character sequence
Which of these method of class stringbuffer is used to find the length of current character sequence
DownloadApp

Home » JAVA Programming » Strings » Question

  1. Which of these method of class StringBuffer is used to find the length of current character sequence?

    1. Length()
    2. capacity()
    3. Capacity()
    4. length()
    5. None of these

length()

Which of these method of class stringbuffer is used to find the length of current character sequence

Java MCQs on StringBuffer class of Java Programming Language.

1. Which of these class is used to create an object whose character sequence is mutable?a) String()b) StringBuffer()c) String() & StringBuffer()d) None of the mentioned

Answer: b
Clarification: StringBuffer represents growable and writable character sequence.

2. Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking string?a) concat()b) append()c) join()d) concatenate()

Answer: b
Clarification: None.

3. Which of these method of class StringBuffer is used to find the length of current character sequence?a) length()b) Length()c) capacity()d) Capacity()

Answer: a
Clarification: None.

4. What is the string contained in s after following lines of Java code?

StringBuffer s new StringBuffer("Hello"); s.deleteCharAt(0);

a) Hellb) elloc) Held) llo

Answer: b
Clarification: deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.

5. Which of the following statement is correct?a) reverse() method reverses all charactersb) reverseall() method reverses all charactersc) replace() method replaces first occurrence of a character in invoking string with another characterd) replace() method replaces last occurrence of a character in invoking string with another character

Answer: a
Clarification: reverse() method reverses all characters. It returns the reversed object on which it was called.

6. What will be the output of the following Java program?

  1. class output
  2. {
  3. public static void main(String args[])
  4. {
  5. String a = "hello i love java";
  6. System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
  7. }
  8. }

a) 6 4 6 9b) 5 4 5 9c) 7 8 8 9d) 1 14 8 15

Answer: d Clarification: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of the character pointed by c in the given array.

Output:

$ javac output.java $ java output 1 14 8 15

Here you can find Strings Questions and Answers.

Why Strings Questions and Answers Required?

In this Strings Questions and Answers section you can learn and practice Strings Questions and Answers to improve your skills in order to face technical inerview conducted by organisations. By Practicing these interview questions, you can easily crack any Exams interview.

Where can I get Strings Questions and Answers?

AllIndiaExams provides you lots Strings Questions and Answers with proper explanation. Fully solved examples with detailed answer description. All students, freshers can download Strings Questions and Answers as PDF files and eBooks.

How to solve these Strings Questions and Answers?

You no need to worry, we have given lots of Strings Questions and Answers and also we have provided lots of FAQ's to quickly answer the questions in the Competitive Exams interview.

Strings Questions and Answers

1. Which of these class is superclass of String and StringBuffer class?

Strings Questions and Answers

2. Which of these operators can be used to concatenate two or more String objects?

Strings Questions and Answers

3. Which of these method of class String is used to obtain length of String object?

Strings Questions and Answers

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

Strings Questions and Answers

5. Which of these constructors is used to create an empty String object?

Strings Questions and Answers

6. Which of these is an oncorrect statement?

Strings Questions and Answers

7. Which of these method of class String is used to compare two String objects for their equality?

Strings Questions and Answers

8. Which of these methods is used to compare a specific region inside a string with another specific region in another string?

Strings Questions and Answers

9. Which of these method of class String is used to check weather a given object starts with a particular string literal?

Strings Questions and Answers

10. What is the value returned by function compare to() if the invoking string is less than the string compared?

- Page 4

The length() method of Java StringBuffer class returns the length (total characters) of this sequence.

Syntax:

public int length()

NA

Returns:

The length() method returns the total character count of this sequence.

Exception:

NA

Compatibility Version:

Java 1.0 and above

Example 1

public class StringBufferLengthExample1 { public static void main(String[] args) { StringBuffer sb1= new StringBuffer("javatpoint"); // return the length of string int length1 = sb1.length(); System.out.println("total character count of \"javatpoint\": " + length1); StringBuffer sb2 = new StringBuffer("string l e ng th"); // return the length of string int length2 = sb2.length(); System.out.println("total character count of \"string l e ng th\": " + length2); sb2 = new StringBuffer(""); //return the length of empty string System.out.println("total character count: " + sb2.length()); } }

Test it Now

Output:

total character count of "javatpoint": 10 total character count of "string l e ng th": 16 total character count: 0

Example 2

In this example, we are calculating the length of the string by taking input from a user.

import java.util.Scanner; public class StringBufferLengthExample2 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(""); System.out.print("enter your string value: "); Scanner sc = new Scanner(System.in); sb.append(sc.nextLine()); //returning total input character System.out.println("total input character: "+sb.length()); sc.close(); } }

Test it Now

Output:

enter your string value: hello this is a string total input character: 22


Next TopicStringBuffer class

Which of these method of class stringbuffer is used to find the length of current character sequence
For Videos Join Our Youtube Channel: Join Now

  • Send your Feedback to [email protected]
Which of these method of class stringbuffer is used to find the length of current character sequence
Which of these method of class stringbuffer is used to find the length of current character sequence
Which of these method of class stringbuffer is used to find the length of current character sequence