Algorithms with Selection and Repetition - AP Computer Science A

  1. What is the syntax for a switch statement in Java?
    switch(expression) { case value: statements; break; }. Multi-way selection based on expression value.
  2. What differentiates a for loop from a while loop?
    Initialization, condition, and update in one line. For loop combines all control elements in header.
  3. Which loop structure is best for iterating a known number of times?
    for loop. Compact syntax when iteration count is predetermined.
  4. What is the syntax for a basic if statement in Java?
    if (condition) { statements }. Standard conditional statement structure in Java.
  5. Determine the output: int i = 0; do { System.out.print(i); } while (i > 0);
    0. Executes once since condition checked after execution.
  6. 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.
  7. What is the significance of the loop condition?
    Determines when the loop stops. Controls whether loop continues or terminates.
  8. 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.
  9. Find the error: for (int i = 0; i < 10; i--)
    i-- should be i++. Decrement creates infinite loop since i never reaches 10.
  10. Identify the operator used to compare equality in Java.
    ==. Double equals compares values, single equals assigns.
  11. What is an infinite loop?
    A loop that never terminates. Occurs when loop condition never becomes false.
  12. Which loop condition form ensures a loop runs zero or more times?
    while loop. Condition checked before execution, may not run at all.
  13. 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.
  14. Identify the keyword that ends a switch case.
    break. Prevents fall-through to subsequent cases.
  15. Identify the error: for (int i = 0; i < 5; i++) { System.out.print(i); }
    No error. Syntax is correct for standard for loop.
  16. 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.
  17. What is an iteration in the context of loops?
    A single execution of the loop body. One complete pass through the loop body.
  18. Which loop always executes its block at least once?
    do-while loop. Condition is checked after execution, guaranteeing one run.
  19. What is the default case in a switch statement?
    Executed if no case matches. Catches all unmatched values in switch statement.
  20. What keyword is used to define multiple selections in a switch statement?
    case. Each case represents a possible match value.
  21. What is the result of the expression: 4 >= 4?
    true. Greater than or equal to includes equality.
  22. What is the purpose of the else if statement?
    To test multiple conditions in sequence. Chains conditions for multiple decision paths.
  23. How is a for loop typically structured in Java?
    for (initialization; condition; update) { statements }. Three parts control initialization, termination, and increment.
  24. Identify the error: if (x = 5) { ... }
    Use == instead of =. Single equals is assignment, not comparison.
  25. What is the result of 5 % 2?
    1. Modulo operator returns remainder of division.
  26. What is the result of: boolean b = (2 < 3) && (3 > 2);
    true. Both conditions are true, AND returns true.
  27. Determine the output: int x = 5; if (x < 5) { x--; } System.out.print(x);
    5. Condition is false, so x remains unchanged.
  28. Which statement is used to skip an iteration in a loop?
    continue. Bypasses remaining code in current iteration.
  29. Identify the error: switch (x) { case 1: System.out.print(1); }
    Missing break statement. Without break, execution continues to next case.
  30. 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.
  31. Identify the error: while (true); { System.out.print("Loop"); }
    Semicolon after true creates an infinite loop. Empty statement after while creates infinite loop.
  32. What does an if-else statement do?
    Executes code blocks based on condition being true or false. Provides branching logic for conditional execution.
  33. What happens if break is omitted in a switch case?
    Fall-through to next case occurs. Code continues executing into subsequent cases.
  34. Identify the keyword used for creating a loop in Java.
    for, while, or do-while. These are the three main loop keywords in Java.
  35. 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.
  36. What is the syntax for a do-while loop in Java?
    do { statements } while (condition);. Body executes first, then condition is checked.
  37. What keyword is used to exit a loop prematurely?
    break. Immediately terminates loop execution.
  38. What are nested loops?
    Loops within loops. One loop contained inside another loop's body.
  39. What is the output of: if (false) { System.out.print("X"); }
    No output. False condition means if block doesn't execute.
  40. What is the output of: if (true) { System.out.print("A"); } else { System.out.print("B"); }
    A. True condition executes the if block.

Study these flashcards