Interview Question
Country: India
That string is not read only because it doesn't point to hard coded string.It contains those characters only.
when you create an array of any type, the compiler creates a const pointer for the base address of that array. and since it is a const pointer you cannot modify the size of the array. And since you created the object as an array and not using a pointer to dynamically allocate the memory you cannot call delete [] on the array, therefore there is no way to change the size of the array.
Rather common Java question, since Java abstracts away realloc...
char[] swap(char a,char b)
{
char diff = a ^ b;
a=diff^arr2;
b=diff^arr1;
return new char[]{a,b};
}
void append(List<char> src,int start,List<char> dest)
{
dest.addAll(start,src);
}
void swap(List<char> arr1,List<char> arr2)
{
if(arr1.length==0 && arr2.length==0)return;
int i=0;
for(;i<arr1.length && arr1.length==arr2.length;i++)
{
char[] swapped = swap(arr1.get(i),arr2.get(i));
arr1.set(i,swapped[1]);
arr2.set(i,swapped[0]);
}
if(arr1.length > arr2.length)
{
append(arr1,i,arr2);
}
else if(arr1.length < arr2.length)
{
append(arr2,i,arr1);
}
}
I dont think it is possible........
- Anonymous August 04, 2012rather only the length common in both can be swapped