Reversal Array Operation Multiple Choice Questions and Answers (MCQs)

Uncategorized

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

1. 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)

Explanation: The provided code reverses the input array before printing the output. Since the provided code does not use any additional arrays to complete this mission, the required auxiliary space is O. (1).

2. 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

Explanation: The provided code prints the product of reversing the original array. The array is reversed using the recursive function.

3. 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)

Explanation: The provided code prints the product of reversing the original array. Due to the fact that the number of swaps is proportional to the number of elements in the array, it has an O time complexity (n).

4. 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

Explanation: Only a specific section of the input array is reversed by the given code. Only the first three elements of the array will be reversed since the value of k is set to 3 in the code.

5. What will be the time complexity 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) O(k)
B) O(n)
C) O(k log k)
D) O(n log n)

Explanation: Only a specific section of the input array is reversed by the given code. Since the value of k determines this section, the code’s time complexity will be O. (k).

6. 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

Explanation: When applied to the same array, array rotation and array reversal are two separate operations that produce different results.

7. Which of the following is the predefined function for array reversal in C++ ?
A) reverse()
B) arr_reverse()
C) array_reverse()
D) rev()

Explanation: In C++, reverse() is a built-in function for reversing an array. It has two arguments and is defined by the library algorithm.

8. Which of the following is the predefined function for array reversal in javascript?
A) reverse()
B) arr_reverse()
C) array_reverse()
D) rev()

Explanation: In javascript, reverse() is a built-in feature for reversing an array. It does not necessitate any debate.

9. Predefined function reverse() in C++ is available under which header file?
A) math
B) stdio
C) stdlib
D) algorithm

Explanation: In C++, the predefined feature for reversing an array is reverse(), which is part of the algorithm library. It takes two arguments: the first is a pointer to the array’s starting index, and the second is a pointer to the array’s ending index.

10. 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

Explanation: After reversal, the resulting sequence is arr[]=2,4,5,3. We may use a variety of methods to implement an algorithm for this purpose.

11. 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

Explanation: The number of swaps needed for an odd element array and an even element array differs because the location of the middle element in an odd element array does not need to be modified. As a result, (n-1) / 2 will be the number of swaps.

12. 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

Explanation: The number of swaps needed for an odd element array and an even element array differs because the location of the middle element in an odd element array does not need to be modified. As a result, the number of swaps required will be n/2.

13. 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

Explanation: The provided code reverses the input array before printing the output. As a result, the given code’s output will be 5 3 4 1.

14. 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) 
{ 
    	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)

Explanation: The provided code reverses the input array before printing the output. As a result, the time complexity of the given code will linearly increase with the number of elements in the sequence, resulting in an O time complexity (n).

The JavaScript array reverse() method reverses the order of the elements in an array and returns the original order. To put it another way, the last element of the array becomes the first, and the first element becomes the last. The adjustments in the original array were also made by this process. The reversal algorithm is one of the array rotation algorithms. Subarrays are generated and reversed in this algorithm to perform array rotation. To get the rotated array, subarrays are formed, rotated individually, then joined together and reversed.

Leave a Reply

Your email address will not be published. Required fields are marked *