Engineering Questions with Answers - Multiple Choice Questions

# Reversal Array Operation Multiple Choice MCQ

1 - Question

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

2 - Question

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 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 - Question

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 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 - Question

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);
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 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 - Question

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);
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 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 - Question

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);
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 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 - Question

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);
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 given code reverses the original array and prints the resulting array. Recursive function is used to reverse the array.

8 - Question

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);
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 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 - Question

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: 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 - Question

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: 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 - Question

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: Array rotation and array reversal are different operations and thus they give different outputs when applied to the same array.

12 - Question

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

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

13 - Question

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

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

14 - Question

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