← Quiz: Testing AI Code: all 10 questions
Quiz: Testing AI Code · Question 8 of 10
What does this print? (code)
What does this print?
def max_subarray(nums):
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
print(max_subarray([-3, -8, -1]))
- A.0
- B.-12
- C.-3
- D.-1
Answer & explanation
Correct answer: -1
With all negative numbers, the best subarray is the single largest element.
Can you get the rest right without peeking?
Take the timed Quiz: Testing AI Code quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →