Difficulty: Easy
Correct Answer: NagpurMumbai
Explanation:
Introduction / Context:
This checks understanding of the String.Insert
method, which returns a new string by inserting a specified substring at a given index (0-based).
Given Data / Assumptions:
s1 = 'Nagpur'
(length 6).s1.Insert(6, 'Mumbai')
.s1
, so insertion occurs at the end.
Concept / Approach:
The Insert
method does not mutate s1
(strings are immutable); it produces a new string with the substring inserted at the specified index: prefix up to index + inserted text + remainder. If the index is equal to the original length, it appends.
Step-by-Step Solution:
Verification / Alternative check:
Replace the index with 0 to see a different effect ('MumbaiNagpur'). Attempting an index > length would throw an ArgumentOutOfRangeException
.
Why Other Options Are Wrong:
'NagpuMumbair' misplaces letters. 'Nagpur Mumbai' implies insertion of a space not present in the call. 'Mumbai' or 'Nagpur' would imply ignoring one of the strings; that is not the behavior of Insert
.
Common Pitfalls:
Confusing Insert
with Concat
and off-by-one errors with indexes.
Final Answer:
NagpurMumbai
Discussion & Comments