357 Lê Hồng Phong, P.2, Q.10, TP.HCM 1900 7060 - 028 3622 8849 info@luyenthitap.edu.vn

Practice Exercises - ALGORITHMS AND PROGRAMMING - AP Computer Science Principles Premium 2024

ALGORITHMS AND PROGRAMMING QUESTIONS

1. What will the following algorithm display?

(A) 2 14 17

(B) 13 17 5

(C) 2 12 2

(D) 14 17 2

2. What will the following algorithm display?

(A) 13

(B) 17

(C) 18

(D) 19

3. What will the following algorithm display?

(A) 0

(B) 42

(C) 84

(D) 126

4. What will the following algorithm display?

(A) Milk

(B) Milk Cookies Soda

(C) put them in a bag and they stay crisp

(D) Milk Cookies Soda Chips put them in a bag so you know they stay crisp

5. What is the value displayed after the program is run?

(A) a 8

(B) a 6

(C) 8 6

(D) 16 4

6. What is the value displayed after the program is run?

(A) a 8

(B) a c

(C) 8 6

(D) 16 4

7. What will the following algorithm display?

(A) 26

(B) 13

(C) 1

(D) 0

8. What will the following algorithm display?

(A) 26

(B) 13

(C) 1

(D) 0

9. What will the following algorithm display?

(A) 26

(B) 13

(C) 8

(D) 1

10. What will the following algorithm display?

(A) 26

(B) 13

(C) 8

(D) 0

11. What will the following algorithm display?

(A) 26

(B) 13

(C) 2

(D) 0

12. What will the following algorithm display?

(A) 26

(B) 13

(C) 1

(D) 0

13. What will the following algorithm display?

(A) 26

(B) 13

(C) 1

(D) 0

14. What will the following code segment display?

(A) 13 5

(B) 5 13

(C) a b

(D) 13 13

15. If this statement is executed many times, about what percentage of times does it display true?

(A) 9%

(B) 10%

(C) 60%

(D) 100%

16. If this statement is executed many times, about what percentage of times does it display true?

(A) 10%

(B) 20%

(C) 60%

(D) 100%

17. If this statement is executed many times, about what percentage of times does it display true?

(A) 0%

(B) 20%

(C) 60%

(D) 100%

18. If this statement is executed many times, about what percentage of times does it display true?

(A) 0%

(B) 20%

(C) 60%

(D) 100%

19. If this statement is executed many times, about what percentage of times does it display true?

(A) 0%

(B) 20%

(C) 60%

(D) 100%

20. If this statement is executed many times, about what percentage of times does it display true?

(A) 9%

(B) 10%

(C) 60%

(D) 100%

21. If this statement is executed many times, about what percentage of times does it display true?

(A) 20%

(B) 40%

(C) 60%

(D) 100%

22. If this statement is executed many times, about what percentage of times does it display true?

(A) 0%

(B) 40%

(C) 60%

(D) 100%

23. What is the percentage of times that this algorithm displays true?

(A) 0%

(B) 40%

(C) 60%

(D) 100%

24. The algorithm below displays true 60% of the time.

What can replace the missing condition so the code segment works as intended?

(A) x < 10

(B) x ≤ 10

(C) x > 10

(D) x ≥ 10

25. What is the value displayed after the algorithm is run?

(A) 0

(B) 1

(C) 10

(D) 13

26. What is the value displayed after the algorithm is run?

(A) 0

(B) 1

(C) 10

(D) 12

27. What is the value displayed after the algorithm is run?

(A) 0

(B) 1

(C) 9

(D) 12

28. What is the value displayed after the algorithm is run?

(A) 0

(B) 1

(C) 10

(D) Nothing is displayed due to an infinite loop.

29. What is the value displayed after the algorithm is run?

(A) 0

(B) 1

(C) 10

(D) 12

30. What is the value displayed after the algorithm is run?

(A) 0

(B) 9

(C) 10

(D) 12

31. What is the value displayed after the algorithm is run?

(A) 2 4 6

(B) 0 2 4 6

(C) 6

(D) 8

32. What is the value displayed after the algorithm is run?

(A) 2 4 6

(B) 0 2 4 6

(C) 6

(D) 8

33. What is the value displayed after the algorithm is run?

(A) Nothing is displayed.

(B) Even Even Even

(C) ODD ODD ODD

(D) 2 4 6

34. What is the value displayed after the algorithm is run?

(A) Nothing is displayed.

(B) Even Even Even

(C) ODD ODD ODD

(D) 2 4 6

35. What is the value displayed after the algorithm is run?

(A) 0

(B) 2

(C) 5

(D) 6

36. What is displayed after the algorithm is run?

(A) 0

(B) 3

(C) 9

(D) 12

37. What is displayed after the algorithm is run?

(A) 0

(B) 3

(C) 9

(D) 12

38. How many numbers will the following algorithm display?

(A) 0

(B) 10

(C) 100

(D) An infinite amount

39. What will be printed when this algorithm is run?

(A) Pineapple

(B) Kumquat

(C) Star Fruit

(D) Pineapple Kumquat Star Fruit

40. How many times will “fish” be displayed?

(A) 4

(B) 3

(C) 2

(D) 1

41. What is displayed at the end of the algorithm?

(A) Elephant

(B) Alligator Ostrich

(C) Alligator

(D) Elephant Alligator Ostrich

42. A user inputs 5 for the value of a. What is a possible value of c?

a ← INPUT( )

b ← RANDOM(a, 6)

c ← b − 6

(A) −3

(B) −1

(C)  1

(D)  3

43. The following is a truth table for all possible values of A and B.

Which of the following can replace the missing condition?

(A) (A AND B)

(B) (A OR B)

(C) (A OR NOT(A))

(D) (A AND NOT(A))

44. The following is a truth table for all possible values of A.

Which of the following can replace the missing condition?

(A) (A AND B)

(B) (A OR B)

(C) (A OR NOT(A))

(D) (A AND NOT(A))

45. Which of the following statements describes the major building blocks of algorithms?

I. Sequencing—statements execute in a given order

II. Selection—Boolean conditions determine an algorithm’s path

III. Iteration—the repetition of parts of an algorithm

(A) I only

(B) I and II only

(C) II and III only

(D) I, II, and III

46. A programmer is writing code to display the difference of the squares of two user-inputted numbers (i.e., a2 − b2). The following lines represent parts of the code. What order should they be placed in?

1. DISPLAY(a - b)

2. b ← INPUT()

3. a ← a * a

4. a ← INPUT()

5. b ← b * b

(A) 1, 2, 3, 4, 5

(B) 1, 2, 4, 5, 3

(C) 2, 4, 3, 5, 1

(D) 4, 2, 5, 1, 3

47. An algorithm compares the user-inputted number picked to the randomly selected number drawing and calls the method victoryJingle( ) if the two are the same. What should replace <Missing Code> in the following algorithm?

(A) IF(picked AND drawing)

(B) IF(picked = drawing)

(C) IF(picked ≠ drawing)

(D) IF(picked NOT drawing)

48. Using the program below, where will the robot land after the code segment executes?

(A)

(B)

(C)

(D)

49. Using the program below, which of the following is a possible landing spot after the code segment executes?

Line 1: move ← INPUT()

Line 2: REPEAT move TIMES

Line 3: {

Line 4: MOVE_FORWARD()

Line 5: ROTATE_RIGHT()

Line 6: }

(A)

(B)

(C)

(D)

50. Using the program below, what are the possible locations the robot could visit?


What are the possible landing spots for the robot?

(A)

(B)

(C)

(D)

51. Using the program below, where will the robot land after the code segment executes?

Starting Grid

(A)

(B)

(C)

(D)

52. A programmer is creating an algorithm that doubles the square root of an inputted number and prints “small” if the result is less than 100. Which of the following would be an appropriate way to express it?

(A) input number n1

square root of n1

multiply n1 by 2

is n1 < 100?

yes -> print “small”

no -> do nothing

(B)

(C)

 

(D) All of the above

53. Which of the following MUST an algorithm be written in or be converted into to be executed by a computer?

(A) Natural language

(B) Pseudocode

(C) High-level language

(D) Low-level machine language

54. Suppose that a programmer has created an algorithm using a low-level assembly language. If the algorithm is translated exactly into a higher-level language such as Python, will the solution utilized still work? Why?

(A) Yes, because an exact translation of an algorithm affects only the way that it is read.

(B) Yes, because an algorithm will always work regardless of the language.

(C) No, because the readability of a language affects how complex algorithms can be.

(D) No, because an algorithmic solution can exist only in the language it is written in.

55. Why is it important that algorithms be executed in a reasonable time?

(A) An algorithm that does not execute in a reasonable time will break the computer it is running on.

(B) An algorithm that does not execute in a reasonable time will be rejected by the compiler.

(C) This ensures that the algorithm is capable of handling the data sets it will be given.

(D) This ensures that the algorithm is capable of finding an exact answer.

56. An algorithm has n number of steps. Which of the following would NOT be considered a reasonable number of steps?

(A) n

(B) 4n + 8n2

(C) 100n4

(D) 3n

57. Why might a programmer decide to make a portion of an algorithm heuristic?

(A) Heuristics are more accurate, so adding them makes for a stronger algorithm.

(B) Although heuristics are not as accurate compared to an algorithmic solution, they are much faster to run, which would make the ultimate algorithm more efficient.

(C) Heuristics are always easier to add into a program.

(D) Heuristics make an algorithm much harder to copy.

58. Which of the following would be considered a heuristic solution?

(A) A file-organizing algorithm determines the content of a file based on a certain number of bytes in the beginning of the file.

(B) A sorting algorithm passes every value, swapping two values where the first is lower. This repeats until there are no more swaps left.

(C) An antivirus program scans the entirety of every file on the hard drive.

(D) A searching algorithm determines the bit-level location of a text string in a document.

ALGORITHMS AND PROGRAMMING QUESTIONS PART 2

59. What will the following call to the procedure “mystery” display?

(A) even

(B) odd

(C) True

(D) False

60. What will the following call to the procedure “mystery” display?

(A) even

(B) odd

(C) True

(D) False

61. What will the following call to the procedure “mystery” display?

(A) 0

(B) 1

(C) 2

(D) Nothing is returned due to an infinite loop.

62. What will the following call to the procedure “mystery” display?

(A) 0

(B) 1

(C) 2

(D) Nothing is returned due to an infinite loop.

63. What will the following call to the procedure “mystery” display?

(A) 2 4

(B) 2 4 5

(C) 9

(D) 6

64. What will the following call to the procedure “mystery” display?

(A) 0 2

(B) 2 4 5

(C) 2

(D) 5

65. What will the following call to the procedure “mystery” display?

(A) 0 2

(B) 2 4 5

(C) 2

(D) 5

66. What will the following call to the procedure “mystery” display?

(A) 0

(B) 2 4 5

(C) 2

(D) 5

67. What will the following call to the procedure “mystery” display?

(A) 0

(B) 2 4 5

(C) 2

(D) 5

68. The following call to the procedure “mystery” is intended to display the number 2.

Which of the following can be used to replace <missing condition> so that the procedure will work as intended?

(A) (num < 2)

(B) (num > 2)

(C) (num < 3)

(D) (num > 3)

69. The following call to the procedure “mystery” is intended to display the number 6.

Which of the following can be used to replace <MISSING CONDITION> so that the procedure will work as intended?

(A) (num < 3)

(B) (num > 3)

(C) (num < 6)

(D) (num > 5)

70. What will the following call to the procedure “mystery” display?

(A) 0

(B) 1

(C) 2

(D) 3

71. What will the following call to the procedure “mystery” display?

(A) 0

(B) 1

(C) 2

(D) 3

72. What will the following program display?

(A) 0

(B) 4

(C) 13

(D) 35

73. What will the following program display?

(A) 0

(B) 12

(C) 16

(D) 35

74. What does list2 contain after the program is run?

(A) [6, 76, 4, 98]

(B) [1, 1, 35]

(C) [1, 1, 35, 6, 76, 4, 98]

(D) [ ]

75. What will the following program display?

(A) 0

(B) 13

(C) 16

(D) 35

76. What will list2 contain after this program segment is run?

(A) [6, 76, 4, 98]

(B) [1, 1, 35]

(C) [1, 1, 35, 6, 76, 4, 98]

(D) [ ]

77. What will list2 contain after this program segment is run?

(A) [0]

(B) [−11]

(C) [−35]

(D) [ ]

78. What will list2 contain after this program segment is run?

(A) [6, 76, 4, 98]

(B) [1, 1, 35]

(C) [1, 1, 35, 6, 76, 4, 98]

(D) [ ]

79. What will the following program display?

(A) 0

(B) 1

(C) 76

(D) 98

80. What will the following program display?

(A) 98

(B) 1

(C) 0

(D) −98

81. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown here. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .).

Which of the following shows a possible result of calling the procedure?

(A)

(B)

(C)

(D)

82. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .).

Which of the following shows the result of calling the procedure when p = 4?

(A)

(B)

(C)

(D) Error. The robot will be out of the grid.

83. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .).

Which of the following shows the result of calling the procedure when p = 4?

(A)

(B)

(C)

(D)

84. The program below is intended to find the highest non-negative number in a list.

Does the program work as intended?

(A) Yes, the program works as intended; it displays 1.

(B) No, the program does not work as intended; the result starts at 0.

(C) Yes, the program works as intended; it displays 3.

(D) No, the program does not work as intended; the “IF” conditional should be “item > result AND item < 0.”

85. The following program is intended to find the lowest number in a list.

Does the program work as intended?

(A) Yes, the program works as intended; it displays −7.

(B) No, the program does not work as intended; the result is always list[1].

(C) Yes, the program works as intended; it displays 1.

(D) No, the program does not work as intended; it produces a runtime error.

86. The following program is intended to find the lowest number in a list.

Does the program work as intended?

(A) Yes, the program works as intended and returns −10.

(B) No, the program does not work as intended; although this code runs, it returns a logical error of the value 0.

(C) Yes, the program works as intended and returns the value −1.

(D) No, the program does not work as intended and results in a runtime error not returning a number.

87. The following program is intended to find the greatest number in a list.

Does the program work as intended?

(A) Yes, the program works as intended, returning −200.

(B) No, the program does not work as intended; the result never changes and instead returns −3.

(C) Yes, the program works as intended and returns −1.

(D) No, the program does not work as intended; it produces a runtime error.

88. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .).

Which of the following shows the result of calling the procedure when mystery(6)?

(A)

(B)

(C)

(D)

89. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .).

Which of the following shows the result of calling the procedure when calling mystery(6)?

(A)

(B)

(C)

(D) Error. The robot will stay in its current location and the program will terminate.

90. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the bottom-left square of the grid and facing toward the top of the grid.

Which of the following code segments produces the result above?

(A)

(B)

(C)

(D)

91. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . . ).

Which of the following could be the result of calling the procedure for any positive value of p?

Select two answers.

(A)

(B)

(C)

(D)

92. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the top-left square of the grid and facing toward the top of the grid.

Code for the procedure “mystery” is shown below. Assume that the parameter p has been assigned a positive integer value of either 0 or 1.

Which of the following shows the result of calling the procedure for the value of p equal to 0 or 1?

Select two answers.


93. The following program is intended to find the average of a class’s scores.

What does the code segment display?

(A) 0

(B) 5

(C) 20

(D) Nothing, runtime error

94. The following code segment is intended to find the maximum.

Which of the following code segments can replace missing code to make the procedure work as intended?

(A) max > item

(B) max = item

(C) item > max

(D) max >= item

95. What does the following code segment return?

(A) The number of even items in list

(B) The number of items in list 

(C) The number of odd items in list

(D) Nothing, the code causes a runtime error

96. The following procedure is intended to find the amount of numbers in a list that are divisible by 5 and 2.

Which of the following code segments can replace missing code to make the procedure work as intended?

(A) item MOD 5 = 0 OR item MOD 2 = 1

(B) item MOD 2 = 1 AND item MOD 5 = 1

(C) item MOD 5 = 0 AND item MOD 2 = 1

(D) item MOD 2 = 0 AND item MOD 5 = 0

97. The following code segment is intended to switch the values of x and y (assume x and y have already been initialized).

What can be done to make the code segment work as intended?

(A) Add “temp ← x” above “x ← y” and replace “y ← x” with “y ← temp.”

(B) Nothing, the code works as intended

(C) Add “temp ← x” below “x ← y

(D) Add “temp ← y” above “x ← y

98. What is the purpose of the following procedure?

(A) To find the amount of items in list

(B) To find the amount of items in list that are equal to number

(C) To find the amount of items in list that do not equal number

(D) Nothing, syntax error

99. What is returned by the following procedure?

(A) Nothing, syntax error

(B) To find the number of items in list that equal number

(C) To find the amount of items in list that are divisible by number

(D) Nothing, runtime error

100. The following procedure is intended to find the amount of values divisible by 15 in list.

What can replace missing code to make the function work as intended?

(A) item MOD 15 = 0

(B) item/15 = 1

(C) item MOD 15 = 1

(D) item/15 = 0

101. What will the following code segment display?

x ← 10

arr ← [“I”, “Love”, “Puppies”]

DISPLAY(arr[(x − LENGTH(arr)) MOD 4])

(A) I

(B) Love

(C) Puppies

(D) ArrayOutOfBoundsException

102. The method isFound(list, item) returns true if the item is in the list. What does list3 contain after the procedure is run below?

(A) [−4, −98]

(B) [1, 6, −4]

(C) [−4]

(D) [1, 35, 6, 76, 4, 98, 5, 1, 8, 96, −4]

103. The method isFound(list, item) returns true if the item is in the list. What does list3 contain after the program is run below?

(A) [−4, −98]

(B) [1, 6, −4]

(C) [−4]

(D) [1, 6]

104. In the following program, which chart displays the ways in which the variables are changed?

x ← 5

y ← 10

z ← 15

z ← x − y

x ← z − 15

y ← y + x

x ← x + y + z


105. A programmer is curious about the accuracy of a new touchpad. To test it, he creates a program that graphically displays the location of the detected pressure and technical information about the device’s current state. What types of input and output are used by this program?

106. Which of the following statements correctly describes a procedure?

I. A procedure can be used in any program as long as the original procedure can be located.

II. A procedure is able to work in any program without translation, regardless of the language in which the program is coded.

III. A procedure can be reused throughout a program.

(A) I only

(B) I and II only

(C) I and III only

(D) II and III only

107. Why is it generally considered a better idea to use procedures in a program?

Select two answers.

(A) Procedures make a program easier to read because they can collapse complex algorithms into a single procedure call.

(B) Procedures make a program easier to share because procedures used in the program will always be in the same file.

(C) Procedures are easier to read because they ensure that the code is working correctly.

(D) Procedures make a program easier to modify because editing a repeated algorithm in a procedure requires editing only the procedure.

108. What is the benefit of using a programming library?

(A) Programming libraries include procedures for common functions, such as exponents, which save programmers from having to make such procedures themselves.

(B) Programming libraries make it easy to translate code from one programming language to another.

(C) Programming libraries allow people to derive code from a compiled program because these libraries correspond between compiled code and high-level code.

(D) Programming libraries slow the execution time of programs.

109. The following question uses a robot in a grid. The robot is presented as a triangle, which is initially in the lower-left square of the grid and facing toward the top of the grid.

Which of the following programs will place the robot in the ending grid location?

110. The following question uses a robot in a grid. The robot is presented as a triangle, which is initially in the lower row of the grid and facing up. Which of the following calls to moveAndTurn will result in the robot’s landing on the star?

 


(A) moveAndTurn()

(B) moveAndTurn()

moveAndTurn()

(C) moveAndTurn()

moveAndTurn()

moveAndTurn()

(D) moveAndTurn()

moveAndTurn()

moveAndTurn()

moveAndTurn()

111. The following question uses a robot in a grid. The robot is presented as a triangle, which is initially in the lower-left row of the grid and is facing up. Which of the following calls to moveAndSpin will result in the robot’s landing on the star?

Select two answers.

(A) moveAndSpin(4, 1)

moveAndSpin(6, 0)

(B) moveAndSpin(0, 1)

moveAndSpin(6, 3)

moveAndSpin(4, 0)

(C) moveAndSpin(7, 3)

(D) moveAndSpin(0, 7)

112. A procedure is shown.

Which of the following values for myList will cause the procedure mystery to assign the value of sum to the total value of all the elements in myList added together?

(A) [3, 5, 23, 88]

(B) [−3, −5]

(C) [−3, 5, −8]

(D) [−4, 0]

113. The following procedure is intended to return true if the string contained in target is in the data structure list.

Which of the following method calls will NOT return true?

(A) mystery(“pig”, [“cat”, “pig”])

(B) mystery(“pig”, [“cat”, “pig”, “snake”])

(C) mystery(“pig”, [“cat”, “pig”, “pig”])

(D) mystery(“Cookie”, [pig”, “elephant”, “Cookie”])

114. The following procedure is intended to return true if the string contained in the target is in the data structure list.

Which of the following test cases will return the value true?

(A) mystery(“pig”, [“cat”, “pig”])

(B) mystery(“pig”, [“cat”, “pig”, “snake”])

(C) mystery(“pig”, [“cat”, “pig”, “pig”])

(D) mystery(“pig”, [“pig”, “elephant”, “Cookie”]

115. In the procedure “mystery” below, the parameter number is a positive integer.

Which of the following best describes the result of running the procedure “mystery”?

(A) The procedure will return 0 if the number is even and −1 if the number is odd.

(B) The procedure will return −1 if the number is greater than 3 and 0 if the number is less than 3.

(C) The procedure will return −1 for all numbers that are greater than 0.

(D) This program will not return a number as it is in an infinite loop.

116. What will the procedure “mystery” below return when the parameters (A, B) are Boolean values?

(A) The output will be TRUE no matter the value of A or B.

(B) The output will be FALSE no matter the value of A or B.

(C) The output will be TRUE if input A is TRUE; otherwise it will be FALSE.

(D) The output will be TRUE if input B is TRUE; otherwise it will be FALSE.

117. What will the procedure “mystery” below return when the parameters (A, B) are Boolean values?

(A) The output will be TRUE no matter the value of A or B.

(B) The output will be FALSE no matter the value of A or B.

(C) The output will be TRUE if input A is TRUE; otherwise it will be FALSE.

(D) The output will be TRUE if input B is TRUE; otherwise it will be FALSE.

Tư vấn miễn phí
PHUONG NAM EDUCATION - HOTLINE: 1900 7060
Để lại số điện thoại
để được Phuong Nam Digital liên hệ tư vấn

Hoặc gọi ngay cho chúng tôi:
1900 7060

Gọi ngay
Zalo chat