APCS Midterm

Ace your homework & exams now with Quizwiz!

Assume that both lists and strings are indexed starting with index 1. The list wordList has the following contents. ["abc", "def", "ghi", "jkl"] Let myWord be the element at index 3 of wordList. Let myChar be the character at index 2 of myWord. What is the value of myChar ?

"h"

Consider the following code segment. [i<--1] {REPEAT UNTIL (i>4)} rand<--RANDOM [1, i] DISPLAY [rand] i <-- i + 1 Which of the following CANNOT be displayed as a result of executing the code segment?

1 3 2 4

A time stamp indicates the date and time that a measurement was taken. A data scientist has a list containing 10,000 time stamps, sorted in chronological order. Which of the following is closest to the maximum number of values that will need to be examined when performing a binary search for a value in the list?

15

Consider the following code segment, which uses the variables r, s, and t. r ← 1 s ← 2 t ← 3 r ← s s ← t DISPLAY (r) DISPLAY (s) What is displayed as a result of running the code segment?

2.3

A sorted list of numbers contains 200 elements. Which of the following is closest to the maximum number of list elements that will need to be examined when performing a binary search for a particular value in the list?

8

Consider the 4-bit binary numbers 0011, 0110, and 1111. Which of the following decimal values is NOT equal to one of these binary numbers?

9

Which of the following is a benefit of using a simulation instead of an experiment to make a prediction? Select two answers.

A simulation allows investigation of a phenomenon without the real-world limitations on time, safety, or budget. A simulation can be used to model real-world events that are impractical for experiments.

A teacher is writing a code segment that will use variables to represent a student's name and whether or not the student is currently absent. Which of the following variables are most appropriate for the code segment?

A string variable named studentName and a Boolean variable named isAbsent

The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is the most appropriate data type for isOpen ?

Boolean

A programmer is creating an algorithm that will be used to turn on the motor to open the gate in a parking garage. The specifications for the algorithm are as follows. - The gate should not open when the time is outside of business hours. - The motor should not turn on unless the gate sensor is activated. - The motor should not turn on if the gate is already open. Which of the following algorithms can be used to open the gate under the appropriate conditions?

Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is not, turn on the motor.

In the following procedure, the parameters x and y are integers. {PROCEDURE calculate [x, y] (result <-- x + y) (result <-- result / x) (DISPLAY [result] Which of the following is the most appropriate documentation to appear with the calculate procedure?

Displays the value of (x + y) / x.The value of the parameter x must not be 0.

For which of the following situations would it be best to use a heuristic in order to find a solution that runs in a reasonable amount of time?

Finding the fastest route that visits every location among nn locations, which requires n!n! possible routes be examined.

In a certain game, the integer variable bonus is assigned a value based on the value of the integer variable score. - If score is greater than 100, bonus is assigned a value that is 10 times score. - If score is between 50 and 100 inclusive, bonus is assigned the value of score. - If score is less than 50, bonus is assigned a value of 0. Which of the following code segments assigns bonus correctly for all possible integer values of score ? Select two answers.

IF(score > 100) { bonus ←← score * 10 } ELSE { IF(score ≥ 50) { bonus ←← score } ELSE { bonus ←← 0 } } IF(score < 50) { bonus ←← 0 } ELSE { IF(score > 100) { bonus ←← score * 10 } ELSE { bonus ←← score } }

Suppose a large group of people in a room were all born in the same year. Consider the following three algorithms, which are each intended to identify the people in the room who have the earliest birthday based on just the month and day. For example, a person born on February 10 is considered to have an earlier birthday than a person born on March 5. Which of the three algorithms will identify the correct people? I. All the people in the room stand up. All standing people form pairs where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people sit down. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing. That person has the earliest birthday. II. All the people in the room stand up. All standing people form pairs

II only

The cost of a customer's electricity bill is based on the number of units of electricity the customer uses. For the first 25 units of electricity, the cost is $5 per unit. For units of electricity after the first 25, the cost is $7 per unit. Which of the following code segments correctly sets the value of the variable cost to the cost, in dollars, of using numUnits units of electricity?

If (numUnits < or = 25) [cost<--numUnits * 5] Else [cost<--25 * 7 + (numUnits - 25) * 7]

A certain game keeps track of the maximum and minimum scores obtained so far. If num represents the most recent score obtained, which of the following algorithms correctly updates the values of the maximum and the minimum?

If num is less than the minimum, set the minimum equal to num. Otherwise, if num is greater than the maximum, set the maximum equal to num.

Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9, 16, 4, they should be reordered as 4, 9, 16. Which of the following algorithms can be used to place any three numbers in the correct order?

If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them.

In the following procedure, assume that the parameter x is an integer. {procedure mystery x} [y<---(x<0)] {IF(y)} [Display (y)] Which of the following best describes the behavior of the procedure?

It displays true if x is negative and displays nothing otherwise.

The figure below shows four grids, each containing a robot represented as a triangle. The robot cannot move to a black square or move beyond the edge of the grid. Which of the following algorithms will allow the robot to make a single circuit around the rectangular region of black squares, finishing in the exact location and direction that it started in each of the four grids?

Keep moving forward, one square at a time, until the square to the right of the robot is no longer black. Step 2: Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times.

A programmer has a need to round many numeric values to the nearest integer. Which of the following best explains the benefit of using a list as a data abstraction in this situation?

Keeping the numeric values in a list makes it easier to apply the same computation to every data element.

Which of the following is a benefit of using a list as a data abstraction in a program?

Lists often allow their size to be easily updated to hold as many data values as needed.

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

In the following procedure, the parameter max is a positive integer. PROCEDURE printNums(max) { count ← 1 REPEAT UNTIL(count > max) { DISPLAY(count) count ← count + 2 } } Which of the following is the most appropriate documentation to appear with the printNums procedure?

Prints all positive odd integers that are less than or equal to max.

Which of the following are benefits of procedural abstraction? Select two answers.

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.

In the following procedure, the parameter numList is a list of numbers and the parameters j and k are integers. PROCEDURE swapListElements(numList, j, k) { newList ← numList newList[j] ← numList[k] newList[k] ← numList[j] RETURN(newList) } Which of the following is the most appropriate documentation to appear with the swapListElements procedure?

Returns a copy of numList with the elements at indices j and k interchanged.The values of j and k must both be between 1 and LENGTH(numList), inclusive.

A student wants to determine whether a certain problem is undecidable. Which of the following will demonstrate that the problem is undecidable?

Show that for one instance of the problem, no algorithm can be written that is capable of providing a correct yes-or-no answer.

A teacher has a goal of displaying the names of 2 students selected at random from a group of 30 students in a classroom. Any possible pair of students should be equally likely to be selected. Which of the following algorithms can be used to accomplish the teacher's goal?

Step 1: Assign each student a unique integer from 1 to 30. Step 2: Generate a random integer n from 1 to 30. Step 3: Select the student who is currently assigned integer n and display the student's name. Step 4: The student who was selected in the previous step is assigned 0. All other students are reassigned a unique integer from 1 to 29. Step 5: Generate a new random integer n from 1 to 29. Step 6: Select the student who is currently assigned integer n and display the student's name.

Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ? Select two answers.

Step 1: Set x to 0. Step 2: Increment x by 1. Step 3: If x is not divisible by 3, then display x. Step 4: Repeat steps 2 and 3 until x is 20. Set x to 1. Step 2: If x is divisible by 3, then do nothing; otherwise display x. Step 3: Increment x by 1. Step 4: Repeat steps 2 and 3 until xi s greater than 20.

A student is creating an algorithm to display the distance between the numbers num1 and num2 on a number line. The following table shows the distance for several different values. Value of num1 - Value of num2 - Distance Between num1 and num2 5 2 3 1 8 7 -3 4 7 Which of the following algorithms displays the correct distance for all possible values of num1 and num2?

Subtract num1 from num2 and store the result in the variable diff. Step 2: Take the absolute value of diff and display the result.

A graphic artist uses a program to draw geometric shapes in a given pattern. The program uses an algorithm that draws the shapes based on input from the artist. The table shows the approximate number of steps the algorithm takes to draw different numbers of shapes. Number of Shapes Drawn 4 5 6 7 Number of Steps 17 24 35 50 Based on the values in the table, which of the following best characterizes the algorithm for drawing n shapes, where n is a very large number?

The algorithm runs in a reasonable amount of time because it will use approximately n2n2 steps to draw nn shapes.

Consider the following code segment. [result <--2] REAPEAT 3 TIMES (result<--result*5) [DISPLAY (result) Which of the following best describes the behavior of the code segment?

The code segment displays the value of 2(53)2(53) by initializing result to 2 and then multiplying result by 5 a total of three times.

A programmer wrote the program below. The program uses a list of numbers called numList. The program is intended to display the sum of the numbers in the list. {sum<--numList [1]} [FOR EACH value IN numList] (`sum<--sum + value) DISPLAY (sum) In order to test the program, the programmer initializes numList to [0, 1, 4, 5]. The program displays 10, and the programmer concludes that the program works as intended. Which of the following is true?

The conclusion is incorrect; using the test case [0, 1, 4, 5] is not sufficient to conclude the program is correct.

The list listOne is a sorted list of numbers that contains 700 elements. The list listTwo is a sorted list of numbers that contains 900 elements. Let x represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listOne, and let y represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listTwo. Which of the following statements about x and y is true?

The value of x is approximately equal to the value of y.

Consider the following code segment, where exam and presentation are integer variables and grade is a string variable. IF((exam > 90) AND (presentation > 80)) { grade ← "A" } IF((exam > 80) OR (presentation > 75)) { grade ← "B" } ELSE { IF((exam > 70) OR (presentation > 60)) { grade ← "C" } ELSE { IF(exam > 60) { grade ← "D" } ELSE { grade ← "F" } } } Under which of the following conditions will the value "C" be assigned to the variable grade ?

When the value of exam is 80 and the value of presentation is 60

Consider the following code segment. yourList <-- [20, 40 , 60 , 80] myList<-- [10, 30, 50, 70] yourList <-- myList What are the contents of yourList after the code segment is executed?

[10, 30, 50, 70]

The variable age is to be used to represent a person's age, in years. Which of the following is the most appropriate data type for age ?

number

Consider a game where a player spins a wheel divided into four identical sections, labeled A, B, C, and D. If the player spins A, the score is 10. If the player spins B, the score is 5. If the player spins C or D, the score is -1. The following code segment is intended to implement the game. <MISSING STATEMENT> IF (spin = 1) score <--10 ELSE IF ELSE Which of the following could be used as a replacement for <MISSING STATEMENT> so the code segment works as intended?

spin<--RANDOM (1,4)

In the following code segment, assume that x and y have been assigned integer values. sum ← 0 REPEAT x TIMES { REPEAT y TIMES { sum ← sum + 1 } } At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? Select two answers.

sum ←← 0 z ←← x * y REPEAT z TIMES { sum ←← sum + 1 } sum ←← 0 REPEAT y TIMES { REPEAT x TIMES { sum ←← sum + 1 } }


Related study sets

Chapter 33: The Preschooler and Family NCLEX

View Set

발표와토론 - 기말고사 범위 2

View Set

Chapter 11-Project Analysis and Evaluation

View Set

Health of the Individual, Family, and Community (PrepU)

View Set

2C- 2- Fair Credit Reporting Act of 1970 (FCRA)

View Set

Digestive system Human A&P Mr. Siegel.

View Set

College U.S. History (Chapters 7 &8)

View Set

Materials Science: 10 Things Every Engineer Should Know: Thing 5

View Set

Chapter 25 ebook Quiz, Chapter 25 Bleeding AAOS Emergency Care and Transportation of the Sick and Injured Eleventh Edition

View Set