String and String Buffer
Prepared by
N.pavani[192111171]
B.usha[192111159]
MCQ [1-10]
1. Which of the following is immutable in Java?
a) String
b) StringBuffer
c) StringBuilder
d) None of the above
2. what will be the output of following code?
package classandobject;
public class Length {
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("programming");
sb.insert(1, 'A');
System.out.println(sb);
a) PAogramming
b) pAogramming
c) programming
d) None of the above
3. Which constructor can be used to create an empty StringBuffer object with a
default capacity?
A. StringBuffer()
B. StringBuffer(int capacity)
C. StringBuffer(String str)
D. None of the above
4.. what will be the output of following code?
package classandobject;
public class Length {
public static void main(String[] args)
{
String str = "Java";
str.concat(" Programming");
System.out.println(str);
a) Programming
b) java
c) Java
d) None of the above
5. Which method in StringBuffer is used to reverse the characters?
A. reverse()
B. append()
C. delete()
D. insert()
6. What will be the output of the following code?
package classandobject;
public class Length {
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Hello");
sb.insert(1, 'A');
System.out.println(sb);
}
A. HAllo
B. Hlo
C. Hello
D. Hallo
7. What will be the output of the following code?
package classandobject;
public class Length {
public static void main(String[] args)
{
System.out.println("pavaniindhuushanandhini".length())
}
A. 23
B. 25
C. 24
D.21
8. Which of the following constructors can be used to create an empty StringBuffer
object?
A. StringBuffer()
B. StringBuffer(int capacity)
C. StringBuffer(String str)
D. All of the above
9. Which of the following is a valid way to create a String object in Java?
A. String str = "Hello";
B. String str = new String("Hello");
C. String str = new String();
D. All of the above
10, Which method is used to check if a string starts with a specific prefix?
A. startsWith()
B. endsWith()
C. contains()
D. matches()