
Unit 7 - Algorithms
Quiz by Sarah Gagnier
Feel free to use or edit a copy
includes Teacher and Student dashboards
Measure skillsfrom any curriculum
Measure skills
from any curriculum
Tag the questions with any skills you have. Your dashboard will track each student's mastery of each skill.
With a free account, teachers can
- edit the questions
- save a copy for later
- start a class game
- automatically assign follow-up activities based on students’ scores
- assign as homework
- share a link with colleagues
- print as a bubble sheet
22 questions
Show answers
- Q1Consider the following code segment. int[] list = { 1, 6, 23, 7, 3, 5, 6 }; int t = 0; for (int i = 0; i < list.length; i++) t = t + list[i]; What is the value of t after this code segment has executed?1831513345s
- Q2Consider the following code segment. int[] list = { 1, 6, 23, 7, 3, 5, 6 }; int t = 0; for (int i = 0; i < list.length; i += 2) t = t + list[i]; What is the value of t after this code segment has executed?3255183345s
- Q3Consider the following code segment. for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { System.out.print(i + " "); } } If the variable n is equal to 4, what is the output?0 1 1 2 2 2 3 3 3 30 0 1 2 3 1 2 3 30 1 2 3 0 1 2 3 0 10 0 1 1 2 2 3 345s
- Q4Consider the following code segment. int a = 18; int b = 42; while (b != 0) { int r = a % b; a = b; b = r; } System.out.println(a); What is printed when this code segment is executed?22186445s
- Q5Considering the following code segments. I. int a = 1; while (a < 20) { if (a % 4 == 1) { System.out.print(a + " "); } a = a + 4; } II. for (int a = 0; a < 20; a++) { if (a % 4 == 0) { System.out.print(a + " "); } } III. for (int a = 0; a < 20; a = a + 4) { System.out.print(a + " "); } Which of the code segments will output the following? 1 5 9 13 17IIIIIII45s
- Q6Consider the following definition for an array of integers. int[] a = { -4, 32, -13, 32, -10, 17, 22, 21, 30, 24, -5 }; What is stored in a[1]?32-10-13-445s
- Q7Which statement prints the last element in the array?System.out.println(a[a.length - 1]);System.out.println(a[0] - 1);System.out.println(a[a.length]);System.out.println(a[0]);45s
- Q8Consider the following definition for an array of integers. int[] a = { 3, 4, 6463, 4, 7, 8, 5, 3, 2, 3, 6 }; If you were to do a linear search for 7, how many comparisons would be made?437545s
- Q9Assuming all variables are declared correctly, which of the following code segments correctly swaps the values x and y? I. x = y; y = x; II. int tmp = x; y = x; x = tmp; III. int tmp = x; x = y; y = tmp;II onlyII and III onlyI onlyIII only45s
- Q10Consider the following code: int a [] = /* initialization not shown*/; int flag = 0; for (int i = 1; i < a.length; i++) { if (a[i] < a[i-1]) { flag = 1; break; } } What does it do?Nothing, the loop does not finish running.Tests if the array is in ascending order.Tests if the array is in descending order.Tests if all values in the array are equal.45s
- Q11The following code is intended to sort an input array of integers into ascending order. int j, k; for (j = a.length - 1; j > 0; j--) { int pos = j; for (/* missing code */) { if (a[k] > a[pos]) { pos = k; } } swap(a, j, pos); } Assume swap(a, j, pos) swaps the values of units j and pos in array a. Which of the following code segments, if replaced for /* missing code */, will make the code run properly?k=1; k>a.length; k++k=0; k<=a.length-1; k++k=j-1; k>0; k--k=j-1; k>=0; k--45s
- Q12Consider the following array and method declarations. int[] list = {1, 3, 5, 6, 7, 8, 11, 13, 14, 15, 16, 17, 20, 22, 24, 29, 30, 31, 33, 35}; public static int mystery(int[] a, int s) { for (int i = 0; i < a.length; i++) { if (a[i] == s) { return i; } } return -1; } What would be returned by the following method call? mystery(list,17)?1145s
- Q13What operation does the insertion sort use?searchmodular divisionswapoverloading45s
- Q14For the following array, what is the maximum number of comparisons a linear search will need to do? int[] a= {2, 8, 59, 20, 128, 3, 6, 3, 7, 1};1045s
- Q15Consider the array declaration that follows. int[] a = {1, 3, 5, 6, 7, 8, 9, 11, 13, 14, 15}; How many comparisons would it take if you were to search the array for 3 using a binary search?445s