AP Computer Science A Unit 3

0.0(0)
studied byStudied by 6 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/86

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

87 Terms

1
New cards

Repetition

A form of iteration

2
New cards

Control

Iteration statements change the flow of this by repeating a segment of code zero or more times

3
New cards

True

What the boolean expression controlling the loop must evaluate to in order to repeat the segment of code

4
New cards

One

5
New cards
What is iteration?
Repetition of code controlled by a Boolean expression.
6
New cards
When does a while loop check its condition?
Before executing the loop body.
7
New cards
What causes infinite loops?
Condition never becomes false.
8
New cards
What is an off-by-one error?
Loop runs one time too many or too few.
9
New cards
3 parts of a for-loop header?
Initialization, condition, update.
10
New cards
How many times is initialization executed in a for-loop?
Once before first check.
11
New cards
When is the update executed?
After each loop body execution.
12
New cards
Can loop control variable be used outside loop?
Only if declared before loop header.
13
New cards
Convert for-loop to while-loop?
Move init before, condition in while, update inside.
14
New cards
What is a nested loop?
A loop inside another; inner runs fully each outer iteration.
15
New cards
How to compute total iterations of nested loops?
Outer count × inner count.
16
New cards
Math.random() returns?
Double in [0.0,1.0).
17
New cards
How to generate int in [a,b]?
(int)(Math.random()*(b-a+1))+a
18
New cards
Import for File class?
import java.io.File;
19
New cards
What must be added to main when using File?
throws IOException
20
New cards
What happens if file name invalid?
FileNotFoundException.
21
New cards
Construct File object?
File f = new File("data.txt");
22
New cards
Construct Scanner for file?
Scanner input = new Scanner(f);
23
New cards
What does hasNext() do?
Checks if more tokens exist.
24
New cards
Difference: hasNextInt() vs nextInt()
hasNextInt checks, nextInt consumes.
25
New cards
Why loop for input validation?
To repeatedly prompt until correct type.
26
New cards
What does validation loop on p14 do?
Repeats until integer is entered.
27
New cards
What to print on invalid input?
Error message; consume token with next().
28
New cards
What does while(true) mean?
Creates an intentional infinite loop until a break occurs.
29
New cards
When should you use a while loop?
When number of iterations is unknown beforehand.
30
New cards
When should you use a for loop?
When number of iterations is known or count-controlled.
31
New cards
What does break do?
Immediately exits the nearest loop.
32
New cards
What does continue do?
Skips rest of loop body and proceeds to next iteration.
33
New cards
Common cause of logic errors in loops?
Incorrect condition, update, or variable scope.
34
New cards
What is loop accumulation?
Repeatedly updating a total inside a loop.
35
New cards
Why initialize sum to 0?
So accumulation starts with a neutral element.
36
New cards
Why initialize product to 1?
1 is the multiplicative identity.
37
New cards
What is integer division used for?
Truncating decimal results when dividing ints.
38
New cards
What is casting?
Forcing a value to another type, e.g., (double) num.
39
New cards
Why cast when computing averages?
To avoid integer division and keep decimal precision.
40
New cards
Scanner next() reads what?
A single token up to whitespace.
41
New cards
Scanner nextLine() reads what?
The entire line including spaces.
42
New cards
Common bug mixing nextInt() and nextLine()?
Leftover newline requires consuming nextLine first.
43
New cards
What does FileNotFoundException indicate?
File path or name is incorrect or missing.
44
New cards
Difference between relative and absolute path?
Relative is from project folder; absolute is full system path.
45
New cards
What is a token?
Smallest meaningful unit read by Scanner (word, number).
46
New cards
What happens if nextInt reads a non-int?
InputMismatchException.
47
New cards
What does hasNextDouble check?
Whether next token is a valid double.
48
New cards
What is loop tracing?
Simulating loops step-by-step to predict output.
49
New cards
Purpose of nested loop tracing tables?
Helps determine variable values each iteration.
50
New cards
What is time complexity of nested loops?
Usually O(n^2) when loops scale together.
51
New cards
What is the loop invariant?
A condition that remains true throughout loop execution.
52
New cards
What is the purpose of a loop counter?
Tracks iterations and controls loop progress.
53
New cards
What happens if you modify loop variable inside the loop?
May cause unintended behavior or infinite loops.
54
New cards
What is accumulation?
Adding values repeatedly inside a loop (e.g., sum += x).
55
New cards
What is concatenation in loops?
Building strings by repeatedly adding substrings.
56
New cards
Difference between ++i and i++?
++i increments then returns; i++ returns then increments.
57
New cards
What is the scope of a variable declared in a loop header?
It exists only inside the loop.
58
New cards
What is the purpose of using {} around loops?
Groups statements; without them only first line is inside loop.
59
New cards
What does return do inside a loop?
Immediately exits the method.
60
New cards
What is sentinel-controlled input?
Loop continues until a special sentinel value is entered.
61
New cards
What is flag-controlled input?
Loop continues based on a boolean variable toggled when a condition occurs.
62
New cards
Why should floating-point comparisons avoid == ?
Precision errors make exact comparison unreliable.
63
New cards
What does System.out.print vs println do?
print stays on same line; println moves to next line.
64
New cards
Why avoid hard-coded values?
Reduces flexibility and makes code harder to maintain.
65
New cards
What happens if you open a Scanner on a file twice?
May cause errors or read pointer resets depending on implementation.
66
New cards
Why close a Scanner reading a file?
Releases file resources.
67
New cards
Does closing Scanner(System.in) close System.in?
Yes; can cause errors on further input.
68
New cards
Why must hasNext be checked before next?
Calling next without a next token throws an exception.
69
New cards
What does nextLine return if last input was nextInt?
An empty line due to leftover newline.
70
New cards
What is string parsing?
Using methods to read specific characters/substrings.
71
New cards
What does substring(begin,end) do?
Returns substring from begin up to end-1.
72
New cards
What is charAt used for?
Accessing a single character at a specific index.
73
New cards
What type does compareTo return?
An int:
74
New cards
What is escape sequence \n?
Represents a newline.
75
New cards
What is escape sequence \t?
Represents a tab.
76
New cards
Why use equals instead of == for Strings?
#ERROR!
77
New cards
What is the difference between compile-time and runtime errors?
Compile-time found before running; runtime occurs during execution.
78
New cards
What is an algorithm?
Finite set of steps to solve a problem.
79
New cards
What is pseudocode?
High-level description of an algorithm.
80
New cards
What does trace table help with?
Tracking variable values across iterations.
81
New cards
What is input sanitization?
Ensuring input meets expected format before processing.
82
New cards
What is defensive programming?
Code written to handle unexpected inputs and errors.
83
New cards
Why use constants (`final`)?
Improves clarity and prevents accidental changes.
84
New cards
What is variable shadowing?
Local variable hides a variable declared outside.
85
New cards
What does % operator compute?
Remainder of integer division.
86
New cards
How do you check if a number is even?
num % 2 == 0
87
New cards
How do you check if a number is odd?
num % 2 != 0