🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

1.2.1 What Does this Algorithm Do KEY.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Solution Reference for What Does this Algorithm Do? 1.2.1 Question: 1 Consider the code segment below. temp ← x x ← y y ← temp Based on the code segment, how would you best describe what the program is doing to variable x and variable y? ✔ The program swaps th...

Solution Reference for What Does this Algorithm Do? 1.2.1 Question: 1 Consider the code segment below. temp ← x x ← y y ← temp Based on the code segment, how would you best describe what the program is doing to variable x and variable y? ✔ The program swaps the value of x and y The program sets the value of x to y The program sets the value of y to x The program sets the value of temp to x and then to y Question: 2 Consider the code segment below. DISPLAY ("What is your name?") name ← INPUT () DISPLAY ("Hello") DISPLAY (name) What is displayed as a result if the user inputs “Karel” to the program? What is your name? Hello Karel What is your name?HelloKarel ✔ What is your name? Hello Karel Hello Karel Question: 3 Consider the code segment below. PROCEDURE Mystery (number) { RETURN ((number MOD 2) = 0) } Which of the following best describes the behavior of the Mystery PROCEDURE? Returns the remainder of number divided by 2 ✔ Returns whether or not number is even Returns number divided by 2 Returns whether or not number is equal to 0 Question: 4 In the program below, the initial value of a is 0 and the initial value of b is 1. IF (b = 0) { IF (b = 1) { DISPLAY ("Karel") } } ELSE { IF (a = 1) { DISPLAY ("Tracy") } ELSE { DISPLAY ("Dog") } DISPLAY ("Turtle") } What is displayed as a result of running this program? Karel Dog Tracy Turtle Tracy Dog ✔ Dog Turtle Question: 5 Consider the code segment below. PROCEDURE Mystery (number) { DISPLAY ("WOW") REPEAT number TIMES { DISPLAY ("!") } } What is the result of calling the Mystery procedure with an input of 3? WOW!!! ✔ WOW ! ! ! WOW ! ! ! !!! Question: 6 Consider the code segment below. x ← RANDOM (1, 10) y ← RANDOM (10, 20) DISPLAY (x = y) Which of the following best describes the behavior of the the code? The program will always display true The program will always display false ✔ The program could display either true or false The program will error Question: 7 Consider the code segment below. PROCEDURE Mystery (word, list) { FOR EACH item IN list { IF (item = word) { RETURN (true) } } RETURN (false) } Which of the following best describes the behavior of the Mystery PROCEDURE? ✔ Return whether or not word is in list Return the index of word in list Display true for each occurrence of word in list Display the index of word in list Question: 8 Consider the code segment below. PROCEDURE Mystery (word, list) { x ← 0 y ← 0 FOR EACH item IN list { IF (item = word) { x ← x + 1 } ELSE { x ← 0 } IF (x > y) { y ← x } } RETURN (y) } What is the value of Mystery("karel", ["karel", "karel", "tracy", "karel", "karel", "karel", "tracy"]) 2 ✔ 3 4 5 Question: 9 Consider the code segment below. PROCEDURE Mystery (x) { result ← 0 REPEAT 5 TIMES { result ← x + result } RETURN result } Which of the following expressions is equivalent to the value returned from Mystery(x)? x+5 x/5 x-5 ✔ x*5 Question: 10 Consider the code segment below. PROCEDURE Mystery (x) { result ← 0 REPEAT 2 TIMES { REPEAT 3 TIMES { result ← x + result } } RETURN result } Which of the following expressions is equivalent to the value returned from Mystery(x)? 2+3+x 2 + (3 * x) ✔ 2*3*x 3 + (2 * x) Question: 11 Consider the code segment below. PROCEDURE Mystery (x) { result ← 0 REPEAT x TIMES { result ← result + 1 } RETURN result } Which of the following expressions is equivalent to the value returned from Mystery(x)? ✔ x x+1 0 x*2 Question: 12 Consider the code segment below. PROCEDURE Mystery (x, y) { result ← 0 REPEAT y TIMES { REPEAT x TIMES { result ← result + 1 } } RETURN result } Which of the following expressions is equivalent to the value returned from Mystery(x, y)? ✔ y*x y+x y/x y-x Question: 13 Consider the code segment below. You can assume that Mystery returns either true or false, and Input_List_From_User asks the user for several values and returns them as a list. inputList ← Input_List_From_User () resultList ← [] FOR EACH item in inputList { IF (Mystery (item)) { APPEND (resultList, item) } } Based on the code segment, how would you best describe the value of resultList? resultList contains all items from inputList that did not meet the Mystery condition resultList is always an empty list resultList contains all items from inputList that met the Mystery ✔ condition resultList always contains all items from inputList Question: 14 Consider the code segment below. You can assume that Input_List_From_User asks the user for several values and returns them as a list. inputList ← Input_List_From_User () resultList ← [] FOR EACH item in inputList { APPEND (resultList, Mystery (item)) } Based on the code segment, how would you best describe the value of resultList? resultList is always an empty list resultList always contains all items from inputList resultList contains all items from inputList that met the Mystery condition resultList contains the result of calling the Mystery procedure on ✔ every item in inputList Question: 15 Consider the code segment below. what ← 1 does ← what + 1 the ← what + does + 3 fox ← the + what say ← 3 result ← what + does + the + fox + say DISPLAY (result) Based on the code segment, what is displayed after running the program? 18 ✔ 19 20 21

Use Quizgecko on...
Browser
Browser