-1-
STRING ONE MARK ANSWER
1. What is the output of the following code?
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2);
a) true
b) false
c) Compilation error
d) Runtime exception
2. Which class is immutable in Java?
a) String
b) StringBuffer
c) StringBuilder
d) All of the above
3. What will the following code print?
String s1 = new String("Java");
String s2 = "Java";
System.out.println(s1 == s2);
a) true
b) false
c) null
d) Compilation error
4. Which class is not thread-safe?
a) String
b) StringBuffer
c) StringBuilder
d) All are thread-safe.
-2-
5. What is the result of this code?
StringBuilder sb = new StringBuilder("abc");
sb.append("def");
System.out.println(sb);
a) abc
b) def
c) abcdef
d) Compile-time error
6. Which is faster in single-threaded environments?
a) StringBuffer
b) StringBuilder
c) String
d) All are same
7. Which method converts a StringBuilder to a String?
a) toText()
b) getString()
c) toString()
d) toCharArray()
8. What will this print?
String s = "Java";
s.concat("World");
System.out.println(s);
a) JavaWorld
b) Java
c) World
d) Null
-3-
9. What is the output?
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb1 == sb2);
a) true
b) false
c) abc
d) Compilation error
10. Which is mutable in Java?
a) String
b) StringBuffer
c) StringBuilder
d) Both B and C
11. What happens here?
StringBuilder sb = new StringBuilder("Hello");
sb.insert(5, "Java");
System.out.println(sb);
a) StringIndexOutOfBoundException
b) HelloJava
c) Hello
d) Compile error
-4-
12. Which is better for synchronization?
a) String
b) StringBuffer
c) StringBuilder
d) None
13. What is the length of this string?
String str = "Java ";
System.out.println(str.trim().length());
a) 5
b) 4
c) 3
d) 6
14. Which method is used to compare two strings ignoring case?
a) compareToIgnoreCase()
b) equalsIgnoreCase()
c) isEqual()
d) checkIgnoreCase()
15. Which method replaces characters in a string?
a) replaceAll()
b) switch()
c) change()
d) alter()
-5-
16. Which of these classes has a default capacity of 16 characters?
a) String
b) StringBuffer
c) StringBuilder
d) Both B and C
17. What is the output of this?
StringBuilder sb = new StringBuilder("Hello");
sb.setLength(2);
System.out.println(sb);
a) He
b) Hello
c) Hel
d) H
18. What does StringBuilder reverse() method do?
a) Removes whitespace
b) Converts to uppercase
c) Reverses character order
d) None of the above
19. What is the output?
String s = null;
System.out.println(s + "Test");
a) nullTest
b) Testnull
c) Test
d) null
-6-
20. Which method checks if a string starts with a given prefix?
a) beginsWith()
b) start()
c) startsWith()
d) prefixOf()
21. What will this print?
StringBuilder sb = new StringBuilder("12345");
sb.delete(1, 3);
System.out.println(sb);
a) 12345
b) 15
c) 145
d) 1345
22. Which of the following creates a new string in the pool?
a) new String("Hello")
b) "Hello"
c) String.valueOf("Hello")
d) new StringBuilder("Hello").toString()
23. Which method checks for string content equality?
a) ==
b) equals()
c) equalsIgnoreCase()
d) isSame()
-7-
24. What happens here?
String s = "Java";
s = s.concat("World");
System.out.println(s);
a) Java
b) JavaWorld
c) World
d) null
25. What’s the result?
StringBuilder sb = new StringBuilder();
sb.append("a").append("b").append("c");
System.out.println(sb.length());
a) 0
b) 1
c) 2
d) 3