String, String Buffers And Its Functions Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By 304751
3
304751
Community Contributor
Quizzes Created: 1 | Total Attempts: 5,109
| Attempts: 5,109 | Questions: 19
Please wait...
Question 1 / 19
0 %
0/100
Score 0/100
1. What is the output of this code? String s="First Program"; System.out.println("[" + s + "]"); s.trim( );

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.

Submit
Please wait...
About This Quiz
String, String Buffers And Its Functions Quiz - Quiz

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... see morethe 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.
see less

2. In one of the cases, we must apply StringBuffer rather than StringBuilder

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.

Submit
3. Choose the correct option.

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.

Submit
4. If  two strings are same,the method public int compareTo returns

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.

Submit
5. 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"));             }

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.

Submit
6. Which of the following are used to manipulate the string buffer?

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.

Submit
7. If we override equals() , then we may or maynot override hashCode(). State true or 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.

Submit
8. 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?

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".

Submit
9. Passing null to indexOf  or lastIndexOf will throw

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.

Submit
10. How many overloaded constructors ,other than the default constructor, does Stringbuffer have?

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.

Submit
11. 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"));

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.

Submit
12. Number of constructors in string object in JAVA2  v5.0 ?

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.

Submit
13. What will be the output? String s="Synergic Thunders"; System.out.println( s.substing(4,12) );

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.

Submit
14.  What will be the result if you compare StringBuffer with String if both have same values?

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.

Submit
15. Which operators are overloaded for Strings?

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.

Submit
16. What is the use of ensureCapacity()?

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.

Submit
17. With respect to equals() , if x.equals(y) is true, then x.hashCode() == y.hashCode() is true. This is know as...............?

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".

Submit
18. String objects are immutable. what do you mean by that?

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.

Submit
19. Which of the following statements are false?

Explanation

The given answer states that reverse() is available in String, which is false. The reverse() method is not available in the String class.

Submit
View My Results

Quiz Review Timeline (Updated): Jul 2, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 02, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 10, 2011
    Quiz Created by
    304751
Cancel
  • All
    All (19)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the output of this code?...
In one of the cases, we must apply StringBuffer rather than...
Choose the correct option.
If  two strings are same,the method public int compareTo returns
What iis the output?...
Which of the following are used to manipulate the string buffer?
If we override equals() , then we may or maynot override hashCode()....
StringBuffer ab=new StringBuffer("smith");...
Passing null to indexOf  or lastIndexOf will throw
How many overloaded constructors ,other than the default constructor,...
What is the output of this code?...
Number of constructors in string object in JAVA2  v5.0 ?
What will be the output? ...
 What will be the result if you compare StringBuffer with String...
Which operators are overloaded for Strings?
What is the use of ensureCapacity()?
With respect to equals() , if x.equals(y) is true, then x.hashCode()...
String objects are immutable. what do you mean by that?
Which of the following statements are false?
Alert!

Advertisement