Which statement best explains why a while loop can execute its body zero times?
05. Iteration and Loops Quiz Online Quiz Questions
Use this free practice quiz with 20 questions to review 05. Iteration and Loops, test your knowledge, and prepare for your next test or exam.
True or false: In Python-like syntax, range(1, 5) includes the value 5.
- A
True
- B
False
What is the name for a special input value that signals the end of a sentinel-controlled loop?
In the three-part loop design of initialization, condition, and , which part changes the state so the loop can eventually finish?
A program examines [4, 7, 2, 9, 6] and increases a variable only when the current value is even. What is the variable functioning as?
- A
It stores the sum of every number in the list.
- B
It stores the largest number seen so far.
- C
It increases when a specified event is observed.
- D
It ensures that the loop condition is always true.
True or false: A break statement inside the inner loop of a nested-loop structure automatically terminates both the inner and outer loops.
- A
True
- B
False
How many times does the innermost body execute?\n\nfor row in range(2):\n for column in range(3):\n process()
In a loop, the statement skips the remainder of the current iteration and moves to the next one.
A program must apply the same operation to every item in a list whose contents are already available. Which design is most appropriate?
- A
Use a for loop to visit every item in a known list.
- B
Use a while loop and manually track an index even though the list is the direct source of repetition.
- C
Use a sentinel-controlled while loop for every list traversal.
- D
Use an infinite loop because the list length is known.
Design and explain a loop that counts the positive values in [-2, 0, 4, 7, -1]. State the initialization, the condition that causes the counter to increase, the final result, and an invariant that remains true after each iteration.
A program needs to create a list of even values from an existing list. Which approach is safest?
- A
Remove each matching item from the list during the same traversal.
- B
Build a new collection while traversing the original list.
- C
Insert new items at the current position during every iteration.
- D
Change the list length whenever the loop condition is checked.
What values does this loop print?\n\nfor n in range(2, 6):\n print(n)
- A
2, 3, 4, 5, 6
- B
2, 3, 4, 5
- C
1, 2, 3, 4, 5
- D
3, 4, 5, 6
What is the result of this code?\n\ni = 0\nwhile i < 0:\n print(i)\n i = i + 1
- A
It prints 0 once.
- B
It prints 0 repeatedly.
- C
It prints nothing.
- D
It raises an error because the condition is false.
A sentinel-controlled loop reads the values 4, 7, and then -1, where -1 is the sentinel. What total should the loop produce if it adds each ordinary value and excludes the sentinel?
- A
11
- B
10
- C
12
- D
-1
Which initialization is appropriate for total before a loop computes the sum of all values in [3, 5, 2]?
- A
total = 0
- B
total = 1
- C
total = 3
- D
total = 10
True or false: In a nested loop, a break statement inside the inner loop automatically terminates both the inner and outer loops.
- A
True
- B
False
What integer does this code print?\n\nvalues = [-2, 0, 4, 7, -1]\npositive_count = 0\n\nfor value in values:\n if value > 0:\n positive_count += 1\n\nprint(positive_count)
How many numbers does this loop print?\n\nfor number in range(1, 6):\n if number == 3:\n continue\n print(number)
A command-processing loop should print every command except "quit", then stop when "quit" is entered. Which two redesigns preserve that behavior while providing a reliable path to termination? Select all correct choices.
- A
Use a condition such as
while command != "quit"and read the next command at the end of each non-quit iteration. - B
Keep an explicit
breakfor the quit command and read the next command after processing each non-quit command. - C
Add a
breakimmediately after processing the first non-quit command. - D
Set
commandto"quit"before entering the loop so that the loop has a guaranteed exit. - E
Remove every update to
commandbecause the loop condition is checked automatically.
You are testing a loop that finds the largest value in a list. Which test cases are especially useful for exposing boundary or initialization problems? Select all correct choices.
- A
An empty list, to reveal whether the algorithm handles zero items safely.
- B
A one-item list, to test the smallest nonempty input.
- C
A list whose largest value is the first item.
- D
A list whose largest value is the last item.
- E
Only one typical multi-item list with the largest value somewhere in the middle.
Related study packs and tools
AP Computer Science A: Integrated Java Problem Solving
A practical review of the integrated Java skills, debugging habits, algorithm reasoning, and free-response strategies needed for AP Computer Science A.
Fault Tolerance and Redundant Systems
A progressive guide to designing, operating, and testing systems that continue useful service despite component faults, overload, data loss, and recovery challenges.
How the Internet Works
A medium-difficulty quiz on how independently operated networks, protocols, addresses, naming systems, transport services, and routing mechanisms work together to form the Internet.
Java ArrayList and Collections: Operations, Traversals, and Algorithms
Learn how to create, modify, traverse, and analyze Java lists with ArrayList, wrapper types, standard algorithms, and the Collections utility class for AP Computer Science A.
Java Boolean Logic and Selection
Build a clear understanding of Java Boolean expressions, logical operators, selection statements, nested branching, equivalent logic, and systematic code tracing for AP Computer Science A.
Java Foundations and Program Design
A practical introduction to Java program structure, data types, operators, input and output, problem-solving strategy, testing, and debugging for AP Computer Science A.
Java Inheritance and Polymorphism
A practical guide to Java inheritance, constructor chaining, method overriding, polymorphism, access control, and object behavior for tracing and writing AP Computer Science A programs.
Java Objects, Classes, and the String API
A focused guide to Java classes, object references, constructors, methods, strings, wrapper classes, and API usage for AP Computer Science A.
Java Primitive Types and Expression Evaluation
A focused guide to Java primitive types, expression evaluation, numeric conversion, floating-point behavior, integer overflow, and common methods in the Math class for AP Computer Science A.
Java Primitive Types and Expressions
A focused review of Java primitive types, expression evaluation, numeric conversion, arithmetic behavior, Math methods, randomness, and tracing techniques for AP Computer Science A.
Packets, Links, and Network Performance
A progressive guide to how packet-switched networks encapsulate data, move it across links, and manage delay, throughput, queues, and congestion.
Parallel and Distributed Computing
A structured guide to designing parallel and distributed systems through decomposition, communication, coordination, state management, scalability, and fault tolerance.