The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Do you know what a string is? Do you think you can pass this string, string buffers, and its functions quiz? If you have an idea of how the string and the string buffer work and their respective functions, then this quiz will be easy for you to crack. So, try this quiz and get to test your knowledge today. Wishing you all the best for this test, and enjoy.
Questions and Answers
1.
String objects are immutable. what do you mean by that?
A.
Object state can be replaced
B.
Object state can’t be replaced
C.
Object state can’t be modified
D.
Object state can be modified
Correct Answer
D. Object state can be modified
Explanation String objects in Java are immutable, which means that once a string object is created, its state (the sequence of characters it contains) cannot be changed. However, the given answer states that the object state can be modified, which is incorrect.
Rate this question:
2.
Which operators are overloaded for Strings?
A.
=
B.
++
C.
+=
D.
==
Correct Answer(s)
A. = C. +=
Explanation The operators = and += are overloaded for Strings. The = operator is used to assign a value to a String variable, while the += operator is used to concatenate two Strings together and assign the result to the left operand. The other operators mentioned, ++ and ==, are not overloaded for Strings. The ++ operator is used for incrementing numeric values, and the == operator is used for comparing equality between two objects.
Rate this question:
3.
Which of the following statements are false?
A.
Reverse() is available in String
B.
The content of StringBuffer can be expanded beyond their initial buffer size
C.
The standard java.lang.StringTokenizer class is a special type of Enumeration that represents segments of a string, which may be separated by one or more "delimiters"
D.
StringBuffer is a final class
E.
String objects are immutable
Correct Answer(s)
A. Reverse() is available in String C. The standard java.lang.StringTokenizer class is a special type of Enumeration that represents segments of a string, which may be separated by one or more "delimiters" D. StringBuffer is a final class
Explanation The given answer states that reverse() is available in String, which is false. The reverse() method is not available in the String class.
Rate this question:
4.
What will be the result if you compare StringBuffer with String if both have same values?
A.
True
B.
False
C.
Error
D.
None of these
Correct Answer
B. False
Explanation When comparing a StringBuffer with a String that have the same values, the result will be false. This is because StringBuffer and String are two different classes in Java. The equals() method is not overridden in the StringBuffer class, so when comparing it with a String using the equals() method, it will return false.
Rate this question:
5.
Passing null to indexOf or lastIndexOf will throw
A.
NullCharacterException
B.
NullParameterException
C.
NullPointerException
D.
NumberFormatException
Correct Answer
C. NullPointerException
Explanation Passing null to the indexOf or lastIndexOf methods will throw a NullPointerException. This is because these methods are used to search for a specific character or substring within a string, and if the string is null, it is not possible to perform the search operation. Therefore, a NullPointerException is thrown to indicate that a null value was passed as an argument, which is not allowed.
Rate this question:
6.
What will be the output?
String s="Synergic Thunders";
System.out.println( s.substing(4,12) );
A.
Ergic th
B.
Ergic Thu
C.
Ergic Th
D.
ErgicThu
Correct Answer
C. Ergic Th
Explanation The given code snippet is attempting to print a substring of the string "Synergic Thunders". The substring method is called on the string s with the parameters 4 and 12, which indicates that the substring should start at index 4 and end at index 12 (exclusive). The substring "ergic Th" satisfies this condition, so it will be printed as the output.
Rate this question:
7.
How many overloaded constructors ,other than the default constructor, does Stringbuffer have?
A.
5
B.
2
C.
3
D.
4
Correct Answer
B. 2
Explanation StringBuffer has two overloaded constructors other than the default constructor. Overloaded constructors are multiple constructors within a class that have the same name but different parameters. In the case of StringBuffer, the two overloaded constructors are used to create a StringBuffer object with either a specified initial capacity or a specified String value.
Rate this question:
8.
What is the output of this code?
String s="Now is the time for all good men"+"to come to the aid of their country";
System.out.println(s.indexOf('t'));
System.out.println(s.lastIndexOf('t',10));
System.out.println(s.indexOf("the"));
A.
7,10,7
B.
8,12,8
C.
7,11,7
D.
8,11,8
Correct Answer
C. 7,11,7
Explanation The code first initializes a string variable "s" with the concatenation of two strings. The first print statement uses the indexOf method to find the index of the first occurrence of the character 't' in the string "s", which is 7. The second print statement uses the lastIndexOf method to find the index of the last occurrence of the character 't' in the string "s" but only searches up to index 10, which is 11. The third print statement uses the indexOf method to find the index of the first occurrence of the string "the" in the string "s", which is 7.
Rate this question:
9.
Number of constructors in string object in JAVA2 v5.0 ?
A.
11
B.
10
C.
14
D.
12
Correct Answer
D. 12
Explanation In Java 2 version 5.0, the String object has 12 constructors. These constructors allow the creation of String objects in various ways, such as by providing a character array, byte array, or another String object as input. The availability of multiple constructors provides flexibility and convenience when working with String objects in Java.
Rate this question:
10.
If two strings are same,the method public int compareTo returns
A.
Null
B.
Positive
C.
Negative
D.
Zero
Correct Answer
D. Zero
Explanation The method public int compareTo returns zero when two strings are the same. This means that the strings have the exact same characters in the same order. The compareTo method compares the strings lexicographically, which means it checks the Unicode value of each character in the strings. If the strings are the same, their Unicode values will also be the same, resulting in a zero value being returned.
Rate this question:
11.
Which of the following are used to manipulate the string buffer?
A.
Append,trim,setCharAt,and replace
B.
Append,insert,setCharAt,and reverse
C.
Append,insert,getChars,and replace
D.
Append,trim,getChars,and reverse
Correct Answer
B. Append,insert,setCharAt,and reverse
Explanation The correct answer is "append, insert, setCharAt, and reverse". These methods are used to manipulate the string buffer. The "append" method is used to add characters or strings to the end of the buffer. The "insert" method is used to insert characters or strings at a specified position in the buffer. The "setCharAt" method is used to replace a character at a specific index in the buffer. The "reverse" method is used to reverse the order of characters in the buffer.
Rate this question:
12.
With respect to equals() , if x.equals(y) is true, then x.hashCode() == y.hashCode() is true.
This is know as...............?
A.
Consistent
B.
Symmetric
C.
Null
D.
Reflexive
E.
Transitive
Correct Answer
C. Null
Explanation The statement in the question suggests that if x.equals(y) is true, then x.hashCode() == y.hashCode() is true. However, this statement is not true for the option "null". In Java, if x is null, then x.hashCode() will throw a NullPointerException. Therefore, the correct answer is "null".
Rate this question:
13.
What is the output of this code?
String s=”First Program”;
System.out.println(“[“ + s + “]”);
s.trim( );
A.
[ First Program ]
B.
[First Program]
C.
[FirstProgram]
D.
First Program
Correct Answer
B. [First Program]
Explanation The code will output "[ First Program ]" because it is printing the string "s" surrounded by square brackets. The line "s.trim();" does not affect the output because it is not assigned to a variable or printed.
Rate this question:
14.
Choose the correct option.
A.
StringBuilder is synchronized, it offers faster performance than StringBuffer
B.
StringBuilder is not synchronized, it offers faster performance than StringBuffer
C.
StringBuilder is not synchronized, it offers slower performance than StringBuffer
D.
StringBuilder is synchronized, it offers slower performance than StringBuffer
Correct Answer
B. StringBuilder is not synchronized, it offers faster performance than StringBuffer
Explanation The correct answer is "StringBuilder is not synchronized, it offers faster performance than StringBuffer." StringBuilder is not synchronized, meaning it is not thread-safe, which allows for faster performance compared to StringBuffer. StringBuffer, on the other hand, is synchronized, making it thread-safe but slower in performance due to the synchronization overhead.
Rate this question:
15.
In one of the cases, we must apply StringBuffer rather than StringBuilder
A.
Multitasking
B.
Multithreading
C.
Exception handling
D.
String comparision
Correct Answer
B. Multithreading
Explanation In multithreading, multiple threads are executing concurrently within a single program. Since multiple threads can access and modify shared data simultaneously, there is a possibility of data corruption or inconsistency. To avoid this, synchronization is needed. StringBuffer is thread-safe, meaning it is synchronized and multiple threads can access it without any issues. On the other hand, StringBuilder is not synchronized, making it more efficient but not suitable for multithreaded environments where synchronization is required. Hence, in cases where thread safety is important, such as in multithreading scenarios, StringBuffer should be used instead of StringBuilder.
Rate this question:
16.
If we override equals() , then we may or maynot override hashCode(). State true or false.
A.
True
B.
False
Correct Answer
B. False
Explanation If we override the equals() method, it is strongly recommended to also override the hashCode() method. This is because the hashCode() method is used by certain data structures such as HashMap and HashSet to determine the bucket location for storing objects. If two objects are considered equal based on the equals() method but have different hash codes, they may end up in different buckets, resulting in unexpected behavior. Therefore, it is best practice to override both equals() and hashCode() methods together.
Rate this question:
17.
What is the use of ensureCapacity()?
A.
Returns current number of capacity
B.
The string buffer is allocated atmost ‘n’ places
C.
The string buffer is allocated atleast ‘n’ places
D.
The string buffer is allocated some ‘n+1’ places
Correct Answer
C. The string buffer is allocated atleast ‘n’ places
Explanation The use of ensureCapacity() is to allocate at least 'n' places for the string buffer. This means that if the current capacity of the string buffer is less than 'n', the method will increase the capacity to at least 'n' to accommodate additional characters or data.
Rate this question:
18.
What iis the output?
{
String name = "Petroleum";
System.out.println(name.equals("Petroleum"));
System.out.println(name.equals("petroleum"));
System.out.println(name.equalsIgnoreCase("petroleum"));
}
A.
True true true
B.
True false false
C.
True false true
D.
False true true
Correct Answer
C. True false true
Explanation The output of the code is "true false true". This is because the first System.out.println statement checks if the variable "name" is equal to "Petroleum", which it is, so it returns true. The second System.out.println statement checks if the variable "name" is equal to "petroleum", but since Java is case-sensitive, it returns false. The third System.out.println statement uses the equalsIgnoreCase method, which ignores the case of the strings being compared, so it returns true.
Rate this question:
19.
StringBuffer ab=new StringBuffer(“smith”);
System.out.println(“ab = “ + ab);
char charArr[ ] = “Hi xy”.tocharArray( );
Ab.getChars(0,2,charArr,3);
What is the output?
A.
Hi smi
B.
Hi sm
C.
Smi
D.
Ith
Correct Answer
B. Hi sm
Explanation The code first creates a StringBuffer object "ab" with the value "smith". Then, it creates a character array "charArr" with the value "Hi xy". The getChars() method is then called on the "ab" object, which copies a portion of the characters from the "ab" object to the "charArr" array. In this case, it copies the characters from index 0 to 2 (inclusive) from "ab" to index 3 onwards in "charArr". Therefore, the output will be "Hi smi".
Rate this question:
Quiz Review Timeline +
Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.