Question 64. Error with multiplication using repeated addition (4C algorithms and programs)

The following procedure is intended to return the value of x times y, where x and y are integers. Multiplication is implemented using repeated additions.

For which of the following procedure calls does the procedure NOT return the intended value?
Select Two Answers

Answer Choices

A. Multiply (2,5)
B. Multiply (2,-5)
C. Multiply (-2,5)
D. Multiply (-2,-5)

Correct Answer (B and D):

  • The reason answers B and D are correct is because the Y value is initially negative and if count = 0, the loop never happens because 0 is always greater than a negative number so 0 is returned.

Incorrect Answer (A and C):

  • Answer A is incorrect because the procedure repeatedly adds 2 to result 5 times, which results in 10.
  • Answer C is incorrect because the procedure repeatedly adds -2 to the result 5 times, which results in -10
# flawed multiply function
# as a popcorn hack (coding challenge), fix the multiply function to work with negative numbers

'''
As you can see, the function fails when y is negative,
because the while loop condition count < y is never true in these cases.
'''

def multiply(x, y):
    count = 0
    result = 0
    while count < y:
        result += x
        count += 1
    return result

# Test cases
print(multiply(2, 5))  # Expected output: 10
print(multiply(2, -5))  # Expected output: -10, Actual output: 0
print(multiply(-2, 5))  # Expected output: -10
print(multiply(-2, -5))  # Expected output: 10, Actual output: 0
10
0
-10
0

65. Call to concat and substring (4B string operations)

A program contains the following procedures for string manipulation. Which of the following can be used to store the string “jackalope” in the string variable animal ?

Select two answers.

Answer Choices

A.
animal ←← Substring (“antelope”, 5, 4)
animal ←← Concat (animal, “a”)
animal ←← Concat (Substring (“jackrabbit”, 1, 4), animal)

B.
animal ←← Substring (“antelope”, 5, 4)
animal ←← Concat (“a”, animal)
animal ←← Concat (Substring (“jackrabbit”, 1, 4), animal)

C.
animal ←← Substring (“jackrabbit”, 1, 4)
animal ←← Concat (animal, “a”)
animal ←← Concat (animal, Substring (“antelope”, 5, 4))

D.
animal ←← Substring (“jackrabbit”, 1, 4)
animal ←← Concat (animal, “a”)
animal ←← Concat (Substring (“antelope”, 5, 4), animal)

Correct Answers (B and C):

  • Answer B stores “lope” into animal, and then concatonates or adds “a” to “lope” which stores into animal as “alope”. Finally, it concatonates substring “jack” with “alope” to store “jackalope” in animal.
  • Answer C first stores “jack” into animal and then concatonates “jack” and “a” to store “jacka” in animal. Finally it concatonates “jacka” and “lope” to store jackalope.

    Incorrect Answers (A and D):

  • Answer A first stores “lope” then concatonates “lope” and “a” to store “lopea”. Lastly, it concatonates “jack” with “lopea” to store “jacklopea”
  • Answer Dstores “jack” into animal and concatonates “jack” and “a” to store “jacka” in animal. The last step is where the error is because it concatonates “lope” and “jacka” to store “lopejacka”
# Incorrect Answer D
# as a popcorn hack (binary challenge), create string and concatenation options for A, B, C
 
animal = "jackrabbit"[0:4]  # Substring("jackrabbit", 1, 4)
animal += "a"  # Concat(animal, "a")
animal = "antelope"[4:8] + animal  # Concat(Substring("antelope", 5, 4), animal)
print(animal)  # Outputs: lopejacka
lopejacka