Data Structure Multiple Choice Questions on “String Reversal using Recursion”.
1. Consider the following iterative implementation used to reverse a string:
#include#include void reverse_string(char *s) { int len = strlen(s); int i,j; i=0; j=len-1; while(______) { char tmp = s[i]; s[i] = s[j]; s[j] = tmp; i++; j--; } }
Which of the following lines should be inserted to complete the above code?
a) i > j
b) i < len
c) j > 0
d) i < j
Answer: d
Clarification: The line “i < j” should be inserted to complete the above code.
2. What is the output of the following code?
#include#include void reverse_string(char *s) { int len = strlen(s); int i,j; i=0; j=len-1; while(i < j) { char tmp = s[i]; s[i] = s[j]; s[j] = tmp; i++; j--; } } int main() { char s[100] = "reverse"; reverse_string(s); printf("%s",s); return 0; }
a) ersevre Answer: b 3. What is the time complexity of the above code used to reverse a string? 4. What does the following code do? a) Copies a string to another string Answer: d 5. What is the output of the following code? a) Yes Answer: a 6. Consider the following recursive implementation used to reverse a string: Which of the following lines should be inserted to complete the above code? Answer: c 7. What is the output of the following code? a) recursion 8. How many times is the function recursive_reverse_string() called when the following code is executed? a) 3 Answer: a 9. What is the time complexity of the above recursive implementation used to reverse a string? Answer: b 10. In which of the following cases is the reversal of a string not equal to the original string? Answer: d
b) esrever
c) eserver
d) eresevr
Clarification: The program reverses the string “reverse” and prints “esrever”.
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: b
Clarification: The time complexity of the above code used to reverse a string is O(n).#include
b) Compares two strings
c) Reverses a string
d) Checks if a string is a palindrome
Clarification: The main purpose of the above code is to check if a string is a palindrome.#include
b) No
c) Runtime error
d) Compile time error
Clarification: The program checks if a string is a palindrome. Since the string rotator is a palindrome, it prints “yes”.void recursive_reverse_string(char *s, int left, int right)
{
if(left < right)
{
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
_________;
}
}
a) recursive_reverse_string(s, left+1, right+1)
b) recursive_reverse_string(s, left-1, right-1)
c) recursive_reverse_string(s, left+1, right-1)
d) recursive_reverse_string(s, left-1, right+1)
Clarification: The line “recursive_reverse_string(s, left+1, right-1)” should be inserted to complete the above code.#include
b) nsoirucer
c) noisrcuer
d) noisrucer
Answer: d
Clarification: The program prints the reversed string of “recursion”, which is “noisrucer”.#include
b) 4
c) 5
d) 6
Clarification: The function recursive_reverse_string() is called 3 times when the above code is executed.
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The time complexity of the above recursive implementation used to reverse a string is O(n).
a) Palindromic strings
b) Strings of length 1
c) Empty String
d) Strings of length 2
Clarification: String “ab” is a string of length 2 whose reversal is not the same as the given one. Palindromic Strings, String of length 1 and Empty Strings case – the reversal is the same as the one given.