Loops AP Test Review Your test will comprise about 50 of the following questions.

True/False Indicate whether the sentence or statement is true or false.

____ 1. The assignment operator ( = ) can be combined with the arithmetic operators ( +, -, *, / ) to provide extended assignment operators, i.e. +=, -=, *=, /=. ____ 2. m++, m=m+1, and m+=1 are all equivalent expressions. ____ 3. Parameters cannot be passed as arguments to "read" methods in the KeyboardReader class, i.e. KeyboardReader.readInt( "Enter an integer: " ); ____ 4. If a single statement follows an if or else statement the braces are not necessary, i.e. if( ... ) statement else statement ____ 5. The assignment operator ( = ) and equal to operator ( == ) can be used interchangeably. ____ 6. A while loop will always execute at least once. ____ 7. Both for loops and while loops can be used as count-controlled loops. ____ 8. The counter in a for loop is updated at the beginning of the loop. ____ 9. It is illegal to declare the loop control variable inside the for loop header. ____ 10. A sentinel is a special value that marks the end of a list. ____ 11. When testing programs that use if or if-else statements, be sure to test all of the logical branches. ____ 12. The for statement combines counter initialization, condition test and counter update into a single expression. ____ 13. It is usually safer to use the == or != operators in loops, rather than the other logical operators. ____ 14. The OR operand evaluates to false if one operand is false. ____ 15. At a minimum, test data should try to achieve complete code coverage, which means every line of code should be executed at least once. ____ 16. Test data at the limits of validity are called boundary conditions. ____ 17. Nested if statements offer an alternative to deal with a programs logical complexity. ____ 18. It is better to write code and then create a flow chart or truth table to verify the code. ____ 19. One of the most common mistakes in nested if statements involves misplaced braces. ____ 20. Changing the indentation of if and else statements changes the logic of the code. ____ 21. Since overusing braces clutters the code, only use braces when needed. ____ 22. Arithmetic overflow results when the range of a given number is exceeded. ____ 23. Java uses complete evaluation, in which all parts of a Boolean expression are always evaluated. ____ 24. Complete code coverage in testing will test all logical paths through a program. ____ 25. The following nested if statements are equivalent:

if ( it is 3:00 ) if ( it is 3:00 ) if ( it is Monday ) if ( it is Monday ){ go home go home go to the gym go to the gym } ____ 26. If braces are removed from a nested if/else statement, Java pairs the else with the closest preceding if.

Multiple Choice Identify the letter of the choice that best completes the statement or answers the question.

____ 27. Using the same name for two different methods is called: a. doubling up c. overloading b. an error d. equivalence ____ 28. Which of the following methods is NOT included in the Math class? a. max c. abs b. min d. readInt ____ 29. Which of the following values could be returned from Random.nextInt(10)? a. -3 c. 11 b. 10 d. 7 ____ 30. What type of expression returns either true or false? a. Boolean c. initialization b. arithmetic d. none of the above ____ 31. Which statement provides a looping mechanism that executes statements repeatedly as long as some condition is true? a. if c. while b. else d. assignment ____ 32. How many times will the following loop execute? int counter = 2; while ( counter < 10 ){ counter += 2; } a. 3 c. 5 b. 4 d. 0 ____ 33. Which statement allows a loop to terminate before the loop condition is false? a. end c. break b. continue d. stop ____ 34. What type of loop error occurs whenever a loop goes around one too many or one too few times? a. Infinite loop c. Initialization error b. Loop body error d. Off-by-one error ____ 35. What type of loop error occurs when a terminating condition is never encountered? a. Infinite loop c. Initialization error b. Loop body error d. Off-by-one error ____ 36. A(n) ______error occurs when a variable is not set to a correct starting value. a. Infinite loop c. Initialization b. Update d. Off-by-one ____ 37. Which of the following is NOT one of the 3 logical operators introduced in this chapter. a. AND c. NOT b. OR d. XOR ____ 38. If the operator AND is used, which of the following will make the whole condition true? a. first operand true, second operand false c. both operands true b. first operand false, second operand true d. both operands false ____ 39. What is the precedence of the logical operator OR ( || )? a. 5 c. 3 b. 6 d. 9 ____ 40. Which of the following is the Java symbol for logical AND? a. && c. == b. ! d. || ____ 41. The sets of test data that exercise a program in the same manner are said to belong to the same: a. code coverage c. equivalence class b. set d. boundary condition ____ 42. It is common for programs to fail around which points? a. boundary condition points c. equivalence class points b. extreme condition points d. no points ____ 43. Reformatting the first if/else statement into the second is called: ( 1 ) ( 2 ) if( ) if( ) statement statement else else if () if( ) statement statement a. extended if statement c. two-way if statement b. one-way if statement d. many-way if statement ____ 44. A program that tolerates errors in user inputs and recovers gracefully is: a. combinatorial c. clear b. robust d. solid ____ 45. When a program attempts to add one to the most positive integer in the range what value is yielded. a. 0 c. null b. the most negative integer d. a random integer ____ 46. In general, how many combinations of true and false are there for n operands in a truth table. a. 2n c. 2n b. n d. 4n ____ 47. What is the symbol for the NOT operator in Java? a. ! c. <> b. ~ d. N ____ 48. Which of the following Boolean expressions is equivalent to: !( p && q ) a. !p && !q c. p && !q b. !p && q d. !p || !q ____ 49. The approach in which evaluation of a Boolean expression stop as soon as possible is called: a. truth evaluation c. complete evaluation b. short-circuit evaluation d. ASAP-evaluation ____ 50. What is used in Java to determine if two string objects contain equal strings. a. == c. equals( ) b. compare( ) d. isEqual( ) ____ 51. An Artificial Intelligence program that learns a set of rules is called a: a. softbot c. smart program b. robot d. neural net

Completion Complete each sentence or statement.

52. There are a total of __ relational operators in Java. 53. The limited precision of ______numbers can cause loops to execute incorrectly. 54. Numbers declared as ______have about 18 decimal digits of precision. 55. The operator _____ changes the truth value of a condition to the opposite value, i.e. if the operand is true, then this operator makes the condition false. 56. When a loop is placed within another loop it is called a ______loop. 57. A ______can determine the value of any complex Boolean expression.

Short Answer

58. What is the result of the expression (int)Math.round(3.49)? 59. List all of the relational operators in Java. 60. Declaring a loop control variable inside the loop header is preferable on most occasions for what two reasons. 61. Name a good place in a loop to dump key variables with System.out.println statements, to find logic errors. 62. Assuming A is true, B is false and C is true, what is the value of the following boolean expression?

( A && !B ) || !C Chapter 7 AP Test Answer Section

TRUE/FALSE

1. T 2. T 3. F 4. T 5. F 6. F 7. T 8. F 9. F 10. T 11. T 12. T 13. F 14. F 15. T 16. F 17. T 18. F 19. T 20. F 21. F 22. T 23. F 24. F 25. F 26. T

MULTIPLE CHOICE

27. C 28. D 29. D 30. A 31. C 32. B 33. C 34. D 35. A 36. C 37. D 38. C 39. D 40. A 41. C 42. A 43. A 44. B 45. B 46. C 47. A 48. D 49. B 50. C 51. D

COMPLETION

52. 6 53. floating-point 54. double 55. NOT 56. nested 57. truth table

SHORT ANSWER

58. 3 59. >, >=, <, <=, ==, != 60. 1. The loop control variable is visible only within the body of the loop where it is intended to be used.

2. The same name can be declared again in other loops in the same program. 61. Immediately after the initialization statements, inside the loop at the top, inside the loop at the bottom. 62. true