Difficulty: Easy
Correct Answer: StringBuffer(int size , String str)
Explanation:
Introduction / Context:
The Java StringBuffer class provides several overloaded constructors, and knowing which ones exist is a common low level interview question. It checks whether a candidate has actually worked with the standard Java libraries and whether they understand how overloading works. In this case, the question asks which constructor signature is not defined for StringBuffer, so you need to recall or reason about the real constructors provided by the Java API.
Given Data / Assumptions:
Concept / Approach:
According to the Java API, StringBuffer has several constructors: a no argument constructor which creates a buffer with default capacity, a constructor that takes an int capacity, a constructor that takes a String to initialize the buffer with specific content, and in some versions a constructor that takes a CharSequence. There is no constructor that takes both an int size and a String argument together. Therefore, any signature that mixes capacity and initial content in a single constructor with two parameters is not valid for StringBuffer in the core API.
Step-by-Step Solution:
Step 1: Recall the commonly used constructors: StringBuffer(), StringBuffer(int capacity), and StringBuffer(String str).
Step 2: Observe that these constructors either set the initial capacity or set the initial content, but they do not combine both in the same signature.
Step 3: Examine option B, which is StringBuffer(int size). This matches the constructor that sets the capacity of the buffer.
Step 4: Examine option C, which is StringBuffer(String str). This matches the constructor that sets the initial content of the buffer.
Step 5: Examine option D, which is StringBuffer(). This matches the default constructor for an empty buffer.
Step 6: Option A, which is StringBuffer(int size , String str), does not match any standard StringBuffer constructor, so it is the incorrect form.
Verification / Alternative check:
If you have used an integrated development environment, you can remember that auto complete only shows constructors with zero, one int, one String, or one CharSequence argument. There is no overload that takes two arguments of type int and String together. Also, from an API design perspective, mixing capacity and content into one constructor would be less clear, and the existing constructors already cover the use cases of capacity and content separately. This confirms that option A is the only invalid constructor signature in the list.
Why Other Options Are Wrong:
Option B is a valid constructor that creates a buffer with the specified initial capacity. Option C is a valid constructor that initializes the buffer with the contents of the given String. Option D is a valid default constructor that creates an empty buffer with a default capacity. None of these are incorrect, so they cannot be the correct choice for this question.
Common Pitfalls:
A common mistake is to assume that more configuration options exist than are actually provided by the core API, especially when mixing parameters such as size and content. Another pitfall is confusing StringBuffer with StringBuilder, which has similar constructors but still does not provide the two parameter constructor shown in option A. In interviews, it is useful to demonstrate that even if you do not memorize every constructor, you can reason about the typical patterns and identify signatures that look inconsistent with them.
Final Answer:
The incorrect constructor form for StringBuffer in Java is StringBuffer(int size , String str).
Discussion & Comments