review 7 for exam

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

A student has a data file containing 10,000 numerical values. The student is writing a program to compute the average of the numbers contained in the file. Which of the following procedures is most likely to be useful in the student's program? A A procedure that returns the quotient when dividing one value by another B A procedure that returns the sum when adding one value to another C A procedure that returns the sum of the values in the file D A procedure that returns true if the file contains any duplicate values and returns false otherwise

C A procedure that returns the sum of the values in the file

Consider the following procedures. PROCEDURE proc1(str) { DISPLAY(str) DISPLAY("happy") } PROCEDURE proc2(str1, str2) { proc1(str2) DISPLAY(str1) } What is displayed as a result of the procedure call proc2("birthday", "to you") ? A birthday happy to you B birthday happy birthday C to you birthday happy D to you happy birthday

D to you happy birthday

The following code segment is intended to draw the figure. startX ←← 2 startY ←← 6 endX ←← 8 endY ←← 8 REPEAT 4 TIMES { <MISSING CODE> } Which of the following can be used to replace <MISSING CODE> so that the figure is drawn correctly? A DrawLine (startX, startY, endX, endY) endY ←← endY - 2 B DrawLine (startX, startY, endX, endY) endX ←← endX - 2 endY ←← endY - 2 C endY ←← endY - 2 DrawLine (startX, startY, endX, endY) D endX ←← endX - 2 endY ←← endY - 2 DrawLine (startX, startY, endX, endY)

A DrawLine (startX, startY, endX, endY) endY ←← endY - 2

In the following procedure, the parameter str is a string and the parameter num is a number. PROCEDURE printArgs(str, num) { DISPLAY(num) DISPLAY(str) DISPLAY(num) } Consider the following code segment. printArgs("**", 1) printArgs("*", 2) What is displayed as a result of executing the code segment? A 1 * 1 2 ** 2 B 1 ** 1 2 * 2 C * 1 * ** 2 ** D ** 1 ** * 2 *

B 1 ** 1 2 * 2

Which of the following procedures would be most useful as part of a program to determine whether a word appears in two different text files? A A procedure getWords, which takes a positive integer n and a text file as input and returns the first n words in the text file. B A procedure isFound, which takes a word and a text file as input and returns true if the word appears in the text file C A procedure textMatch, which takes two text files as input and returns true if the two text files are identical. D A procedure sameSize, which takes two text files as input and returns true if the two text files contain the same number of words.

B A procedure isFound, which takes a word and a text file as input and returns true if the word appears in the text file

In the following procedure, the parameter age represents a person's age. The procedure is intended to return the name of the age group associated with age. People who are under 18 are considered minors, people who are 65 and older are considered senior citizens, and all other people are considered adults. The procedure does not work as intended. Line 1: PROCEDURE ageGroup(age) Line 2: { Line 3: result ←← "adult" Line 4: IF(age ≥ 65) Line 5: { Line 6: result ←← "senior citizen" Line 7: } Line 8: RETURN(result) Line 9: Line 10: result ←← "adult" Line 11: IF(age < 18) Line 12: { Line 13: result ←← "minor" Line 14: } Line 15: RETURN(result) Line 16: } Removing which two lines of code will cause the procedure to work as intended? Select two answers. A Line 3 B Line 8 C Line 10 D Line 15

B Line 8 C Line 10

A computer science student completes a program and asks a classmate for feedback. The classmate suggests rewriting some of the code to include more procedural abstraction. Which of the following is NOT a benefit of procedural abstraction? A Making the code more readable B Making the code run faster C Providing more opportunities for code reuse D Reducing the amount of duplicated code

B Making the code run faster

A programmer notices the following two procedures in a library. The procedures do similar, but not identical, things. Procedure MaxTwo (x, y) returns the greater of its two integer parameters. Procedure MaxThree (x, y, z) returns the greatest of its three integer parameters. Which of the following procedures is a generalization of the MaxTwo and MaxThree procedures? A Procedure Min (x, y), which returns the lesser of its two integer parameters B Procedure Max (numList), which returns the maximum value in the list of integers numList C Procedure MaxFour (w, x, y, z), which returns the greatest of its four integer parameters D Procedure OverMax (numList, max), which returns the number of integers in numList that exceed the integer value max

B Procedure Max (numList), which returns the maximum value in the list of integers numList

A student is writing a program that is intended to replace each negative value in a particular column of a spreadsheet with the value 0. Which of the following procedures is most likely to be useful in the student's program? A A procedure containsNegatives, which returns true if any negative values appear in the column and returns false otherwise. B A procedure countNegatives, which returns the number of negative values that appear in the column. C A procedure findNegative, which returns the row number of the first negative value that appears in the column or -1 if there are no negative values. D A procedure minimum, which returns the minimum value that appears in the column.

C A procedure findNegative, which returns the row number of the first negative value that appears in the column or -1 if there are no negative values.

A list of numbers is considered increasing if each value after the first is greater than or equal to the preceding value. The following procedure is intended to return true if numberList is increasing and return false otherwise. Assume that numberList contains at least two elements. Line 1: PROCEDURE isIncreasing(numberList) Line 2: { Line 3: count ←← 2 Line 4: REPEAT UNTIL(count > LENGTH(numberList)) Line 5: { Line 6: IF(numberList[count] < numberList[count - 1]) Line 7: { Line 8: RETURN(true) Line 9: } Line 10: count ←← count + 1 Line 11: } Line 12: RETURN(false) Line 13: } Which of the following changes is needed for the program to work as intended? A In line 3, 2 should be changed to 1. B In line 6, < should be changed to ≥. C Lines 8 and 12 should be interchanged. D Lines 10 and 11 should be interchanged.

C Lines 8 and 12 should be interchanged.

A game program contains the following code to update three score variables, health, food, and knowledge. The maximum values for the three variables are 100, 80, and 25, respectively. health ←← health + 10 IF(health > 100) { health ←← 100 } food ←← food + 1 IF(food > 80) { food ←← 80 } knowledge ←← knowledge + 5 IF(knowledge > 25) { knowledge ←← 25 } The game program's author would like to write a procedure that could be used to update any variable in the game (myScore) that has a maximum value (myLimit) by a given amount (myAmount). A correct call of the procedure is shown below. myScore ←← updateScore(myScore, myAmount, myLimit) Which of the following is a correct implementation of updateScore ? A PROCEDURE updateScore(score, amount, limit) { score ←← score + amount IF (score > limit) { score ←← limit } } B PROCEDURE updateScore(score, amount, limit) { score ←← score + amount IF(score > amount) { score ←← limit } RETURN(limit) } C PROCEDURE updateScore(score, amount, limit) { score ←← score + amount IF(score > limit) { score ←← limit } RETURN(score) } D PROCEDURE updateScore(score, amount, limit) { IF(score = health) { score ←← score + 10 } IF(score = food) { score ←← score + 1 } IF(score = knowledge) { score ←← score + 5 } IF(score > limit) { score ←← limit } }

C PROCEDURE updateScore(score, amount, limit) { score ←← score + amount IF(score > limit) { score ←← limit } RETURN(score) }

Which of the following are benefits of procedural abstraction? Select two answers. A Procedural abstraction prevents programmers from accidentally using the intellectual property of other programmers. B Procedural abstraction eliminates the need for programmers to document their code. C Procedural abstraction makes it easier for people to read computer programs. D Procedural abstraction provides an opportunity to give a name to a block of code that describes the purpose of the code block.

C Procedural abstraction makes it easier for people to read computer programs. D Procedural abstraction provides an opportunity to give a name to a block of code that describes the purpose of the code block.

A computer program contains code in several places that asks a user to enter an integer within a specified range of values. The code repeats the input request if the value that the user enters is not within the specified range. A programmer would like to create a procedure that will generalize this functionality and can be used throughout the program. The correct use of the procedure is shown below, where min is the least acceptable value, max is the greatest acceptable value, and promptString is the string that should be printed to prompt the user enter a value. inputVal ←← getRange(min, max, promptString) Which of the following is a correct implementation of the getRange procedure? A PROCEDURE getRange(min, max, promptString) { DISPLAY(promptString) RETURN(INPUT()) } B PROCEDURE getRange(min, max, promptString) { DISPLAY(promptString) x ←← INPUT() RETURN(x) } C PROCEDURE getRange(min, max, promptString) { DISPLAY(promptString) x ←← INPUT() REPEAT UNTIL(x ≥ min AND x ≤ max) { DISPLAY(promptString) x ←← INPUT() } } D PROCEDURE getRange(min, max, promptString) { DISPLAY(promptString) x ←← INPUT() REPEAT UNTIL(x ≥ min AND x ≤ max) { DISPLAY(promptString) x ←← INPUT() } RETURN(x) }

D PROCEDURE getRange(min, max, promptString) { DISPLAY(promptString) x ←← INPUT() REPEAT UNTIL(x ≥ min AND x ≤ max) { DISPLAY(promptString) x ←← INPUT() } RETURN(x) }

A student's overall course grade in a certain class is based on the student's scores on individual assignments. The course grade is calculated by dropping the student's lowest individual assignment score and averaging the remaining scores. For example, if a particular student has individual assignment scores of 85, 75, 90, and 95, the lowest score (75) is dropped. The calculated course grade is (85+90+95)/3=90(85+90+95)/3=90. A programmer is writing a program to calculate a student's course grade using the process described. The programmer has the following procedures available. Procedure Call(min/sum (numList)) Explanation Min (numList)Returns the minimum value in the list numListSum (numList)Returns the sum of the values in the list numList The student's individual assignment scores are stored in the list scores. Which of the following can be used to calculate a student's course grade and store the result in the variable finalGrade? A finalGrade ←← Sum (scores) / LENGTH (scores) finalGrade ←← finalGrade - Min (scores) B finalGrade ←← Sum (scores) / (LENGTH (scores) - 1) finalGrade ←← finalGrade - Min (scores) C finalGrade ←← Sum (scores) - Min (scores) finalGrade ←← finalGrade / LENGTH (scores) D finalGrade ←← Sum (scores) - Min (scores) finalGrade ←← finalGrade / (LENGTH (scores) - 1)

D finalGrade ←← Sum (scores) - Min (scores) finalGrade ←← finalGrade / (LENGTH (scores) - 1)

Consider two lists of numbers called list1 and list2. A programmer wants to determine how many different values appear in both lists. For example, if list1 contains [10, 10, 20, 30, 40, 50, 60] and list2 contains [20, 20, 40, 60, 80], then there are three different values that appear in both lists (20, 40, and 60). The programmer has the following procedures available. Procedure Call(something()) ExplanationCombine (myList1, myList2)This procedure creates a new list containing the elements from myList1 ​followed by the entries from myList2. The resulting list is returned. For example, if myList1 contains [2, 4, 6] and myList2 contains [1, 5], the procedure will return the list [2, 4, 6, 1, 5].RemoveAllDups (myList)This procedure creates a new list containing the elements of myList with any duplicate values removed. The resulting list is returned. For example, if myList contains [3, 2, 4, 2, 2, 5, 6, 4], the procedure will return the list [3, 2, 4, 5, 6]. Which of the following can be used to assign the intended value to count ? A bothList ←← Combine (list1, list2) uniqueList ←← RemoveAllDups (bothList) count ←← LENGTH (bothList) - LENGTH (uniqueList) B newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) count ←← LENGTH (list1) + LENGTH (list2) - LENGTH (bothList) C newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) count ←← LENGTH (newList1) + LENGTH (newList2) - LENGTH (bothList) D newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) uniqueList ←← RemoveAllDups (bothList) count ←← LENGTH (bothList) - LENGTH (uniqueList)

D newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) uniqueList ←← RemoveAllDups (bothList) count ←← LENGTH (bothList) - LENGTH (uniqueList)


Set pelajaran terkait

Exam 2 - Developmental Psych - Chapter 4 INFANCY

View Set

ST for the ST Chapter 4: Special Populations

View Set

milady Chapter 21 Haircoloring (copied)

View Set

Simulation Lab 10.2: Module 10 Install Linux in VM

View Set

Bio M01 Cheroske Spring 2014 Chapter 13

View Set

Biology unit 4: stages of mitosis

View Set

biology concept questions ch.4-5

View Set

Discovering Chinese V2 L24 Sentencese

View Set