Unit 2 CSP

¡Supera tus tareas y exámenes ahora con Quizwiz!

x ← 0 a ← 3 k ← 4 REPEAT a TIMES { REPEAT k TIMES { x ← x + a } x ← x * (-1) } DISPLAY x When the code above is run, what will be the final value of the variable x, displayed at the end of the program?

-12

Consider the following code: REPEAT UNTIL (x mod 2 = 1) { x ← x + 3 } Assume that x is initially 6. How many times will the loop iterate?

1

Consider the given code using variables a, b, and c. What will be the output of this code? a ← 2 b ← 6 c ← 12 a ← b b ← c DISPLAY(a) DISPLAY(c)

6 12

Program

A collection of program statements that performs a specific task when run by a computer. A program is often referred to as software.

Logic Gate

A hardware abstraction that is modeled by a Boolean function

Sequential Search

A linear search method of finding a targeted value within a list, looking one at a time until a match is found.

Heuristics

A method for deriving an approximate solution - Rules of Thumb but not guaranteed an accurately correct answer

Binary Search

A method of searching by dividing the search interval in half each time.

If else statement

A more thorough version of an if statement that stipulates what is to happen when a certain criteria is not met.

Procedure

A named collection of steps in an algorithm (or group of programming instructions) that can be reused anytime it is needed. A procedure may have parameters and return values.

procedure

A named collection of steps in an algorithm that can be reused anytime it is needed without restating the detailed procedures (abstraction)

Algorithm

A process or set of rules to be followed in calculations or other problems solving operations

In which of the following programs would the use of heuristics be LEAST likely to be recommended?

A program that calculates the total bill for all the items in a customer's online shopping cart including any taxes

Flowchart

A simple diagram with symbols showing the "flow" of a process flow pattern: pattern that can emerge when data is transformed using computational logic structures.

A team of students is programming a robot to find its way out of a maze (exact layout unknown) for a national competition. The robot will be timed, and teams with the best time will move on to the next level of the competition. What strategy should the team take?

A team of students is programming a robot to find its way out of a maze (exact layout unknown) for a national competition. The robot will be timed, and teams with the best time will move on to the next level of the competition. What strategy should the team take?

Parameter

A variable that defines a procedure or sets the conditions of an operation

Consider the following two algorithms for determining the tallest person in a group. Algorithm A Step 1: Everybody stands and lines up against the wall. Step 2: One person from those who are standing steps forward. Step 3: One person from those who are standing steps forward. Step 4: The shorter of these two people who stepped forward sits down. Step 5: Repeat steps 3 and 4 until only one person is standing. Step 6: The last person standing is the tallest in the group. Algorithm B Step 1: Everybody stands and lines up against the wall. Step 2: Each standing person pairs up with one other standing person. Step 3: The shorter member of each pair sits down. Step 4: Repeat steps 2 and 3 until only one person is standing. Step 5: The last person standing is the tallest in the group.

Algorithm A is never faster than Algorithm B

Selection

Algorithmic structure that uses "if...then" to tell the computer how to select a step or to tell the sequence that should be executed.

Which of the following illustrates an aspect of efficiency?

All of these

Which is NOT true about comments in programs?

All published programs have their comments available.

Comments

An annotation in the code of a computer program

Pseudocode

An informal method of writing algorithmic instructions that do not necessarily follow grammatical rules and syntax of a particular language. "False" code.

Boolean values

Binary values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra.

In a flowchart, a typical decision symbol has what type of output?

Boolean (true/false)

The program below calculates and displays the sum of the integers from m to n inclusive, where m < n. Line 1: i ← m Line 2: r ← n-m+1 Line 3: sum ← 0 Line 4: REPEAT r TIMES Line 5: { Line 6: sum ← sum+i Line 7: i ← i+1 Line 8: } Line 9: DISPLAY sum If Line 1 is changed to read i ← n, which of the following changes will ensure that the program continues to calculate and displays the sum of the integers from m to n inclusive

Change line 7 to read i ← i-1

Consider the code segment below. Line 1: IF (b ≠ 0) Line 2: { Line 3: a ← a/b Line 4: } Line 5: ELSE Line 6: { Line 7: a ← a*b Line 8: }

Changing line 7 to a ← 0

A team of students is collaborating on a program to obtain local weather data from a website, and predict weather-related school closings based on their own formulas. They must present their code to a group of faculty, some of whom have little to no experience with code. What strategies can the group use while writing their code, in order to make it more understandable for the faculty? Select two answers.

Choose meaningful names for all variables and procedures. Include comments detailing the purpose and behavior of variables and procedures.

Computers may be programmed to play logic games such as chess and go. These games have an incredibly large number of possible move sequences which may be made. Which of the following describes a good strategy and accompanying rationale for creating an algorithm which a computer can use to play these games?

Develop an algorithm which uses heuristics because this will be able to provide reasonable solutions, and evaluating every possible move will take an impractical amount of time.

Moore's Law

Developed by Gordon Moore, who accurately predicted that the number of transistors that could fit on a chip would roughly double every one to two years. This law has helped innovators predict and develop technology efficiently for the past 50 years.

The local government for a newly developed community would like to begin conducting an annual census to make an accurate count of its current population and gather other relevant demographic information about its residents. The community is currently relatively small and consists of around 500 homes. However, it is growing quickly and expects to have more than 100,000 homes in the next few years. Which of the following plans would be the most practical and most efficient solution for computing an accurate population count, no matter how large the community grows?

Every year, mail a copy of the census form to every home in the community and require that each household complete the form themselves and mail it back to a central office for processing. Then follow up with those that did not.

Definite Loop

Executes a predetermined set of times for a loop to be repeated

Scalability

How well do algorithms perform at increasingly larger scales. Big-O Notation: a mathematical concept used by computer scientists to determine how well algorithms scale - performances classified into different categories.

Which of the following are likely to be important considerations when writing a program to be capable of processing large data sets? I. Reducing the memory usage of the program II. Reducing the number of statements in the program III. Reducing the CPU time required to run the program

I and III only

Which of the following would be the most appropriate name for the following procedure (currently named MYSTERY), where number is any number and the procedure CHECK (number) returns true if the number is an integer and false if not? PROCEDURE MYSTERY (number) { IF (number > 0 AND CHECK (number)) { RETURN (true) } RETURN (false) }

IS_POS_INT

Which of the following best describes the result of running the program below? number ← RANDOM (1, 6) REPEAT UNTIL number > 100 { number ← number * 2 } I F (number = 128) { DISPLAY (true) } ELSE { DISPLAY (false) }

If number is a 1, 2 or 4, then the program will display true.

Branching

Instruction in a computer program that can cause a computer to begin executing a different sequence of instructions.

Operator Block

Light-green colored blocks of code used to handle strings and math equations in Scratch.

Consider a robot that is initially facing north in a 5-by-5 grid, as shown in the diagram below. A program segment has been written to move the robot to the upper-right corner where it should face west. The following program segment is intended to move a robot from the starting position to the ending position as shown above. However, the program segment does not work correctly. Line 1: MOVE_FORWARD () Line 2: MOVE_FORWARD () Line 3: IF(CAN_MOVE (right)) Line 4: { Line 5: MOVE_FORWARD () Line 6: MOVE_FORWARD () Line 7: ROTATE_RIGHT () Line 8: } Line 9: ROTATE_LEFT () Line 10: ROTATE_LEFT ()

Line 7 and Line 5

Sequencing

Logic structure where instructions are executed in order, one after another, one at a time. Often called linear.

What is displayed as a result of running the following program? a ← 6 b ← 11 IF (a = b) { DISPLAY ("January") } ELSE { IF (a > b) { DISPLAY ("February") } ELSE { IF (a > 0) { DISPLAY ("March") } ELSE { DISPLAY ("April") } } }

March

A mathematician is working with a programmer to write a program to solve a problem using high level mathematics. The mathematician asks the programmer to help her determine the efficiency of the algorithm. How can the efficiency be determined?

Multiple inputs of various sizes and with different expected outputs can be tested to determine the behavior of algorithm.

Repetition

One complete step of a loop, repeated until a certain condition is met

A standard deck of playing cards contains 52 cards, with each card being one of four suits: SPADES, HEARTS, CLUBS, and DIAMONDS. Each suit is represented by 13 cards whose values include ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, and KING. Consider the following two algorithms that are intended to find the card representing the "TEN of HEARTS" in a randomly shuffled deck and separate it from the other 51 cards in the deck. Algorithm A Step 1: Pick up the shuffled deck of cards. Step 2: Look at the topmost card in your hands. Step 3: If the card's suit is a HEART, place the card on top of the pile to your left. Step 4: Otherwise, place the card on top of the pile to your right. Step 5: Repeat steps 2 through 4 until you are no longer holding any cards. Step 6: Pick up the cards in the pile to your left. Step 7: Look at the topmost card in your hands. Step 8: If the card's value is a TEN, place all other cards in your hands on top of the pile to your right. Step 9: Otherwise, place the card on top of the pile to your right. Step 10: Repeat steps 7 through 9 until you are holding only one card. Step 11: The only card in your hand is the TEN of HEARTS. Algorithm B Step 1: Pick up the shuffled deck of cards. Step 2: Look at the topmost card in your hands. Step 3: If the card's suit is a HEART or the card's value is a TEN, place all other cards in your hands on top of the pile to your right. Step 4: Otherwise, place the card on top of the pile to your right. Step 5: Repeat steps 2 through 4 until you are holding only one card. Step 6: The only card in your hand is the TEN of HEARTS.

Only Algorithm A will always work as intended.

Conditionals

Only executes if a certain designated condition is true.

Elsa and her friend Olaf are co-owners of the world's largest ice cream truck, offering their customers the choice of 256 different flavors of ice cream. The truck, which has been built from an 18-wheeled tractor-trailer, houses the 256 tubs of ice cream in the refrigerated trailer. The side of the truck displays a large list of the 256 names of the different flavors, which the customers then name when they place their orders. Business is booming and the duo needs to serve their customers quickly. However, in order to quickly find the right tub of ice cream out of such a large collection, Elsa and Olaf both agree that they need to use an efficient solution. Elsa suggests that they organize the flavors alphabetically while Olaf suggests that they organize the flavors in order from his favorite to his least favorite. Which of the following solutions would allow these ice cream peddlers to find the customers' chosen flavors the most efficiently?

Organize the tubs alphabetically by flavor name and use a binary search algorithm to find the customer's requested flavor.

Sequential Execution

Program instructions that are executed one at a time, in order.

Which of the following best describes the type of language used in the below algorithm?

Pseudocode

A study of beech trees shows that around 40% of these trees may be susceptible to a newly discovered disease. The computer program below is intended to determine the number of trees which may be susceptible to the disease in a forest containing k beech trees: Line 1: total ← 1 Line 2: repeat k times Line 3: { Line 4: IF () Line 5: { Line 6: total ← total + 1 Line 7: } Line 8: } Line 9: DISPLAY (total) Which of the following could be used to replace <MISSING CONDITION> to ensure the program works as intended? Select two answers.

RANDOM (1, 5) ≤ 2

Code Segment

Refers to a collection of program statements that are part of a program.

Iteration

Repetition - one complete step of a loop, repeated until a certain condition is met

Consider the given code: 1 integer ← 22 2 lowtemp ← 0 3 4 IF(integer < 10) 5 { 6 lowtemp ← lowtemp - 5 7 } 8 ELSE 9 { 10 lowtemp ← lowtemp + 10 11 } 12 13 DISPLAY(lowtemp) What is the best description of the structure implied by line 4?

Selection

Which of the following describes a search algorithm which would be very inefficient for finding values in the middle of a large ordered data set?

Sequential

Operators

Symbols that imply a comparison in conditional selection statements.

Consider the following code: Which of the following is the output of the code? n ← 4 x ← -2.5 IF(n < 3) { DISPLAY("Team A") } ELSE { IF(n < 2) { DISPLAY("Team B") } ELSE { IF(x > -2) { DISPLAY("Team C") } ELSE { DISPLAY("Team D") } } }

Team D

What must you consider when determining the efficiency of an algorithm? Select two choices.

The amount of resources, such as storage, required The length of time required to run the program

Which of the following is a key factor in determining the efficiency of an algorithm?

The amount of storage required by the algorithm

Which of the following will cause an algorithm to fall into an infinite loop?

The condition of the loop never becomes false

Control Flow

The direction the computer program moves from instruction to instruction over time. Can also be controlled by if statements and other binary conditions.

Consider the following code: IF (n > 0) { DISPLAY ("The number is positive.") } ELSE { DISPLAY ("The number is not positive.") }

The number is not positive.

Which of the following best describes an effect of Moore's Law?

The past record of increasing computing power has driven more investment in innovation

A program has been developed to compute the sum of all elements with a value greater than 10 in a list of integers. Which of the following statements best describes how iteration may be used to in the program in this example?

The program will repeat the execution of a block of commands which test the value of an item and add it to a running total (if it is greater than 10) for each value in the list.

Loop

The repetition of some code

The following question uses a robot in a grid of squares. Which of the following will cause the robot code to stop executing?

The robot attempts to move off its grid.

Modularity

The subdivision of a computer program into separate subprograms.

Halting Problem

There cannot be a program that will determine which computer programs will halt (or exit) and which programs will go on forever (infinite loop) undecidable problem: where no algorithm can be made that always leads to a correct yes or no answer.

Remix

To modify and share a version of an uploaded existing project

Brute Forcing

Trial and error method used to decode encrypted data such as passwords.

Switching

Turning on or off (binary)

If statement

Type of selection statement that only executes when a certain criteria is met.

A mathematics professor at Howard University wants his students to write a program to solve complex mathematical problems. The professor asks a programmer to determine the efficiency of each student's algorithm. How can the programmer determine the efficiency of an algorithm?

Use mathematical calculations to determine how the number of computations performed by the program will increase as the size of the inputs increase. Run the program with many different inputs to determine how much CPU time will be required for algorithm to solve the problem for inputs of different sizes.

Indefinite loop

When it is unknown how many times a loop will iterate, usually we are waiting for an event to occur such as "repeat until...."

Under which of the following conditions is it most beneficial to use a heuristic approach to solve a problem?

When the problem cannot be accurately solved in a reasonable time and an approximate solution is acceptable.

Nesting

Where different logic structures sequence, selection and loops are combined or nested in one another.

What should replace <missing statement> in the following code be if we want this code to repeat until the variable a is greater than 10? a ← 0 REPEAT UNTIL (<missing statement>) { a ← a + 1 }

a ) 10

Logarithmic Behavior

doubling the size of a problem only requires one extra unit of work.

The algorithm below is used to simulate the results of rolling a standard 6-sided die 5 times. Consider the goal of determining whether the simulation resulted in more results which were odd than results which were even. Step 1:Initialize the variables odd_counter and roll_counter to 0. Step 2:A variable dice_roll is randomly assigned an integer value between 1 and 6 inclusive. If dice_roll has a value of 1, 3 or 5, then odd_counter is incremented by 1. Step 3:Increment the value of roll_counter by 1. Step 4:Repeat steps 2 and 3 until roll_counter equals 5. Following the execution of the algorithm, which of the following expressions indicates that the simulation resulted in more results which were odd than results which were even?

odd_counter>2

Which of the following is the best example of a heuristic approach to solving a problem?

sing your thumb and forefinger in order to add a pinch of salt to a meal because the recipe that your grandma has written on an index card says, "Add some salt".

The following procedure, validate (x), is intended to display "PASS" if an integer, x, is within the range of 128 through 32768, inclusive. Values of x that are not within the range should display "FAIL". Which of the following sets of test cases would be the best choice to fully test this procedure and provide evidence that it executes as intended for all values of x?

validate(127) validate(128) validate(2048) validate(32768) validate(32769


Conjuntos de estudio relacionados

OpenStax US History Ch. 7 Review Questions & Terms

View Set

Introduction to Biblical Literature Exam 1

View Set

ACIS 3115 - Concept Check Questions

View Set

Ch.9 Gross Anatomy of Skeletal Muscles

View Set

Unidad 3.35 Averiguar la información (el nombre, etc.) ¿Cuál es? ¿Cuáles son?

View Set

LearningCurve 6a: What is Memory?

View Set

Real Estate University | S1 | Chapter 7

View Set

JS-US007-14 - Level I Antiterrorism Awareness Training

View Set