Command Palette

Search for a command to run...

MTH 4300

MTH 4300 Practice Quiz

Quiz format

Every graded quiz in MTH 4300 has the same shape:

  1. Tracing - predict the exact output of a program
  2. Debugging - find and fix the errors in a program
  3. Implementation - write a program from scratch

You get the first 45 minutes of class. Three questions, one of each type.

This one does not count

This practice quiz is not graded. Its only job is to make sure the format holds no surprises the first time it counts.

Question 1 - Tracing

What does this program print? Write the output exactly as it would appear, and track the value of every variable after each statement that changes it.

#include <iostream>

int main() {
    int a = 7;
    int b = 2;
    double c = 7;

    std::cout << a / b << std::endl;
    std::cout << a % b << std::endl;
    std::cout << c / b << std::endl;

    int d = a++;
    int e = ++b;
    std::cout << a << " " << b << " " << d << " " << e << std::endl;

    bool f = (a > b) && (d < e);
    std::cout << f << std::endl;

    return 0;
}

Question 2 - Debugging

This program is supposed to read a count, then read that many prices, and print their average. It has two bugs. Identify each one, explain what goes wrong, and write the corrected program.

#include <iostream>

int main() {
    int count;
    std::cout << "How many items? "
    std::cin >> count;

    double total = 0;
    for (int i = 0; i < count; i++) {
        double price;
        std::cin >> price;
        total += price;
    }

    int average = total / count;
    std::cout << "Average: " << average << std::endl;

    return 0;
}

Question 3 - Implementation

Write a complete C++ program that reads a single non-negative integer from standard input representing a duration in seconds, then prints that duration broken into hours, minutes, and seconds.

For an input of 3671, your program should print:

1h 1m 11s

Use only integer arithmetic. Do not use any function from a library other than <iostream>.