Algorithms with Selection and Repetition - AP Computer Science A
- What is the syntax for a switch statement in Java?
- switch(expression) { case value: statements; break; }. Multi-way selection based on expression value.
- What differentiates a for loop from a while loop?
- Initialization, condition, and update in one line. For loop combines all control elements in header.
- Which loop structure is best for iterating a known number of times?
- for loop. Compact syntax when iteration count is predetermined.
- What is the syntax for a basic if statement in Java?
- if (condition) { statements }. Standard conditional statement structure in Java.
- Determine the output: int i = 0; do { System.out.print(i); } while (i > 0);
- 0. Executes once since condition checked after execution.
- How many times will this loop execute? for (int i = 0; i < 5; i++)
- 5 times. Loop runs while i < 5: values 0, 1, 2, 3, 4.
- What is the significance of the loop condition?
- Determines when the loop stops. Controls whether loop continues or terminates.
- What is the purpose of a loop control variable?
- To control the number of iterations of the loop. Manages how many times the loop body executes.
- Find the error: for (int i = 0; i < 10; i--)
- i-- should be i++. Decrement creates infinite loop since i never reaches 10.
- Identify the operator used to compare equality in Java.
- ==. Double equals compares values, single equals assigns.
- What is an infinite loop?
- A loop that never terminates. Occurs when loop condition never becomes false.
- Which loop condition form ensures a loop runs zero or more times?
- while loop. Condition checked before execution, may not run at all.
- What is the purpose of using else after if?
- To execute code when the if condition is false. Provides alternative path when condition is false.
- Identify the keyword that ends a switch case.
- break. Prevents fall-through to subsequent cases.
- Identify the error: for (int i = 0; i < 5; i++) { System.out.print(i); }
- No error. Syntax is correct for standard for loop.
- What does the continue statement do in a loop?
- Skips the current iteration and continues with the next. Jumps to next iteration without executing remaining code.
- What is an iteration in the context of loops?
- A single execution of the loop body. One complete pass through the loop body.
- Which loop always executes its block at least once?
- do-while loop. Condition is checked after execution, guaranteeing one run.
- What is the default case in a switch statement?
- Executed if no case matches. Catches all unmatched values in switch statement.
- What keyword is used to define multiple selections in a switch statement?
- case. Each case represents a possible match value.
- What is the result of the expression: 4 >= 4?
- true. Greater than or equal to includes equality.
- What is the purpose of the else if statement?
- To test multiple conditions in sequence. Chains conditions for multiple decision paths.
- How is a for loop typically structured in Java?
- for (initialization; condition; update) { statements }. Three parts control initialization, termination, and increment.
- Identify the error: if (x = 5) { ... }
- Use == instead of =. Single equals is assignment, not comparison.
- What is the result of 5 % 2?
- 1. Modulo operator returns remainder of division.
- What is the result of: boolean b = (2 < 3) && (3 > 2);
- true. Both conditions are true, AND returns true.
- Determine the output: int x = 5; if (x < 5) { x--; } System.out.print(x);
- 5. Condition is false, so x remains unchanged.
- Which statement is used to skip an iteration in a loop?
- continue. Bypasses remaining code in current iteration.
- Identify the error: switch (x) { case 1: System.out.print(1); }
- Missing break statement. Without break, execution continues to next case.
- Determine the output: int x = 0; while (x < 2) { x++; } System.out.print(x);
- 2. Loop increments x twice (0→1, 1→2), then x printed.
- Identify the error: while (true); { System.out.print("Loop"); }
- Semicolon after true creates an infinite loop. Empty statement after while creates infinite loop.
- What does an if-else statement do?
- Executes code blocks based on condition being true or false. Provides branching logic for conditional execution.
- What happens if break is omitted in a switch case?
- Fall-through to next case occurs. Code continues executing into subsequent cases.
- Identify the keyword used for creating a loop in Java.
- for, while, or do-while. These are the three main loop keywords in Java.
- What is the purpose of a selection statement in programming?
- To execute code based on a condition. Allows programs to make decisions and branch execution paths.
- What is the syntax for a do-while loop in Java?
- do { statements } while (condition);. Body executes first, then condition is checked.
- What keyword is used to exit a loop prematurely?
- break. Immediately terminates loop execution.
- What are nested loops?
- Loops within loops. One loop contained inside another loop's body.
- What is the output of: if (false) { System.out.print("X"); }
- No output. False condition means if block doesn't execute.
- What is the output of: if (true) { System.out.print("A"); } else { System.out.print("B"); }
- A. True condition executes the if block.