Embercode
✕
ArraysQuick quiz

Bounds debugging

6 questions. Covers Out-of-bounds debugging lab.

Questions 1 to 5 of 6

  1. 1. The loop below should add up the five scores. Which change fixes the out-of-bounds read? ``cpp vector<int> v = {70, 85, 90, 60, 75}; int total = 0; for (size_t i = 0; i <= v.size(); ++i) { total += v[i]; } ``
  2. 2. What goes wrong in this loop when v is empty? ``cpp vector<int> v; int total = 0; for (size_t i = 0; i < v.size() - 1; ++i) { total += v[i]; } ``
  3. 3. The smoothing loop below fails on its first pass. Which read goes outside the array? ``cpp vector<int> readings = {10, 20, 30, 40, 50}; for (int i = 0; i < static_cast<int>(readings.size()); ++i) { int left = readings[i - 1]; int right = readings[i + 1]; // ... } ``
  4. 4. A program reads scores.data()[i] for i from 0 up to i <= scores.size() and prints total 380 with exit code 0. A classmate says the program is correct because the total is right. Which answer is correct?
  5. 5. What does this program print? ``cpp #include <cstddef> #include <iostream> #include <vector> using namespace std; int main() { vector<int> temps = {18, 21, 19, 24}; size_t day = 0; cout << boolalpha << (day - 1 < temps.size()) << "\n"; } ``

5 question(s) left on this page.