Implementing Selection and Iteration Algorithms - AP Computer Science A

  1. What is an iteration in the context of loops?
    A single execution of the loop body. One complete pass through the loop body statements.
  2. Identify the error: if(x = 5) {...}
    Assignment operator used instead of equality. Single == assigns value; double ==== compares for equality.
  3. What is the syntax for a do-while loop in Java?
    do { // statements } while (condition);. Executes body first, then checks condition; semicolon required after while.
  4. What is the primary use of a switch statement?
    To execute code based on a variable's value. Provides multi-way branching based on variable values instead of if-else chains.
  5. What is the result of the expression: !true?
    false. NOT operation: inverts the boolean value from true to false.
  6. What does the expression (a == b) evaluate to?
    true if a is equal to b, else false. Equality operator returns boolean value based on comparison result.
  7. What is the default case in a switch statement for?
    Executes if no case matches. Optional catch-all case when no other cases match the value.
  8. What does the logical operator && mean?
    Logical AND. Returns true only when both operands are true.
  9. What is the syntax for an if statement in Java?
    if (condition) { // statements }. Standard conditional statement structure with condition in parentheses and code in braces.
  10. What is the output of: do {i++;} while(i<0); for i=0?
    i=1. Body executes once before condition check, incrementing ii from 0 to 1.
  11. Identify the loop type: for(int i = 0; i < n; i++) {...}
    for loop. Uses initialization, condition, and increment/decrement in parentheses.
  12. What does the break statement do in a loop?
    Terminates the loop immediately. Exits the loop completely and continues execution after the loop.
  13. What keyword is used to exit a method in Java?
    return. Immediately exits the current method and returns control to caller.
  14. How many times does a nested loop run: for(i=0;i<3;i++) for(j=0;j<2;j++)?
    6 times. Outer loop runs 3 times, inner loop runs 2 times each: 3×2=63 \times 2 = 6.
  15. What is the output of: int x=0; while(x<3) {x++;} System.out.print(x);
    3. Loop increments xx three times, final value is 3.
  16. Identify the error: switch(x) { case 1: y=1; case 2: y=2; }
    Missing break statements. Without breaks, execution falls through to subsequent cases.
  17. What is the result of the expression: true && false?
    false. AND operation: true only when both operands are true.
  18. What does a case label in a switch statement require?
    A constant expression. Must be a compile-time constant like literal or final variable.
  19. What is the result of the expression: true || false?
    true. OR operation: true when at least one operand is true.
  20. Identify the error: for(int i=0; i<5; i--) {...}
    Loop condition will never be false. Decrementing ii makes it more negative, never reaching the termination condition.
  21. What is the output of: boolean b = (3 > 2); System.out.print(b);
    true. Boolean variable stores the result of the comparison expression.
  22. What is the precedence of logical operators: && and ||?
    && has higher precedence than ||. &&\&\& is evaluated before ∣∣|| in logical expressions.
  23. What do you call a loop inside another loop?
    Nested loop. One loop placed inside the body of another loop structure.
  24. What is the output of: for(int i=0;i<3;i++){System.out.print(i);}
    012. Loop runs 3 times with ii values 0, 1, 2 printing consecutively.
  25. What does the logical operator || mean?
    Logical OR. Returns true when at least one operand is true.
  26. What is the output of: for(int i=0; i<2; i++) {for(int j=0; j<2; j++) {System.out.print("*");}}
    ****. Nested loops: outer runs 2 times, inner runs 2 times each.
  27. What is the output of: int x=0; if(x){x++;} System.out.print(x);
    Compilation error. Integer cannot be used as boolean condition in Java if statements.
  28. What is a condition in the context of control structures?
    An expression that evaluates to true or false. Boolean expression that determines which code path to execute.
  29. What is the output of: boolean b = (2 == 2); System.out.print(b);
    true. Equality comparison evaluates to true since both sides are equal.
  30. Which loop runs at least once: while or do-while?
    do-while loop. Checks condition after executing the body, guaranteeing at least one execution.
  31. What keyword can skip the current iteration of a loop?
    continue. Skips remaining statements in current iteration and moves to next iteration.
  32. What is the syntax for a while loop in Java?
    while (condition) { // statements }. Executes statements while the condition remains true.
  33. What is the output of: for(int i=5; i>0; i--) {System.out.print(i);}
    54321. Countdown loop printing decreasing values from 5 to 1.
  34. What is the purpose of a loop counter?
    To track the number of iterations. Variable that keeps track of how many iterations have occurred.
  35. What is the output of: if(1 < 2) {System.out.print("A");} else {System.out.print("B");}
    A. Condition 1<21 < 2 is true, so executes the if branch.
  36. What is infinite loop?
    A loop that never ends. Condition never becomes false, causing continuous execution without termination.
  37. What is the purpose of an else statement in Java?
    Executes code if the if condition is false. Alternative path when the if condition evaluates to false.
  38. Identify the error: while(x == 10); { x++; }
    Semicolon after while condition is incorrect. Semicolon creates empty statement, making the loop body separate from while.
  39. What is the primary use of a for loop?
    To iterate a block of code a known number of times. Best for counting loops where the number of iterations is predetermined.

Study these flashcards