Saturday, August 10, 2024

String, StringBuffer, and StringBuilder MCQ's

 Here are multiple-choice questions (MCQs) on String, StringBuffer, and StringBuilder in Java:


Question 1

Which of the following is immutable in Java?


A) String


B) StringBuffer


C) StringBuilder


D) All of the above


Answer: A) String


Question 2

What is the key difference between StringBuffer and StringBuilder?


A) StringBuffer is immutable, while StringBuilder is mutable.


B) StringBuffer is synchronized, while StringBuilder is not.


C) StringBuffer is faster than StringBuilder.


D) There is no difference.


Answer: B) StringBuffer is synchronized, while StringBuilder is not.


Question 3

Which of the following classes is thread-safe?


A) String


B) StringBuffer


C) StringBuilder


D) None of the above


Answer: B) StringBuffer


Question 4

What is the default initial capacity of a StringBuffer or StringBuilder object?


A) 8


B) 10


C) 16


D) 32


Answer: C) 16


Question 5

Which method in String class is used to return a new string that is a substring of the original string?


A) split()


B) subString()


C) substring()


D) slice()


Answer: C) substring()


Question 6

Which of the following will create an empty StringBuilder object with a specified capacity of 50?


A) new StringBuilder();


B) new StringBuilder(50);


C) new StringBuilder("50");


D) new StringBuilder(new String("50"));


Answer: B) new StringBuilder(50);


Question 7

Which method is used to reverse the characters in a StringBuffer or StringBuilder object?


A) reverseString()


B) reverse()


C) invert()


D) flip()


Answer: B) reverse()


Question 8

Which of the following methods is NOT available in the String class?


A) append()


B) charAt()


C) length()


D) equals()


Answer: A) append()


Question 9

What will be the output of the following code?


java

Copy code

String str1 = "Hello";

String str2 = "Hello";

System.out.println(str1 == str2);

A) true


B) false


C) Compilation error


D) Runtime error


Answer: A) true


Question 10

What is the output of the following code?


java

Copy code

String str = "abc";

str.concat("def");

System.out.println(str);

A) abcdef


B) def


C) abc


D) abcdefdef


Answer: C) abc


Question 11

Which class should be used when you need a mutable sequence of characters that is not thread-safe?


A) String


B) StringBuffer


C) StringBuilder


D) CharSequence


Answer: C) StringBuilder


Question 12

What will be the output of the following code?


java

Copy code

StringBuffer sb = new StringBuffer("Hello");

sb.append(" World");

System.out.println(sb.toString());

A) Hello World


B) HelloWorld


C) Hello


D) World


Answer: A) Hello World


Question 13

Which of the following is the correct way to create a new String object from a StringBuffer object?


A) new String(new StringBuffer("Hello"));


B) new StringBuffer(new String("Hello"));


C) StringBuffer.toString();


D) new StringBuffer("Hello").toString();


Answer: D) new StringBuffer("Hello").toString();


Question 14

What is the primary purpose of the capacity() method in StringBuffer and StringBuilder?


A) To return the current length of the string.


B) To return the maximum length that the object can hold without resizing.


C) To return the number of characters in the object.


D) To return the number of unused space left in the object.


Answer: B) To return the maximum length that the object can hold without resizing.


Question 15

What is the output of the following code?


java

Copy code

StringBuilder sb = new StringBuilder("abc");

sb.delete(1, 3);

System.out.println(sb);

A) a


B) ab


C) c


D) ac


Answer: A) a


Question 16

Which of the following methods can be used to compare two strings lexicographically in Java?


A) compareTo()


B) equals()


C) compare()


D) compareWith()


Answer: A) compareTo()


Question 17

What will be the output of the following code?


java

Copy code

String str = "hello";

String upperStr = str.toUpperCase();

System.out.println(upperStr);

A) HELLO


B) hello


C) Hello


D) hELLO


Answer: A) HELLO


Question 18

Which of the following is true about the insert() method in StringBuffer and StringBuilder?


A) It replaces characters in the string.


B) It inserts characters at a specified position.


C) It appends characters to the end of the string.


D) It deletes characters from the string.


Answer: B) It inserts characters at a specified position.


Question 19

What is the purpose of the substring(int beginIndex, int endIndex) method in the String class?


A) To return a new string starting from beginIndex to endIndex.


B) To modify the original string from beginIndex to endIndex.


C) To delete a portion of the string from beginIndex to endIndex.


D) To replace the characters from beginIndex to endIndex.


Answer: A) To return a new string starting from beginIndex to endIndex.


Question 20

What will be the result of the following code?


java

Copy code

String str = "java";

StringBuilder sb = new StringBuilder(str);

str = sb.reverse().toString();

System.out.println(str);

A) avaj


B) java


C) Error


D) jvaa


Answer: A) avaj


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...