APCSP

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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?

1 ** 1 2 * 2

Consider the following procedure. PROCEDURE doSomething(num1, num2) { DISPLAY(num1) RETURN(num1) DISPLAY(num2) } Consider the following statement. DISPLAY(doSomething(10, 20)) What is displayed as a result of executing the statement above?

10 10

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 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.

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 procedure isFound, which takes a word and a text file as input and returns true if the word appears in the text file

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 procedure that returns the sum of the values in the file

Which of the following expressions can be used to generate the string "Happy"?

Concat (Substring ("Harp", 1,2), Substring ("Puppy", 3,3))

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?

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

The student later decides to modify the procedure to calculate the sum of the integers from 1 to max, which represents any positive integer greater than 1. Which of the following changes should be made to the procedure to meet the student's goal? I. The procedure should take max as an input parameter. II. The condition in the REPEAT UNTIL block should be changed to count > max. III. The condition in the REPEAT UNTIL block should be changed to max < 5.

I and II

Let an original file name be stored in the string variable original. Which of the following statements will correctly extract the description and store it in the string variable descr ? I. descr ←← TrimLeft (TrimRight (original, 4), 11) II. descr ←← TrimLeft (TrimRight (original, 11), 4) III. descr ←← TrimRight (TrimLeft (original, 11), 4)

I and III

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?

Line 8 Line 10

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?

Lines 8 and 12 should be interchanged.

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?

Making the code run faster

A programmer would like to find the greatest of three integer values a,b, and c. Which of the following expressions will produce the desired result in every case?

Max (Max (a,b) c)

Which of the following code segments will move the robot to the gray square?

MoveXTimes 2 RightXTimes 3 MoveXTimes 3

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?

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

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 ?

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

Which of the following are benefits of procedural abstraction?

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

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?

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

A programmer notices the following two procedures in a library. The procedures do similar, but not identical, things. Procedure Square (n) returns the value n^2. Procedure Cube (n) returns the value n^3. Which of the following procedures is a generalization of the procedures described above?

Procedure Power (n,m) which returns the value n^m

Which of the following are ways in which a programmer can use abstraction to manage the complexity of a program?

Replacing each instance of repeated code with a call to a procedure Replacing several lines of documentation with a single line of documentation name1, name2, name3, and name4 with a list of strings called names

Which of the following best describes the result of running the procedure?

The procedure returns false no matter what the input value is.

Which of the following best describes the result of running the procedure Mystery

The procedure returns true when the initial value of number is even, and it otherwise returns false.

A student is developing a program that allows users to look up the definitions of words that appear in a book. The student plans to perform a large number of searches through a dictionary containing words and their definitions. The student will use a procedure written by a computer scientist to quickly search the dictionary (and knows that the procedure will return a definition if one is available). The student cannot modify the search procedure written by the computer scientist but can call the procedure by supplying a word. Which of the following is a true statement about the student's use of the computer scientist's search procedure?

The student is reusing the computer scientist's procedural abstraction by knowing what the procedure does without knowing how it does it.

Which of the following code segments will move the robot to the gray square along the path indicated by the arrows? procedureBotStepper n

botStepper2 move_Forward botStepper3

The drawCircle procedure is to be used to draw the following figure on a coordinate grid. Let the value of the variable x be 2, the value of the variable y be 2, and the value of the variable r be 1. Which of the following code segments can be used to draw the figure?

drawCircle(x, y, r) drawCircle(x, y + 3, r) drawCircle(x + 3, y, r) drawCircle(x + 3, y + 3, r)

The drawLine procedure is to be used to draw the following figure on a coordinate grid. Let the value of the variable xVal be 6 and the value of the variable yVal be 5. Which of the following code segments can be used to draw the figure?

drawLine(1, 5, xVal, yVal) drawline(1, 5, xVal, yVal + 2) drawline(1, 5, xVal, yVal - 2)

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?

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

A programmer wants to create a new string by removing the character in position n of the string oldStr. For example, if oldStr is "best" and n is 3, then the new string should be "bet". Assume that 1 < n < len(oldStr). Which of the following code segments can be used to create the desired new string and store it in newStr ?

left ←← substring(oldStr, 1, n - 1) right ←← substring(oldStr, n + 1, len(oldStr)) newStr ←← concat(left, right) newStr ←← substring(oldStr, 1, n - 1) newStr ←← concat(newStr, substring(oldStr, n + 1, len(oldStr)))

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). Which of the following can be used to assign the intended value to count ?

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

The device is positioned at the upper-left corner of a sheet of paper and is facing right. Which of the following code segments will produce the drawing shown?

penDown() goForward(10) penUp() turnRight(90) goForward(10) turnRight(90) penDown() goForward(10)

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") ?

to you happy birthday

The drawCircle procedure is to be used to draw the following figure on a coordinate grid. Which of the following code segments can be used to draw the figure?

x ←← 4 y ←← 1 r ←← 0 REPEAT 3 TIMES { r ←← r + 1 y ←← y + 1 drawCircle(x, y, r) } x ←← 4 y ←← 4 r ←← 3 REPEAT 3 TIMES { drawCircle(x, y, r) y ←← y - 1 r ←← r - 1 }

The procedure (x,y,r) can be used to draw a circle on a coordinate grid. The circle is centered at the coordinate (x,y) and has a radius of r units. The procedure will be used to draw the following figure on a coordinate grid. Which of the following code segments can be used to draw the figure?

xPos <-- 3 yPos <-- 6 Repeat 3 Times { DrawCircle (xPos, yPos, 2) xPos <-- xPos +2 yPos <-- yPos -2 }

The drawLine procedure is to be used to draw the following figure on a coordinate grid. Which of the following code segments can be used to draw the figure?

xVal ←← 1 yVal ←← 0 len ←← 1 REPEAT 5 TIMES { drawLine(xVal, yVal, xVal, yVal + len) xVal ←← xVal + 1 len ←← len + 1 }


Kaugnay na mga set ng pag-aaral

BISC 1005 Mastering Biology chapter 8

View Set

CH4: Prokaryotic & Eukaryotic Cells

View Set

Physics 102 Lab Final: Study Guide

View Set

Math 115 Chapter 7-1 Estimating a Population Proportion

View Set

Home Inspection Ch. 07: Electrical Systems

View Set

Oral Pathology exam 3 (CH 8 AND 9)

View Set