AP COMP SCI UNIT 4 QUIZ PRACTICE JAN 22, 2021
IF (num > 10) { num ← num * 2 } IF (num < 40) { num ← num / 2 } DISPLAY (num) What number, if originally assigned to num, would display the same value for num after executing the above block of code?
ANSWER: 15 EXPLAINED: Tracing the value of num = 15 through this code... 15 is greater than 10, so we multiply num by 2 and then store it back to num - resulting in num now be equal to 30. 30 is less than 40, so we divide num by 2 and store it back to num resulting in num now being equal to 15. It would then display 15 which is the value we started with.
Consider the following procedures. PROCEDURE math (ans1, ans2) { a ← INPUT() b ← INPUT() c ← math2(a, b) e ← a + ans1 f ← b + ans2 g ← math2(e, f) h ← c + g DISPLAY(h) }PROCEDURE math2 (res1, res2) { d ← res1 + res2 RETURN(d) } What is displayed as a result of executing the following program, if when prompted, the user enters a = 3 and b = 4?
ANSWER: 17 EXPLAINED: Tracing through the program: At first, math is called and sent 1 and 2 - so 1 is stored to ans1 and 2 is stored to ans2. If the user inputs 3 for a and 4 for b, the program then calls function math2 and passed values of 3 and 4. So now inside of math2, 3 is stored to res1 and 4 is stored to res3. Inside of this function, res1 and res2 are added together - so then 7 is stored to d and then returned back to the original function. So now 7 was stored back to the variable c. Then a and ans1 are added together (3 + 1 = 4) and b and ans2 are added together (4 + 2 = 6). Each of these values are stored back to e and f. Then those values, e and f (4 and 6) are again sent to math2, which simply adds the values together and returns it back to the function. So 10 is sent back to math and stored to the value of g. Then c (7) and g (10) are added together and displayed. So the display will be 17.
What is the result of running the following code segment? num ← [1, 2, 3, 4, 5] answer ← 0 FOR EACH value IN num { x ← value * 2 answer ← answer + x } DISPLAY (answer)
ANSWER: 30 EXPLAINED: If we trace through the code: Each time the FOR EACH loop runs, a new value from the list of numbers is stored to the variable "value." 1. value is multiplied by 2 and stored to x (the first time through the code, 1 is multiplied by 2 and the value 2 is stored to x) 2. Then x plus answer is stored back to answer (first time through, we take 2 + 0, so 2 is stored back to answer) Steps 1 and 2 are repeated for each value - essentially summing together 2 times each value in the list... 2 + 4 + 6 + 8 + 10. Then the value for answer is displayed - which is 30.
What will be displayed after running this section of code: i ← 4 REPEAT 4 TIMES { i = i + 1 DISPLAY (i) }
ANSWER: 8 EXPLAINED: The loop runs 4 times - BEFORE displaying the value of i, first we add 1 to it. This would make the first displayed value a 5 and then it would repeat 3 more times giving us the output: 5 6 7 8
A gaming console is being programmed to ask questions, such as "Do you love computer science?", of a user when they log into the system. The programmer wants to ensure that the user answers "Yes", so he creates a loop that will re-ask the question if the user answers "No" and continues to repeat the loop until the user answers "Yes." What type of loop would the programmer likely use in this scenario?
ANSWER: A while loop EXPLAINED: This is correct. A while loop could be used with the condition that the loop repeats while the variable to which the users answer is assigned is not equal to "Yes". This would cause the loop to repeat until the user answered "Yes".
In order to be inducted into a school's National Honor Society (NHS), they must meet certain criteria for their GPA, serviceHours and grade. The expression below indicates whether a student has been accepted as a member of the NHS. (GPA ≥ 3.65) AND (serviceHours ≥ 30) AND (NOT grade = 9) Which of the following values would indicate a student that would be accepted into NHS?
ANSWER: GPA = 3.65, serviceHours = 30, grade = 12 EXPLAINED: If we were to plug in these values to the boolean expressions: (3.65 ≥ 3.65) AND (30 ≥ 30) AND (NOT 12 = 9) True AND True AND NOT False True AND True AND True True
A list is a form of data abstraction that helps us to manage the complexity in our program by giving a collection of data a name without the specific details of the representation. What else is true about a list?
ANSWER: Individual elements in a list are typically referenced by using something called an index. EXPLANATION: The position in which an element is located is index.
What best describes the result of running the program below? number ← RANDOM (1, 6)REPEAT UNTIL number > 100 { number ← number * 2 } IF (number = 128) { DISPLAY (true) } ELSE { DISPLAY (false) }
ANSWER:If number is a 1, 2 or 4, then the program will display true. EXPLAINED: If we trace the values of number = 1, 2, 3, 4, 5 and 6 we get the following: 1 would be 1 then 2 then 4, 8, 16, 32, 64, 128 and result in TRUE 2 would be 2 then 4 then 8, 16, 32, 64, 128 and result in TRUE 3 would be 3 then 6 then 12, 24, 48, 96, 192 and result in FALSE 4 would be 4 then 8 then 16, 32, 64, 128 and result in TRUE 5 would be 5 then 10 then 20, 40, 80, 160 and result in FALSE 6 would be 6 then 12 then 24, 48, 96, 192 and result in FALSE
Instead of solving a problem using one large code segment, we can base that solution off of the solutions of smaller sub-problems. This is often referred to as modularity. This helps to manage the complexity of the program and can be done in the form of what type of abstraction?
ANSWER:Procedural Abstraction EXPLAINED: Procedural abstraction is the process of creating different smaller functions which have a specific (reusable purpose) for use in solving a larger problem.
Which of the following is an advantage that text-based programming languages (i.e. Python) have over block-based programming languages (i.e. Scratch)?
Answer: Text-based programming languages offer programmers more detailed control over lower-level functionality when writing a program. EXPLAINED: In block-based programs, we are restricted to use the blocks provided to us and often times are limited to how we can use them. In a text-based language we are given more flexibility to fine-tune lower-level functionality because we are NOT limited to a set number of available blocks.
Although not all programming environments support documentation in the form of comments, when programmers use one that does support it, they should be adding comments throughout the programming process. Although the comments are ignored by the compiler when executing, they are still useful. Which of the following are true about documentation / comments?
It helps in developing and maintaining correct programs when working in an individual or collaborative setting. It provides a way to acknowledge any code segments that were developed collaboratively or by another source (generally this acknowledgement is in the form of the author's name and the origin of the code). It is used to describe the function of a code segment, procedure or program and how it was developed.