placeholder image to represent content

Unit 7 - Algorithms

Quiz by Sarah Gagnier

Our brand new solo games combine with your quiz, on the same screen

Correct quiz answers unlock more play!

New Quizalize solo game modes
22 questions
Show answers
  • Q1
    Consider 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?
    18
    31
    51
    33
    45s
  • Q2
    Consider 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?
    32
    55
    18
    33
    45s
  • Q3
    Consider 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 3
    0 0 1 2 3 1 2 3 3
    0 1 2 3 0 1 2 3 0 1
    0 0 1 1 2 2 3 3
    45s
  • Q4
    Consider 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?
    22
    18
    6
    4
    45s
  • Q5
    Considering 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 17
    I
    III
    I
    II
    45s
  • Q6
    Consider 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
    -4
    45s
  • Q7
    Which 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
  • Q8
    Consider 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?
    4
    3
    7
    5
    45s
  • Q9
    Assuming 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 only
    II and III only
    I only
    III only
    45s
  • Q10
    Consider 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
  • Q11
    The 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
  • Q12
    Consider 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)?
    11
    45s
  • Q13
    What operation does the insertion sort use?
    search
    modular division
    swap
    overloading
    45s
  • Q14
    For 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};
    10
    45s
  • Q15
    Consider 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?
    4
    45s

Teachers give this quiz to your class