Module 4: Loops and Nested Loops Flashcards

(116 cards)

1
Q

What is a loop?

A

A construct that controls repeated executions of a block of statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three types of loop statements in Java?

A

while loops, do-while loops, and for loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax of a while loop?

A

while (loop-continuation-condition) { Statement(s); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the loop body?

A

The part of the loop that contains the statements to be repeated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an iteration?

A

A one-time execution of a loop body (also called repetition)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a loop-continuation-condition?

A

A Boolean expression that controls the execution of the loop body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When does a while loop terminate?

A

When the loop-continuation-condition evaluates to false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a counter-controlled loop?

A

A loop where the control variable is used to count the number of iterations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you don’t increment the counter in a while loop?

A

The loop becomes infinite because the condition never becomes false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an infinite loop?

A

A loop that runs forever because the loop-continuation-condition never becomes false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you stop an infinite loop from command line?

A

Press CTRL+C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an off-by-one error?

A

A common mistake where the loop executes one more or less time than intended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What would happen with count <= 100 instead of count < 100?

A

The loop would execute 101 times instead of 100 times (off-by-one error)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In the RepeatAdditionQuiz example, what does Math.random() * 10 do?

A

Generates a random number from 0.0 to 9.999…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does (int)(Math.random() * 10) do?

A

Generates a random integer from 0 to 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 3 steps for loop design strategy?

A

1) Identify statements to repeat, 2) Wrap in while(true) loop, 3) Add loop-continuation-condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is user confirmation in loops?

A

Using input from user to decide whether to continue the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a sentinel value?

A

A special input value that signifies the end of input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a sentinel-controlled loop?

A

A loop that uses a sentinel value to control its execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Why shouldn’t you use floating-point values for loop control?

A

Floating-point arithmetic is approximated and may never reach exact values, causing infinite loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does input redirection do?

A

Takes input from a file rather than keyboard using < filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does output redirection do?

A

Sends output to a file rather than console using > filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does input.hasNext() do?

A

Returns true if there is more input available, false otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you end input from keyboard?

A

Press ENTER, then CTRL+Z, then ENTER again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the syntax of a do-while loop?
do { Statement(s); } while (loop-continuation-condition);
26
What is the key difference between while and do-while loops?
do-while executes the loop body at least once, while may not execute at all
27
When should you use a do-while loop?
When you need statements inside the loop to execute at least once
28
What is the syntax of a for loop?
for (initial-action; loop-continuation-condition; action-after-each-iteration) { Statement(s); }
29
What does initial-action do in a for loop?
Often initializes a control variable (executed once)
30
What does action-after-each-iteration do?
Usually increments or decrements the control variable (executed after each iteration)
31
What is a control variable?
A variable used to control how many times the loop executes and when it terminates
32
Can you declare a control variable inside the for loop?
Yes, using for (int i = 0; i < 100; i++)
33
What happens if you declare a variable in the for loop control structure?
It cannot be referenced outside the loop
34
Can initial-action contain multiple statements?
Yes, separated by commas: for (int i = 0, j = 0; ...)
35
Can action-after-each-iteration contain multiple statements?
Yes, separated by commas: for (...; i++, j++)
36
What happens if you omit the loop-continuation-condition in a for loop?
It becomes implicitly true, creating an infinite loop
37
What does ++i vs i++ do in a for loop?
Both increment i by 1; no difference in this context
38
What are the three parts of a for loop control?
initial-action, loop-continuation-condition, action-after-each-iteration
39
What is a nested loop?
A loop inside another loop
40
What happens each time the outer loop repeats?
The inner loops are reentered and started anew
41
How many times does an action execute in nested loops?
Outer loop iterations × Inner loop iterations
42
If outer loop runs 1000 times and inner loop 1000 times, how many total iterations?
1,000,000 iterations (one million)
43
What should you be aware of with deeply nested loops?
They may take an extremely long time to run
44
What does System.currentTimeMillis() return?
Current time in milliseconds since January 1, 1970 GMT
45
How do you convert milliseconds to seconds?
Divide by 1000
46
What does System.nanoTime() return?
Current time in nanoseconds (more precise than currentTimeMillis)
47
What does break do in a loop?
Immediately terminates the loop
48
What does continue do in a loop?
Ends the current iteration and goes to the end of the loop body
49
What's the difference between break and continue?
break exits the entire loop, continue skips to next iteration
50
Where does program control go after break?
To the statement following the loop
51
Where does program control go after continue in while/do-while?
To evaluate the loop-continuation-condition
52
Where does program control go after continue in for loop?
To execute action-after-each-iteration, then evaluate condition
53
Should break and continue be used extensively?
No, too many break/continue statements make programs hard to read
54
Can you always write loops without break/continue?
Yes, but sometimes break/continue makes code simpler
55
What is the difference between break in loops vs switch?
Same keyword, but break in loops exits the loop, in switch exits the switch
56
Are break and continue the same as goto?
No, goto transfers control anywhere; break/continue only work in loops/switch
57
What happens with missing semicolon in for loop?
Compilation error - semicolons separate the three parts
58
What happens with semicolon after for loop header?
Creates an empty loop body that may cause logic errors
59
What error occurs if loop variable is used outside its scope?
Compilation error - variable not in scope
60
What happens if you don't initialize loop control variable?
May cause compilation error or unpredictable behavior
61
What is the loop continuation condition in this: for (int i = 0; i < 10; i++)?
i < 10
62
What is the initial action in this: for (int i = 0; i < 10; i++)?
int i = 0
63
What is the action after each iteration in this: for (int i = 0; i < 10; i++)?
i++
64
How many times does this loop execute: for (int i = 0; i < 5; i++)?
5 times (i = 0, 1, 2, 3, 4)
65
How many times does this loop execute: for (int i = 1; i <= 5; i++)?
5 times (i = 1, 2, 3, 4, 5)
66
How many times does this loop execute: for (int i = 0; i <= 5; i++)?
6 times (i = 0, 1, 2, 3, 4, 5)
67
What does this loop do: for (int i = 10; i > 0; i--)?
Counts down from 10 to 1
68
What is wrong with: for (int i = 0; i < 10; i++);?
Semicolon creates empty body, following statements not in loop
69
What is wrong with: while (i < 10) sum += i;?
Missing i increment causes infinite loop
70
What is wrong with: do sum += i; while (i < 10);?
Missing i increment causes infinite loop
71
When is do-while preferred over while?
When loop body must execute at least once
72
When is for loop preferred over while?
When you know exactly how many iterations needed
73
When is while loop preferred?
When number of iterations depends on a condition that changes during execution
74
What does this calculate: sum = 1 + 2 + 3 + ... + n?
Sum of first n natural numbers = n(n+1)/2
75
In a multiplication table program, which is the outer loop?
The loop that controls rows (typically i)
76
In a multiplication table program, which is the inner loop?
The loop that controls columns (typically j)
77
What does printf('%4d', value) do?
Prints integer in field width of 4 characters, right-aligned
78
What is the purpose of output formatting in tables?
To align columns properly for readability
79
How do you read multiple integers until 0 is entered?
Use while (data != 0) with data = input.nextInt() inside loop
80
What is the pattern for input validation loops?
Read input, check if valid, if not prompt again and re-read
81
How do you count correct answers in a quiz loop?
Use a counter variable, increment when answer is correct
82
What is the pattern for accumulating values?
Initialize sum to 0, add each value to sum in loop
83
How do you generate random integers from 1 to n?
(int)(Math.random() * n) + 1
84
How do you generate random integers from 0 to n-1?
(int)(Math.random() * n)
85
How do you swap two variables?
Use temporary variable: temp = a; a = b; b = temp;
86
What does number1 < number2 check accomplish in subtraction quiz?
Ensures result is positive (larger - smaller)
87
How do you build an output string in a loop?
Initialize empty string, concatenate new content each iteration
88
What is the ternary operator used for in loops?
Conditional string building: condition ? 'correct' : 'wrong'
89
How do you measure execution time?
Record start time, execute code, record end time, subtract
90
What units does System.currentTimeMillis() use?
Milliseconds
91
How do you convert time from milliseconds to seconds?
Divide by 1000
92
What does startTime and endTime pattern measure?
How long code takes to execute
93
In SubtractionQuizLoop, what controls the number of questions?
final int NUMBER_OF_QUESTIONS = 5
94
Why use final for NUMBER_OF_QUESTIONS?
Makes it a constant that cannot be changed
95
What happens if you input 0 first in sentinel loop?
Loop body never executes, sum remains 0
96
What is the advantage of sentinel-controlled input?
Can handle unknown number of input values
97
How do you process files with input redirection?
java ClassName < inputfile.txt
98
How do you save output to file?
java ClassName > outputfile.txt
99
Can you use both input and output redirection?
Yes: java ClassName < input.txt > output.txt
100
What does hasNext() return when no more input?
false
101
How do you end keyboard input for hasNext()?
ENTER, then CTRL+Z, then ENTER
102
What is the benefit of using constants in loop limits?
Easy to change, makes code more readable and maintainable
103
What error occurs with uninitialized variables in loops?
Compilation error or unpredictable behavior
104
What causes StringIndexOutOfBoundsException in loops?
Accessing string characters beyond valid index range
105
What causes ArrayIndexOutOfBoundsException in loops?
Accessing array elements beyond valid index range
106
Why should loop conditions be carefully designed?
To avoid infinite loops and off-by-one errors
107
What is the purpose of loop invariants?
Conditions that remain true throughout loop execution
108
How do you debug infinite loops?
Add print statements to see variable values, check loop condition logic
109
What is the most common cause of infinite loops?
Forgetting to update the loop control variable
110
How do you avoid off-by-one errors?
Carefully check loop boundaries with test cases
111
What testing should you do for loops?
Test with 0 iterations, 1 iteration, multiple iterations
112
When should you use nested loops?
When you need to process multi-dimensional data or patterns
113
What is the time complexity of nested loops?
O(n²) for two nested loops with n iterations each
114
How do you optimize nested loops?
Minimize work in inner loop, consider breaking early when possible
115
What is loop unrolling?
Optimization technique that reduces loop overhead by duplicating loop body
116
When might you prefer while over for?
When initialization and increment logic is complex or non-standard