NEW
Font size
Worksheetsblackys
Total questions: 40
Worksheet time: 20mins
1. What kind of data can be sorted by the Bubble, Selection, and Insertion sort algorithms?
All of these can be sorted
Numbers
Strings
Other objects
2. Which sorting algorithm relies on swapping pairs of side-by-side elements that are out of order?
Bubble sort
Selection sort
Insertion sort
Merge sort
3. Which sorting algorithm iterates through the array, starting one element to the right each time. On each iteration, it scans all elements to the right of the starting point for a smaller value, and swaps it with the starting position if found.
Selection sort
Bubble sort
Insertion sort
Merge sort
4. Which sorting algorithm makes a single pass through the array, starting at the second element, and moving that element backward while the numbers to the left are greater?
Insertion sort
Bubble sort
Selection sort
Merge sor
5. If the Bubble Sort algorithm begins working on an array with "N" elements that is already completely sorted, how many loops will it need to complete over the entire array?
1
0
N
N / 2
6. Given the starting values below, what will the order become after the first complete pass through the array by the Bubble Sort algorithm? { 4, 1, 8, 5 }
1, 4, 5, 8
1, 5, 8, 4
8, 1, 4, 5
5, 1, 4, 8
7. What is one simple way to understand the efficiency of an algorithm?
Do a rough statement execution count, focusing on the innermost loop
Calculate the amount of memory that will be consumed during the algorithm
Look at the size of the compiled .class file that contains the Java algorithm
All of these will work equally well
8. How would you change an algorithm that uses a simple 1D array to use an ArrayList instead?
Both of these steps should be taken Java wrapper classes in the
Replace simple array tasks like getting or setting values or finding the array length with the equivalent methods on the
ArrayList If the simple array contains primitive values, use the corresponding
ArrayList Neither of these steps should be taken
9. On each pass, when the Selection sort scans the array to the right of the starting position, what is it looking for?
The smallest number to the right
The largest number to the right
A number that matches the value at the starting point
Any of these could be true, depending on how the initial data is stored
10. Given the starting values below, what will the order become after the first complete pass through the array by the Selection Sort algorithm? { 4, 1, 8, 5 }
1, 4, 8, 5
1, 8, 4, 5
4, 1, 5, 8
5, 1, 4, 8
11. Under what condition will the Selection sort not make any swaps when it makes a pass through the array?
If the value at the starting index is already less than or equal to all remaining values to the right
If the value at the starting index is already less than or equal to all previous values to the left
If the value at the starting index and the value at the next element to the right are equal
The Selection sort is guaranteed to make at least one swap per pass
12. The overall efficiency of the Selection sort with "N" elements is best described by what formula?
N2 (N-squared)
N * 2
N / 2
N + 2
13. What does it mean to compare two strings "lexicographically "?
Compare the underlying Unicode character numbers position by position to see which string is considered greater than, less than, or equal to the other
Compare the two strings to see which is longer
Compare the underlying encoding schemes to see which string is using
Unicode and which is using ASCII Compare the strings to see which one has more capital letters
14.The Insertion sort relies on sliding elements to the right. When you are using an array or ArrayList to hold your data, how can this sliding be accomplished in Java code?
Use a loop that iterates backward, copying each element one space to the right
Use a loop that iterates forwards, copying each element one space to the right
Call the slide() method on the array or ArrayList object
Use nested loops to swap neighboring elements
15. Between the Bubble sort, the Selection sort, and the Insertion sort, which algorithm has the best general efficiency rating?
They are all about the same efficiency
Bubble sort
Insertion sort
Selection sort
16. Given the following starting data set, what search algorithm would you use to find a target value? { 15, 9, 1, 14, 12, 8, 10, 6, 5, 3 }
Sequential search
Binary search
Both of these would work
Neither of these would work
17. Which of the following best describes the sequential search algorithm?
One for() loop that compares each element in sequence until the target value is found
One for() loop that discards half of the target array on each step
One while() loop that randomly selects an index within the array and compares that value, continuing until the value is found
Nested for() loops that will swap elements until the target values is located at the front of the array
18. Given the following starting data set, how many steps would it take the sequential search algorithm to find the value 12? { 15, 9, 1, 14, 12, 8, 10, 6, 5, 3 }
5
7
2
The sequential search cannot be run on unsorted data
9. Given the following starting data set, how many steps would it take for a binary search to find the number 8? { 1, 2, 3, 4, 5, 6, 7, 8 }
4
2
8
The binary search cannot be used on sorted data
20.If the binary search algorithm is using "lowIndex" and "highIndex" variables to track the current lowerand upper boundaries within the array, what calculation would it use to select a value for comparisonon each pass?
(lowIndex + highIndex) / 2
lowIndex + 1
highIndex - 1
highIndex - lowIndex
1. Which of the following statements will create a 2x3 2D array of integers named "numbers"?
int[][] numbers = new int[2][3];
int[][] numbers = new int(2,3);
int[2][3] numbers;
int numbers = new int[2][3]
2. Given the code below, how would you read the value 3 out of the array? int[][] numbers = { {1,2,3}, {4,5,6}};
int three = numbers[0][2];
int three = numbers[1][3];
int three = numbers(1,2);
int three = numbers[0][3];
3. If a 2D array has 3 rows and 4 columns, what are the valid index values in that array?
row 0 to 2, column 0 to 3
row 1 to 3, column 1 to 4
row 0 to 3, column 0 to 4
row 1 to 2, column 1 to 3
4. Given the 2D array named "block", which statement will correctly get the number of rows in the array?
int numRows = block.length;
int numRows = block.size;
int numRows = block[0].size;
int numRows = block[0].length();
5. Assuming the Image class has been defined, given the following 2D array, what statement would be needed to create a valid Image object in the element at row 0, column 0? Image[][] pictures = new Image[4][4];
pictures[0][0] = new Image();
pictures(0,0) = new Image();
pictures.image = new Image();
Not necessary, the pictures array is filled with valid Image objects when it is created
6. What kind of traversal will use an outer for() loop over all rows and an inner for() loop over all columns?
Row-major traversal
Column-major traversal
Sequential traversal
Nested traversal
7. Given the code below, which answer describes the resulting output?
int[][] numbers = {
{1,2,3}, {4,5,6}};
for (int row=0; row < numbers.length; row++)
{
for (int col=0; col < numbers[row].length; col++)
{
System.out.print(numbers[row][col]);
}
}
123456
654321
142536
415263
8. Given the code below, which answer describes the resulting output?
int[][] numbers = {
{1,2,3}, {4,5,6}};
for (int col=0; col < numbers[0].length; col++)
{
for (int row=0; row < numbers.length; row++)
{
System.out.print(numbers[row][col]);
}
}
142536
123456
654321
415263
9. Given the 2D array below, if an algorithm traverses the array and prints the element values "ACBD" as a result, how would you describe that traversal?
char[][] letters = {
{'A','B'},
{'C','D'}};
Column-major order
Row-major order
Horizontal order
Random order
10. Given the following 2D array, which of the following statements would set up an outer, enhanced for() loop to iterate over all of the rows?
int[][] numbers = new int[2][3];
for (int[] row : numbers)
for (int row : numbers)
for (number : int row)
for (int row = numbers; row < numbers.length; numbers++)
11.Which of the following conditions would not cause you to use standard for() loops instead of enhanced for() loops to traverse a 2D array?
Your 2D array contains object references instead of primitive values
You need to modify the values stored in the 2D array elements during traversal
You only want to process part of the 2D array or traverse it backward
You need to know the index values for each element as you process the element
12. In a 2D array of primitive "double" values, which of the following represents the data type of each of the elements in the first dimension?
double[]
double
java.lang.Double()
void
13. If the Image class has been defined, what is wrong with the following code? Image[][] pictures = { {new Image(), new Image()}, {new Image(), new Image()}};
Nothing is wrong; this code will fully initialize the pictures array
The code fails to specify the size of each dimension inside the square brackets
The curly braces define more dimensions than are present in the Image[][] data type
You cannot create new objects as the values in an array initializer list
14. Given the 2D array declared below, which answer represents a reference to the entire row stored at index 1?
int[][] numbers = {
{1,2,3},
{4,5,6}};
numbers[1]
numbers(1)
numbers.row(1)
numbers
15. A cube is a good visualization for an array with how many dimensions?
3
2
1
4
16. What term is used to describe the following 2D array?
int[][] numbers = {
{1},
{2,3,4},
{5,6}}
Ragged
Bumpy
Inconsistent
Illegal
17.Given a 2D array, if an algorithm reverses the elements in individual rows from left to right, then reverses the order of the rows from top to bottom, what have you done?
Reversed the entire 2D array
Randomized the array contents
Returned the array values back to their original starting points
Created a mirror image of the original array
18. Study the pseudocode below. Given targetArray as some 2D array, what is the purpose of this algorithm?
DECLARE targetValue AS 42
FOR EACH row in targetArray
SET col AS CALL sequentialSearch1D(row, targetValue)
IF result GREATER THAN OR EQUAL TO 0 THEN
PRINT "Result: row " + row + ", col " + col
RETURN
ENDIF
ENDFOR
PRINT "No Result"
To sequentially search the 2D array one row at a time, stopping when the targetValue is found
To report the position of the targetValue in every row in the targetArray
To report the position of the targetValue in every column in the targetArray
To count the number of times the targetValue is found in the entire targetArray
19. Which of the following algorithms produces a row and column index as output?
Sequential 2D array search
Reverse 2D array
Count target value in 2D array
Binary 1D array search
20. Which of the following best describes how an algorithm to count the number of times a target value is found a 2D array would work?
Iterate over the entire 2D array with nested for() loops, increasing a count each time the current element contains the target value
Iterate over the entire 2D array with a single for() loop, printing a message each time the target value is found
Iterate over the array row-by-row, printing a success message and halting when the target element is found
Iterate over the entire 2D array with nested for() loops, adding together the values found in each element, and return the total value
