Back to marketplace

Module 04: Iteration

No description yet. This curated deck is ready for daily study.

Total cards: 24•Content version: v4•Public deck•Updated: 8/1/2026

Card preview (24)

#1
Front (Question)

What would the method call myMethod(“Karel The Dog”, ‘e’) output?

Card front image
Back (Answer)

3

#2
Front (Question)

What kind of error would the method call myMethod(“Frog”) cause? public void myMethod(String x) { for(int i = 0; i <= x.length(); i++) { System.out.println(x.substring(i, i+1)); } } • Compile Time Error: Missing return value • Runtime Error: String index out of range • Compile Time Error: Unexpected return value • Compile Time Error: Syntax Error in method definition

Back (Answer)

• Runtime Error: String index out of range

#3
Front (Question)

What will the call to the method funWithNumbers(314159) print to the screen?

Card front image
Back (Answer)

314159

#4
Front (Question)

What does the call to the method someMethod(3,1) output?

Card front image
Back (Answer)

10

#5
Front (Question)

What will the call to method patternGrid(3,4,’#’) print?

Card front image
Back (Answer)

#### #### ####

#6
Front (Question)

What would the call to method divideByTen(340) output? public int divideByTen(int num) { while(num / 10 >= 10) { num /= 10; } return num; }

Back (Answer)

34

#7
Front (Question)

Consider the following method: What is returned as a result of the call mystery(“Lunchbox”, 4)?

Card front image
Back (Answer)

xoxbox

#8
Front (Question)

Consider the following code segment: int x = 10; int y = 2; int count = 1; while(x > y) { x /= y; count++; } What will the value of count be after executing the code segment?

Back (Answer)

3

#9
Front (Question)

What will the final result be when run in the console?

Card front image
Back (Answer)

* * * * * * * * * *

#10
Front (Question)

Consider the following code segment: String word = “Cafeteria”; for(/* missing condition */) { System.out.print(word.substring(i+1,i+2) + “ “); } The code segment is intended to print every other letter in the word, starting with index 0, to produce the result C f t r a. Which of the following can be used to replace /* missing condition */ so that the code segment works as intended? • int i = 0; i < word.length(); i+=2 • int i = -1; i < word.length(); i+=2 • int i = 0; i < word.length(); i++ • int i = -1; i <= word.length(); i+=2 • int i = word.length() - 1; i < -1 ; i-=2

Back (Answer)

• int i = -1; i < word.length(); i+=2

#11
Front (Question)

Consider the following code segment: Which of the following for loops would return the same value if System.out.println(sum) were printed? III only II, III and IV II only II and III III and I

Card front image
Back (Answer)

II and III

#12
Front (Question)

Consider the following method: What would the value of sum be after the method call countPs(“Peter Piper picked a pack of pickled peppers”)?

Back (Answer)

9

#13
Front (Question)

How many times will the word “Heyo!” be printed in the following code segment?

Card front image
Back (Answer)

15

#14
Front (Question)

Consider the following code segment: int num = 8; for(int i = num; i > 0; i -= 3) //Line 2 { System.out.print(“ “ + i + “ “); } Which of the following best explains how changing i > 0 to i >= 0 will change the result? • An additional value will be printed, as the for loop will iterate one additional time. • There will be no change in the program because the for loop will iterate the same number of times. • One fewer value will be printed, as the for loop will iterate one fewer time. • This program will result in an infinite loop, as the condition will never be false.

Back (Answer)

• There will be no change in the program because the for loop will iterate the same number of times

#15
Front (Question)

Consider the following method: What value word would be printed as a result of the call mix(“Hippopotamus”, “p”)?

Back (Answer)

Hi0o0tamus

#16
Front (Question)

Consider the following incomplete code segment, which is intended to increase the value of each digit in a String by one. For example, if num is 12345, then the resulting String would be 23456. Which of the following should replace /* Missing Loop Header */ so that the code segment works as intended? • counter < num.length() - 1 • counter <= num.length() • counter < num.length() • counter > num.length() • counter < num.indexOf(counter)

Card front image
Back (Answer)

• counter < num.length()

#17
Front (Question)

Consider the following code segment: Which of the following best explains the result of changing j < 4 to j > 4 on line 1? • The numbers will be printed in reverse order compared to the original because the outer loop will occur in reverse order. • The program will produce the same result, as the number of iterations in the outer loop hasn’t changed. • An infinite loop will occur because the termination of the loop will never be reached. • No output will be produced, as the boolean condition will never be met in the outer for loop.

Card front image
Back (Answer)

• No output will be produced, as the boolean condition will never be met in the outer for loop

#18
Front (Question)

What, if anything, is printed as a result of executing this statement? • 150 • 310 • 10204080 • 10204080160 • There will not be any result printed, as ints cannot be converted to type String.

Card front image
Back (Answer)

• 10204080

#19
Front (Question)

What are while loops?

Back (Answer)

while (boolean expression) { //code executes until false }

#20
Front (Question)

What are infinite loops?

Back (Answer)

Occurs when the expression in a while loop never evaluates to false. The program continues to run infinitely

#21
Front (Question)

What is the purpose of “break?”

Back (Answer)

Breaks out of a while loop and executes statement that immediately follows while loop

#22
Front (Question)

What is the purpose of return?

Back (Answer)

Keyword used in methods to return a value back to the initial program that called the method

#23
Front (Question)

How can you avoid infinity loops?

Back (Answer)

• Add a condition to the while loop • Add a “break” statement • Add a return keyword

#24
Front (Question)

What are for loops?

Back (Answer)

• Another tool we can use to repeat code sequences • for loops allows repeating a set of statement a specfic number of times! for(variable initialization; boolean expression; increment) { //will execute if boolean is true, and until the boolean expression is false } • variable initialization: The initialization; this variable controls the for loop execution • boolean expression: if the expression is false, the for loop will not execute • increment: changes the value of the loop control variables