Implementing Selection and Iteration Algorithms - AP Computer Science A
- What is an iteration in the context of loops?
- A single execution of the loop body. One complete pass through the loop body statements.
- Identify the error: if(x = 5) {...}
- Assignment operator used instead of equality. Single assigns value; double compares for equality.
- 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.
- 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.
- What is the result of the expression: !true?
- false. NOT operation: inverts the boolean value from true to false.
- 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.
- 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.
- What does the logical operator && mean?
- Logical AND. Returns true only when both operands are true.
- 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.
- What is the output of: do {i++;} while(i<0); for i=0?
- i=1. Body executes once before condition check, incrementing from 0 to 1.
- Identify the loop type: for(int i = 0; i < n; i++) {...}
- for loop. Uses initialization, condition, and increment/decrement in parentheses.
- What does the break statement do in a loop?
- Terminates the loop immediately. Exits the loop completely and continues execution after the loop.
- What keyword is used to exit a method in Java?
- return. Immediately exits the current method and returns control to caller.
- 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: .
- What is the output of: int x=0; while(x<3) {x++;} System.out.print(x);
- 3. Loop increments three times, final value is 3.
- Identify the error: switch(x) { case 1: y=1; case 2: y=2; }
- Missing break statements. Without breaks, execution falls through to subsequent cases.
- What is the result of the expression: true && false?
- false. AND operation: true only when both operands are true.
- What does a case label in a switch statement require?
- A constant expression. Must be a compile-time constant like literal or final variable.
- What is the result of the expression: true || false?
- true. OR operation: true when at least one operand is true.
- Identify the error: for(int i=0; i<5; i--) {...}
- Loop condition will never be false. Decrementing makes it more negative, never reaching the termination condition.
- What is the output of: boolean b = (3 > 2); System.out.print(b);
- true. Boolean variable stores the result of the comparison expression.
- What is the precedence of logical operators: && and ||?
- && has higher precedence than ||. is evaluated before in logical expressions.
- What do you call a loop inside another loop?
- Nested loop. One loop placed inside the body of another loop structure.
- What is the output of: for(int i=0;i<3;i++){System.out.print(i);}
- 012. Loop runs 3 times with values 0, 1, 2 printing consecutively.
- What does the logical operator || mean?
- Logical OR. Returns true when at least one operand is true.
- 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.
- 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.
- 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.
- What is the output of: boolean b = (2 == 2); System.out.print(b);
- true. Equality comparison evaluates to true since both sides are equal.
- Which loop runs at least once: while or do-while?
- do-while loop. Checks condition after executing the body, guaranteeing at least one execution.
- What keyword can skip the current iteration of a loop?
- continue. Skips remaining statements in current iteration and moves to next iteration.
- What is the syntax for a while loop in Java?
- while (condition) { // statements }. Executes statements while the condition remains true.
- 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.
- What is the purpose of a loop counter?
- To track the number of iterations. Variable that keeps track of how many iterations have occurred.
- What is the output of: if(1 < 2) {System.out.print("A");} else {System.out.print("B");}
- A. Condition is true, so executes the if branch.
- What is infinite loop?
- A loop that never ends. Condition never becomes false, causing continuous execution without termination.
- 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.
- Identify the error: while(x == 10); { x++; }
- Semicolon after while condition is incorrect. Semicolon creates empty statement, making the loop body separate from while.
- 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.