wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

blackys

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

1. What kind of data can be sorted by the Bubble, Selection, and Insertion sort algorithms?

a)

All of these can be sorted

b)

Numbers

c)

Strings

d)

Other objects

2.

2. Which sorting algorithm relies on swapping pairs of side-by-side elements that are out of order?

a)

Bubble sort

b)

Selection sort

c)

Insertion sort

d)

Merge sort

3.

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.

a)

Selection sort

b)

Bubble sort

c)

Insertion sort

d)

Merge sort

4.

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?

a)

Insertion sort

b)

Bubble sort

c)

Selection sort

d)

Merge sor

5.

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?

a)

1

b)

0

c)

N

d)

N / 2

6.

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 }

a)

1, 4, 5, 8

b)

1, 5, 8, 4

c)

8, 1, 4, 5

d)

5, 1, 4, 8

7.

7. What is one simple way to understand the efficiency of an algorithm?

a)

Do a rough statement execution count, focusing on the innermost loop

b)

Calculate the amount of memory that will be consumed during the algorithm

c)

Look at the size of the compiled .class file that contains the Java algorithm

d)

All of these will work equally well

8.

8. How would you change an algorithm that uses a simple 1D array to use an ArrayList instead?

a)

Both of these steps should be taken Java wrapper classes in the

b)

Replace simple array tasks like getting or setting values or finding the array length with the equivalent methods on the

c)

ArrayList If the simple array contains primitive values, use the corresponding

d)

ArrayList Neither of these steps should be taken

9.

9. On each pass, when the Selection sort scans the array to the right of the starting position, what is it looking for?

a)

The smallest number to the right

b)

The largest number to the right

c)

A number that matches the value at the starting point

d)

Any of these could be true, depending on how the initial data is stored

10.

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 }

a)

1, 4, 8, 5

b)

1, 8, 4, 5

c)

4, 1, 5, 8

d)

5, 1, 4, 8

11.

11. Under what condition will the Selection sort not make any swaps when it makes a pass through the array?

a)

If the value at the starting index is already less than or equal to all remaining values to the right

b)

If the value at the starting index is already less than or equal to all previous values to the left

c)

If the value at the starting index and the value at the next element to the right are equal

d)

The Selection sort is guaranteed to make at least one swap per pass

12.

12. The overall efficiency of the Selection sort with "N" elements is best described by what formula?

a)

N2 (N-squared)

b)

N * 2

c)

N / 2

d)

N + 2

13.

13. What does it mean to compare two strings "lexicographically "?

a)

Compare the underlying Unicode character numbers position by position to see which string is considered greater than, less than, or equal to the other

b)

Compare the two strings to see which is longer

c)

Compare the underlying encoding schemes to see which string is using

d)

Unicode and which is using ASCII Compare the strings to see which one has more capital letters

14.

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?

a)

Use a loop that iterates backward, copying each element one space to the right

b)

Use a loop that iterates forwards, copying each element one space to the right

c)

Call the slide() method on the array or ArrayList object

d)

Use nested loops to swap neighboring elements

15.

15. Between the Bubble sort, the Selection sort, and the Insertion sort, which algorithm has the best general efficiency rating?

a)

They are all about the same efficiency

b)

Bubble sort

c)

Insertion sort

d)

Selection sort

16.

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 }

a)

Sequential search

b)

Binary search

c)

Both of these would work

d)

Neither of these would work

17.

17. Which of the following best describes the sequential search algorithm?

a)

One for() loop that compares each element in sequence until the target value is found

b)

One for() loop that discards half of the target array on each step

c)

One while() loop that randomly selects an index within the array and compares that value, continuing until the value is found

d)

Nested for() loops that will swap elements until the target values is located at the front of the array

18.

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 }

a)

5

b)

7

c)

2

d)

The sequential search cannot be run on unsorted data

19.

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 }

a)

4

b)

2

c)

8

d)

The binary search cannot be used on sorted data

20.

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?

a)

(lowIndex + highIndex) / 2

b)

lowIndex + 1

c)

highIndex - 1

d)

highIndex - lowIndex

21.

1. Which of the following statements will create a 2x3 2D array of integers named "numbers"?

a)

int[][] numbers = new int[2][3];

b)

int[][] numbers = new int(2,3);

c)

int[2][3] numbers;

d)

int numbers = new int[2][3]

22.

2. Given the code below, how would you read the value 3 out of the array? int[][] numbers = { {1,2,3}, {4,5,6}};

a)

int three = numbers[0][2];

b)

int three = numbers[1][3];

c)

int three = numbers(1,2);

d)

int three = numbers[0][3];

23.

3. If a 2D array has 3 rows and 4 columns, what are the valid index values in that array?

a)

row 0 to 2, column 0 to 3

b)

row 1 to 3, column 1 to 4

c)

row 0 to 3, column 0 to 4

d)

row 1 to 2, column 1 to 3

24.

4. Given the 2D array named "block", which statement will correctly get the number of rows in the array?

a)

int numRows = block.length;

b)

int numRows = block.size;

c)

int numRows = block[0].size;

d)

int numRows = block[0].length();

25.

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];

a)

pictures[0][0] = new Image();

b)

pictures(0,0) = new Image();

c)

pictures.image = new Image();

d)

Not necessary, the pictures array is filled with valid Image objects when it is created

26.

6. What kind of traversal will use an outer for() loop over all rows and an inner for() loop over all columns?

a)

Row-major traversal

b)

Column-major traversal

c)

Sequential traversal

d)

Nested traversal

27.

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

}

}

a)

123456

b)

654321

c)

142536

d)

415263

28.

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

}

}

a)

142536

b)

123456

c)

654321

d)

415263

29.

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'}};

a)

Column-major order

b)

Row-major order

c)

Horizontal order

d)

Random order

30.

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];

a)

for (int[] row : numbers)

b)

for (int row : numbers)

c)

for (number : int row)

d)

for (int row = numbers; row < numbers.length; numbers++)

31.

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?

a)

Your 2D array contains object references instead of primitive values

b)

You need to modify the values stored in the 2D array elements during traversal

c)

You only want to process part of the 2D array or traverse it backward

d)

You need to know the index values for each element as you process the element

32.

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?

a)

double[]

b)

double

c)

java.lang.Double()

d)

void

33.

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()}};

a)

Nothing is wrong; this code will fully initialize the pictures array

b)

The code fails to specify the size of each dimension inside the square brackets

c)

The curly braces define more dimensions than are present in the Image[][] data type

d)

You cannot create new objects as the values in an array initializer list

34.

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}};

a)

numbers[1]

b)

numbers(1)

c)

numbers.row(1)

d)

numbers

35.

15. A cube is a good visualization for an array with how many dimensions?

a)

3

b)

2

c)

1

d)

4

36.

16. What term is used to describe the following 2D array?

int[][] numbers = {

{1},

{2,3,4},

{5,6}}

a)

Ragged

b)

Bumpy

c)

Inconsistent

d)

Illegal

37.

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?

a)

Reversed the entire 2D array

b)

Randomized the array contents

c)

Returned the array values back to their original starting points

d)

Created a mirror image of the original array

38.

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"

a)

To sequentially search the 2D array one row at a time, stopping when the targetValue is found

b)

To report the position of the targetValue in every row in the targetArray

c)

To report the position of the targetValue in every column in the targetArray

d)

To count the number of times the targetValue is found in the entire targetArray

39.

19. Which of the following algorithms produces a row and column index as output?

a)

Sequential 2D array search

b)

Reverse 2D array

c)

Count target value in 2D array

d)

Binary 1D array search

40.

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?

a)

Iterate over the entire 2D array with nested for() loops, increasing a count each time the current element contains the target value

b)

Iterate over the entire 2D array with a single for() loop, printing a message each time the target value is found

c)

Iterate over the array row-by-row, printing a success message and halting when the target element is found

d)

Iterate over the entire 2D array with nested for() loops, adding together the values found in each element, and return the total value