NEW
Font size
S
M
L
XL
WorksheetsVCE-BETA-20.11.2023-AN
Total questions: 15
Worksheet time: 15mins
Name
Class
Date
1.
For the following question, how will the array elements look like after second pass if sorted using Insertion Sort?
34, 8, 64, 51, 32, 21
a)
8, 21, 32, 34, 51, 64
b)
8, 32, 34, 51, 64, 21
c)
8, 34, 51, 64, 32, 21
d)
8, 34, 64, 51, 32, 21
2.
If the given input array is sorted or nearly sorted, which of the following algorithm gives the best performance?
a)
Insertion sort
b)
Selection sort
c)
Quick sort
d)
Merge sort
3.
The way a card game player arranges his cards as he picks them up one by one, is an example of
a)
bubble sort
b)
Selection sort
c)
insertion sort
d)
merge sort
4.
As part of the maintenance work, you are entrusted with the work of rearranging the library books in a shelf in proper order, at the end of each day. The ideal choice will be
a)
bubble sort
b)
insertion sort
c)
selection sort
d)
Quick sort
5.
int binarySearch(int arr[], int left, int right, int target) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
return binarySearch(arr, mid + 1, right, target);
else
return binarySearch(arr, left, mid - 1, target);
}
return -1;
}
What type of binary search does this code snippet represent?
a)
Iterative binary search
b)
Recursive binary search
c)
Linear search
d)
Exponential search
6.
int func(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
What is the primary purpose of the code snippet?
a)
Sorting an array in ascending order
b)
Searching for a target element in a sorted array
c)
Calculating the sum of elements in an array
d)
Reversing the order of elements in an array
7.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid;
else
right = mid;
}
return -1;
}
What issue or error is present in this code snippet?
a)
There is no issue; the code is correct
b)
The loop condition should be left <= right
c)
The left and right updates should be left = mid + 1 and right = mid - 1
d)
The return value is not correct
8.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid;
}
return -1;
}
What is the primary issue with this code snippet?
a)
The code is correct; there are no issues
b)
The loop condition should be left <= right
c)
The left and right updates should be left = mid + 1 and right = mid - 1
d)
The target element is not correctly returned
9.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
If you apply this binary search code snippet to a sorted array with 10 elements, how many iterations will it take to find the target element if it exists in the array?
a)
1
b)
2
c)
3
d)
4
10.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid + 1;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return 0;
}
If you apply the above code snippet to a sorted array with 12 elements, and the target is not found, what will be the return value?
a)
0
b)
1
c)
-1
d)
12
11.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
In the binary search code snippet above, what is the time complexity of the algorithm in terms of the input size n?
a)
O(1)
b)
O(n)
c)
O(log n)
d)
O(n log n)
12.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n;
int comparisons = 0;
while (left < right) {
int mid = left + (right - left) / 2;
comparisons++;
if (arr[mid] == target)
return comparisons;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid;
}
return -1;
}
In Code Snippet, if you apply this binary search code to a sorted array with 32 elements and the target is found, how many comparisons will be made in the worst case?
a)
5
b)
6
c)
4
d)
3
13.
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
int iterations = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
iterations++;
if (arr[mid] == target)
return iterations;
else if (arr[mid] < target)
left = mid;
else
right = mid;
}
return -1;
}
In Code Snippet, if you apply this binary search code to a sorted array with 16 elements and the target is found, how many iterations will be made?
a)
4
b)
5
c)
3
d)
2
14.
What is the output of following program, if n=8 and j=1
void print(int n, int j)
{
if (j >= n)
return;
if (n-j > 0 && n-j >= j)
printf("%d %d,", j, n-j);
print(n, j+1);
}
a)
1 7, 2 6, 3 5, 4 4, 4 4
b)
1 7, 2 6, 3 5, 4 4
c)
1 7, 2 6, 3 5
d)
1 2, 3 4, 5 6, 7 8
15.
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
In Code Snippet, if you apply this linear search code to an array of 15 elements and the target is found at the 5th position, what value will be returned?
a)
0
b)
1
c)
4
d)
5
Reset
