Q1 Swap values first and second

3.1

The code segment below is intended to swap the values of the variables first and second using a temporary variable, temp.

The block code consists of 3 lines. Line 1: temp, left arrow, first Line 2: first, left arrow, second Line 3: MISSING CODE

Which of the following can be used to replace missing code so that the code segment works as intended?

Answer B

This option is incorrect. The code segment assigns the initial value of first to temp, then assigns the initial value of second to first. The initial value of second, which has been stored in first, is then assigned to second. Therefore, the value of second has not changed from its initial value.

Q11 Simulate spinner with three unequal sections

3.15

A spinner is divided into three sections. The sector labeled Red is four times as large as each of the sectors labeled Blue and Yellow, which are of equal size.

The figure shows a spinner divided into 3 sections. The sector labeled Red is 4 times as large as each of the sectors labeled Blue and Yellow, which are of equal size.

The procedure below is intended to simulate the behavior of the spinner.

The block code consists of 2 lines. Begin block Line 1: PROCEDURE Spinner Line 2, indented 1 tab: begin block, MISSING CODE, end block End block

Which of the following can be used to replace missing code so that the procedure correctly simulates the spinner?

Answer D

This option is incorrect. The code segment simulates a spinner in which selecting each of three sections is equally likely.

Q21 Comparing robot algorithms

3.12

The following question uses a robot in a grid of squares. The robot is represented by a triangle, which is initially facing right.

The figure shows a grid of squares with 4 columns and 8 rows. The square in the first row and third column is gray, and all other squares are white. The square in the fourth row and first column contains a right-facing triangle, representing a robot. A path of arrows shows the robot’s movement from its initial location to the gray square. The path shows the robot moving 2 squares to the right from its initial location, then 3 squares up to the gray square.

Consider the procedures below.

The block code consists of 3 lines. Begin block Line 1: PROCEDURE, Move X Times, 1 word with capital M, X, and T, begin block, x, end block Begin block Line 2, indented 1 tab: REPEAT x TIMES Begin block Line 3, indented 2 tabs: MOVE underscore FORWARD End Block End block End block The block code consists of 3 lines. Begin block Line 1: PROCEDURE, Right X Times, 1 word with capital R, X, and T, begin block, x, end block Begin block Line 2, indented 1 tab: REPEAT x TIMES Begin block Line 3, indented 2 tabs: ROTATE underscore RIGHT End block End block End block

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

Answer B

This option is correct. This code segment moves the robot forward two squares, rotates it right three times so that the robot faces the top of the grid, and then moves the robot forward three squares to the gray square.

Developing Procedures

  • One common type of abstraction is procedural abstraction, which providdes a name for a process and allows a procedure to be used only knowing what it does, not how it does it
  • Procedural abstraction allows a solution to a large problem to be based on the solutions of smaller subproblems. This is accomblished by creating procedures to solve each of the subproblems
  • The subdivision of a computer program into seperate subprograms is caled modularity
  • A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program reuse, which helps manage complexity

Calling Procedures

  • A proceudre is a named group of programing insturctions that may have parameters and return values
  • Proceudres are reffered to by different names, such as method or function, depending on the programing language
  • Parameters are input values fo a procedure. Arguments specify the values of the parametes when a proceudre is called
  • A proceudre call interuppts the sequential execution of statements, causing the program to execute the statement in the procedure (or a return statement) has executed, flow of control is returned to the point immediatly following where the proceudre was called