Midterm Trivia

Mar 3, 2024  β”‚  m. Mar 5, 2024 by Charlotte Curtis
  1. If I assign 5 / 2 to a double, what value will be stored in the variable?

    Answer 2.0
  2. C-strings are arrays of characters, with one special difference. What is it?

    Answer C-strings are null-terminated, meaning they have a null character ('\0') at the end.
  3. When an array is passed to a function, what is actually passed?

    Answer A pointer to the first element of the array.
  4. Why might you pass something as a const reference instead of by value?

    Answer To avoid making a copy of the object, which can be expensive for large objects.
  5. Draw a memory diagram for the following code:

    int x = 5;
    int *p1;
    int *p2 = &x;
    
    Answer Memory diagram
  6. When extracting (using >>) an integer from an input stream, what happens to whitespace before and after the integer? Assume the integer is valid.

    Answer Whitespace is consumed and skipped before the integer, but left behind in the buffer after.
  7. When does the eof flag get triggered?

    Answer When an attempt is made to read past the end of the file.
  8. Name one advantage of separate compilation.

    Answer Possible answers:
    • Only compile the files that have changed (faster compilation)
    • Re-use libraries of code
    • Hide implementation details and keep the main logic clean
  9. Trace the following code, assuming the user inputs 3 5 0 2. What will be stored in nums at the end of this code snippet?

    int nums[8], n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }
    
    Answer [5, 0, 2, ?, ?, ?, ?, ?]
  10. In the previous question, what would happen if you added cout << nums[-1]?

    Answer Undefined behaviour - might crash, might print random garbage, might launch missiles.


Previous: Fixing Git Labs
Next: Another ADT: Queues