Free Practice Quiz Question List

Iteration and Algorithmic Patterns Online Quiz Questions

Use this free practice quiz with 30 questions to review Iteration and Algorithmic Patterns, test your knowledge, and prepare for your next test or exam.

30 questions
01
Choose one
1 point

In Java, what is normally true of the variable declared in for (int i = 0; i < 3; i++)?

  1. A

    It can be used anywhere in the class.

  2. B

    It can be used after the loop but not inside it.

  3. C

    It normally exists only inside the loop.

  4. D

    It is automatically converted into a class variable.

02
Choose all
1 point

Select all statements that are true about Java while and do-while loops.

  1. A

    A do-while loop executes its body at least once.

  2. B

    A while loop always executes its body at least once.

  3. C

    A while loop can execute its body zero times.

  4. D

    A do-while loop tests its condition before its body.

03
Choose all
1 point

Select all correct statements about these nested-loop patterns.

  1. A

    A 3-by-4 nested loop executes its inner body 7 times.

  2. B

    A 3-by-4 nested loop executes its inner body 12 times.

  3. C

    A triangular loop with row values 1 through 4 executes its inner body 16 times.

  4. D

    A triangular loop with row values 1 through 4 executes its inner body 10 times.

04
True or false
1 point

True or false: In a sentinel-controlled loop, the sentinel should normally be tested before it is processed.

  1. A

    True

  2. B

    False

05
Written response
1 point

Which Java loop keyword names a pretest loop that may execute its body zero times? Enter the keyword in lowercase.

06
Fill in the blank
1 point

Complete the statement with the final valid index expression for a string of length n: the valid indexes range from 0 through .

07
Fill in the blank
1 point

In the digit-reversal algorithm, complete both blanks: number % extracts the last decimal digit, and reversed * + digit appends that digit to the reversed number.

08
Open ended
1 point

Explain how to implement a correct linear search for a target in an array. Your answer must describe the stopping conditions and explain how an optional break should be used.

09
Choose one
1 point

What is printed by this code? int number = 10; do { System.out.println(number); number++; } while (number < 10);

  1. A

    The body executes zero times because the condition is false.

  2. B

    The body executes twice because the condition is tested after the body.

  3. C

    The body executes once because the condition is tested after the body.

  4. D

    The program fails to compile because the condition is initially false.

10
True or false
1 point

True or false: In for (int i = 0; i < 3; i++) { System.out.println(i); }, the variable i can normally be used in a statement after the loop.

  1. A

    True

  2. B

    False

11
Choose one
1 point

What is the final value of result after this code executes? int result = 0; for (int i = 2; i <= 6; i += 2) { result += i; }

  1. A

    6

  2. B

    12

  3. C

    14

  4. D

    18

12
True or false
1 point

True or false: In a sentinel-controlled loop that adds input values to a sum, the sentinel should normally be checked before it is added.

  1. A

    True

  2. B

    False

13
Written response
1 point

How many times does the counter become incremented in this code? String word = "banana"; int count = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'a') { count++; } } Enter the whole-number count.

14
Fill in the blank
1 point

An iterative algorithm typically contains initialization, condition, body, and .

15
Choose all
1 point

Select all statements that correctly describe nested loops.

  1. A

    The inner loop completes all of its iterations before the outer loop advances.

  2. B

    The inner-loop variable keeps its final value from the previous outer iteration.

  3. C

    The inner-loop variable is reinitialized when the outer loop starts a new iteration.

  4. D

    If the inner bound depends on the outer variable, the total may be triangular rather than a simple product.

16
Written response
1 point

What algorithm examines array elements one at a time until it finds a target or reaches the end? Enter the algorithm name.

17
True or false
1 point

True or false: In while (x < 5); { x++; }, the statement x++ executes as the body of the while loop.

  1. A

    True

  2. B

    False

18
Choose one
1 point

What is the informal run-time growth of this algorithm as n increases? for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { /* work */ } }

  1. A

    Constant growth

  2. B

    Linear growth

  3. C

    Logarithmic growth

  4. D

    Quadratic growth

19
Written response
1 point

What whole number is printed by this code? int number = 472; int reversed = 0; while (number > 0) { int digit = number % 10; reversed = reversed * 10 + digit; number /= 10; } System.out.println(reversed);

20
Written response
1 point

In the pattern that initializes a result with values[0] and replaces it whenever a later value is larger, which quantity is being computed? Enter the algorithm term.

21
True or false
1 point

True or false: Initializing a maximum-finding variable to 0 is always correct, regardless of the values in the array.

  1. A

    True

  2. B

    False

22
Choose one
1 point

What happens when this loop processes the target value? boolean found = false; for (int value : values) { if (value == target) { found = true; break; } }

  1. A

    The loop exits before checking the target, so found remains false.

  2. B

    The loop sets found to true and then exits immediately.

  3. C

    The loop skips the matching value and continues through all later values.

  4. D

    The loop restarts from the first array element after the match.

23
Choose one
1 point

What is printed by this Java code?

int x = 5; while (x < 5) { System.out.println(x); x++; }

  1. A

    It prints 5 once.

  2. B

    It prints nothing.

  3. C

    It prints the numbers 1 through 5.

  4. D

    It enters an infinite loop.

24
Choose one
1 point

What is the final value of result?

int result = 0; for (int i = 2; i <= 6; i += 2) { result += i; }

  1. A

    6

  2. B

    8

  3. C

    12

  4. D

    14

25
Choose one
1 point

What does this code print?

int number = 10; do { System.out.println(number); number++; } while (number < 10);

  1. A

    It prints nothing.

  2. B

    It prints 10 repeatedly.

  3. C

    It prints 11 once.

  4. D

    It prints 10 once.

26
Choose one
1 point

What is the final value of vowels?

String text = "Computer"; int vowels = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; } }

  1. A

    3

  2. B

    2

  3. C

    4

  4. D

    8

27
Choose one
1 point

How many times does System.out.print("* ") execute?

for (int row = 1; row <= 3; row++) { for (int column = 1; column <= 4; column++) { System.out.print("* "); } }

  1. A

    7

  2. B

    12

  3. C

    16

  4. D

    9

28
Choose one
1 point

Consider this algorithm as n increases:

for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { // work } }

What is the best informal description of its run-time growth?

  1. A

    Constant growth

  2. B

    Linear growth

  3. C

    Quadratic growth

  4. D

    Exponential growth

29
Written response
1 point

What integer is printed? Enter the exact integer; no tolerance applies.

int number = 472; int reversed = 0; while (number > 0) { int digit = number % 10; reversed = reversed * 10 + digit; number /= 10; } System.out.println(reversed);

30
Choose one
1 point

A method must print every integer from 1 through 20, including both endpoints, exactly once. Which Java loop accomplishes this?

  1. A

    for (int i = 0; i < 20; i++) { System.out.println(i); }

  2. B

    for (int i = 1; i <= 20; i++) { System.out.println(i); }

  3. C

    for (int i = 1; i < 20; i++) { System.out.println(i); }

  4. D

    for (int i = 0; i <= 20; i++) { System.out.println(i); }