What is the time complexity of finding the maximum element in a sorted array?
O(n)
O(log n)
O(1)
O(n log n)
In an array with indices starting at 0, what is the index of the last element in an array of size 'n'?
It depends on the data type of the array.
0
n
n - 1
Imagine a 2D array representing a grayscale image. Each element holds a pixel intensity value. How would you swap two rows 'r1' and 'r2' in this array?
Swapping rows is not possible in a 2D array.
Directly assign 'image[r1]' to 'image[r2]' and vice-versa.
Create a new 2D array with the swapped rows.
Iterate through columns, swapping elements at 'image[r1][j]' and 'image[r2][j]' for each column 'j'.
Which code snippet correctly initializes a 2D array named 'grid' with 3 rows and 4 columns, all filled with zeros?
int grid[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) grid[i][j] = 0;
int grid[3][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int grid[3][4] = {0};
int grid[3][4] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
What is the primary advantage of using binary search over linear search?
Binary search works on unsorted arrays.
Binary search is generally faster for large arrays.
Binary search uses less memory.
Binary search is simpler to implement.
If you have an array of size 5 and you try to add a 6th element to it, what is a likely outcome?
An error or exception will occur indicating an out-of-bounds access.
The behavior is undefined and can lead to unpredictable program crashes.
The array will automatically resize to accommodate the new element.
The new element will overwrite the value at the first index (index 0).
Which sorting algorithm works by repeatedly selecting the minimum element and placing it in its correct position?
Merge Sort
Bubble Sort
Quick Sort
Selection Sort
Accessing an element outside the valid index range of an array leads to what kind of error?
ValueError
IndexError or OutOfBounds exception
SyntaxError
TypeError
You are given a 2D array representing a matrix. What does transposing this matrix mean?
Sorting the matrix in ascending order.
Reversing all elements in the matrix.
Finding the sum of all elements in the matrix.
Swapping rows and columns of the matrix.
Which of the following sorting algorithms has the best average-case time complexity?
Insertion Sort