Trimester Final 1 Blog



Q36 Store even numbers in evenList

Consider the following code segment, which is intended to store ten consecutive even integers, beginning with 2, in the list evenList. Assume that evenList is initially empty.

i ← 1

REPEAT 10 TIMES

{

< MISSING CODE >

}

Which of the following can be used to replace < MISSING CODE > so that the code segment works as intended?

My Answer:

i ← i + 1

APPEND(evenList, 2 * i)

Correct Answer:

APPEND(evenList, 2 * i)

i ← i + 1

For the first iteration of the loop, twice the value of i, or 2, is appended to evenList, and then i is incremented to 2. For the second iteration of the loop, twice the value of i, or 4, is appended to the list, and then i is incremented to 3. This continues eight more times, appending the next eight even numbers to evenList. This code segment will generate the list [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].



Q39 Valid index for wordList

The list wordList contains a list of 10 string values. Which of the following is a valid index for the list? (4, 2.5, -2, “hello”)

My Answer: “hello”

Correct Answer: 4

While the list elements are strings, the indices of a list are typically nonnegative integers.



Q42 Value of sum after IF ELSE IF

What is the value of sum after the code segment is executed?

My Answer: 12

Correct Answer: 16

The first three statements assign values to the variables. Since num1 < num2 evaluates to false, the body of the ELSE block is executed and num3 is assigned the value 4. Since num2 ≥ num3 evaluates to true, the body of the second IF block is executed and num1 is assigned the value 8. Lastly, sum is assigned the value of 8 + 4 + 4, or 16.



Q43 Value of x after REPEAT UNTIL block

What is the value of result after the code segment is executed?

My Answer: 6

Correct Answer: 15

The variables x and result are initialized to 0. Inside the loop, result is increased by x and x is increased by 1. The loop terminates when x exceeds 5. Therefore, result is assigned the sum of the integers from 0 to 5, or 15.



Q48 Which variables are equal to 50 after assignments

Consider the following code segment.

x ← 25

y ← 50

z ← 75

x ← y

y ← z

z ← x

Which of the variables have the value 50 after executing the code segment?

My Answer: z y and z

Correct Answer: x and z

The first three statements assign values to the variables. The fourth statement assigns the value of y (which is 50) to x. The fifth statement assigns the value of z (which is 75) to y. The sixth statement assigns the value of x (which is 50) to z. Therefore, x and z both have the value 50.



Q50 Use concat and prefix to form initials

Consider the following procedures for string manipulation.

Procedure Call Explanation
concat(str1, str2) Returns a single string consisting of str1 followed by str2. For example, concat("key", "board") returns "keyboard".
prefix(str, length) Returns the first length characters of str or str if length is greater than the number of characters in str. For example, prefix("delivery", 3) returns "del" and prefix("delivery", 100) returns "delivery".

The variable initials is to be assigned a string consisting of the first letter of the string firstName followed by the first letter of the string lastName. Which of the following assigns the correct string to initials ?

My Answer:

initials ← prefix(concat(firstName, lastName), 2)

Correct Answer:

initials ← concat(prefix(firstName, 1), prefix(lastName, 1))

This statement will correctly form the initials. It uses calls to prefix to obtain the first letters of each name, then uses a call to concat to concatenate the two letters in the correct order.