Reversal Array Operation Multiple Choice Questions and Answers (MCQs)

«
»

This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Reversal Array Operation”.

1. What will be the resulting array after reversing arr[]={3,5,4,2}?
a) 2,3,5,4
b) 4,2,3,5
c) 5,4,2,3
d) 2,4,5,3
View Answer

Answer: d
Explanation: The resulting array upon reversing after reversal is arr[]={2,4,5,3}. We can implement an algorithm for this purpose in various possible ways.
advertisement

2. How many swaps are required for reversing an array having n elements where n is an odd number?
a) (n-1) / 2
b) n/2
c) (n/2) – 1
d) (n+1)/2
View Answer

Answer: a
Explanation: The number of swaps required for an odd element and an even element array is different because in an odd element array the position of the middle element does not need to be changed. So the number of swaps will be (n-1) / 2.

3. How many swaps are required for reversing an array having n elements where n is an even number?
a) (n-1) / 2
b) n/2
c) (n/2) – 1
d) (n+1)/2
View Answer

Answer: b
Explanation: The number of swaps required for an odd element and an even element array is different because in an odd element array the position of the middle element does not need to be changed. So number of swaps required will be n/2.

4. What will be the output of the following code?

#include <bits/stdc++.h> 
using namespace std; 
 
void func(int arr[], int left, int right) 
{     
	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	 
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}

a) 5 1 4 3
b) 3 5 1 4
c) 5 3 4 1
d) error
View Answer

Answer: c
Explanation: The given code reverses the input array and then prints the resulting array. So the output of the given code will be 5 3 4 1.

5. What will be the time complexity of the following code?

advertisement
#include <bits/stdc++.h> 
using namespace std; 
void func(int arr[], int left, int right) 
{ 
    	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	 
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}

a) O(n)
b) O(log n)
c) O(1)
d) O(n log n)
View Answer

Answer: a
Explanation: The given code reverses the input array and then prints the resulting array. So the time complexity of the given code will linearly vary with the number of elements in the array and thus the time complexity will be O(n).

6. What will be the auxiliary space requirement of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func(int arr[], int left, int right) 
{     
	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}

a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
View Answer

Answer: a
Explanation: The given code reverses the input array and then prints the resulting array. The given code does not use any extra array to complete this task thus the auxiliary space requirement is O(1).

7. What will be the output of the following code ?

#include <bits/stdc++.h> 
using namespace std; 
 
void func(int arr[], int left, int right) 
{ 
    if (left >= right) 
    return; 
 
    int temp = arr[left];  
    arr[left] = arr[right]; 
    arr[right] = temp; 
 
    func(arr, left + 1, right - 1);  
}      
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,2,3,4}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}

a) 1 2 3 4
b) 4 3 2 1
c) 1 4 2 3
d) 4 1 2 3
View Answer

Answer: b
Explanation: The given code reverses the original array and prints the resulting array. Recursive function is used to reverse the array.
advertisement

8. What will be the time complexity of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func(int arr[], int left, int right) 
{ 
    if (left >= right) 
    return; 
 
    int temp = arr[left];  
    arr[left] = arr[right]; 
    arr[right] = temp; 
 
    func(arr, left + 1, right - 1);  
}      
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,2,3,4}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}

a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
View Answer

Answer: b
Explanation: The given code reverses the original array and prints the resulting array. The number of swaps is proportional to the number of elements in the array so it requires a time complexity of O(n).

9. What will be the output of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func(int a[], int n, int k) 
{ 
	if (k <= n) 
	{ 
		for (int i = 0; i < k/2; i++) 
		swap(a[i], a[k-i-1]); 
	} 
 
} 
int main() 
{ 
	int a[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(a) / sizeof(int), k = 3; 
	func(a, n, k); 
	for (int i = 0; i < n; ++i) 
		cout << a[i]<<" ";
	return 0; 
}

a) 3 2 1 4 5
b) 5 4 3 2 1
c) 1 2 5 4 3
d) error
View Answer

Answer: a
Explanation: The given code reverses only a specified segment of the input array. As the value of k is given to be 3 in the code thus only the first three elements of the array will be reversed.

10. What will be the time complexity of the following code?

advertisement
#include <bits/stdc++.h> 
using namespace std; 
 
void func(int a[], int n, int k) 
{ 
	if (k <= n) 
	{ 
		for (int i = 0; i < k/2; i++) 
		swap(a[i], a[k-i-1]); 
	} 
 
} 
int main() 
{ 
	int a[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(a) / sizeof(int), k = 3; 
	func(a, n, k); 
	for (int i = 0; i < n; ++i) 
		cout << a[i]<<" ";
	return 0; 
}

a) O(k)
b) O(n)
c) O(k log k)
d) O(n log n)
View Answer

Answer: a
Explanation: The given code reverses only a specified segment of the input array. This segment is decided by the value of k so the time complexity of the code will be O(k).

11. When array reversal and rotation is applied to the same array then the output produced will also be the same every time.
a) true
b) false
View Answer

Answer: b
Explanation: Array rotation and array reversal are different operations and thus they give different outputs when applied to the same array.

12. Which of the following is the predefined function for array reversal in C++ ?
a) reverse()
b) arr_reverse()
c) array_reverse()
d) rev()
View Answer

Answer: a
Explanation: The predefined function for reversing an array is reverse() in C++. It is defined under the library algorithm and requires 2 arguments.

13. Which of the following is the predefined function for array reversal in javascript?
a) reverse()
b) arr_reverse()
c) array_reverse()
d) rev()
View Answer

Answer: a
Explanation: The predefined function for reversing an array is reverse() in javascript. It does not requires any argument.

14. Predefined function reverse() in C++ is available under which header file?
a) math
b) stdio
c) stdlib
d) algorithm
View Answer

Answer: d
Explanation: The predefined function for reversing an array is reverse() in C++ which comes under the library called an algorithm. It requires 2 arguments the first being the pointer to the starting index of the array and the second being the pointer to the last index of the array.

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn