Companion module: the C++ Foundations module is the complete, enriched version of this unit’s material — study these pages together for extra depth, practice sets, and the variation lab.</div>

Unit 01 — Exercises

Attempt everything before checking any answer · Unit index · Answers for the predict items are in §4; coding tasks check themselves by compiling.

1. Concept checks (answer in one written sentence)

  1. In your own words: what is a program?
  2. What does a compiler do, and what file does it produce?
  3. Name the three steps of the edit–compile–run cycle and say what you do when a step fails.
  4. What is the difference between hello.cpp and hello (the executable)?
  5. Why does this course compile with -Wall -Wextra — what is a warning?

2. Predict the output

Write each answer down before running anything. (You may run afterwards to check — that’s the point of the answer table.)

6.

std::cout << "One";
std::cout << "Two\n";
std::cout << "Three\n";

7.

std::cout << "A\tB\nC\tD\n";

8.

std::cout << "She said \"C++ is fun\"\n";
  1. How many output lines does this produce?
std::cout << "x\n\ny\n";
  1. This program has one bug. Which line, and what is it?
#include <iostream>

int main() {
    std::cout << "Ready\n"
    return 0;
}

3. Short coding tasks

Compile each with g++ -std=c++17 -Wall -Wextra task.cpp -o task and check the output yourself against the requirement.

T1 — Timetable. Print a two-column mini timetable using \t, with a title line and one line per day, e.g. Mon\tAlgorithms. At least four days.

T2 — Quote me. Print exactly this line (quote characters included):

My teacher said: "Practice daily."

T3 — Card upgrade. Take the Student Card example from Lesson 2 and add a fourth line with your city. Keep the ==== borders the same width as the title.

T4 — The missing semicolons. The program below produces three compiler errors when compiled. Predict what the first error will be and on which line; then compile, fix only the first error, recompile, and repeat until clean. (Write in your bug diary how many rounds it took.)

#include <iostream>

int main() {
    std::cout << "One\n"
    std::cout << "Two\n"
    std::cout << "Three\n"
    return 0;
}

4. Answers to predict-the-output

Reveal answers (attempt everything first!)

A6.

OneTwo
Three

The first statement prints One with no newline, so Two continues on the same line; \n after Two moves the next output down.

A7.

A       B
C       D

\t jumps to the next tab stop — the gap’s exact width depends on your terminal, the columns are what matters.

A8.

She said "C++ is fun"

\" prints one literal quote.

A9. Three lines: x, then an empty line (the extra \n), then y.

A10. Line 4: the statement std::cout << "Ready\n" is missing its closing ;. (Depending on your compiler you may see expected ';' before 'return' — notice the reported line may be 6 while the mistake is on 4. Read upward.)


← Unit index · Next: Lab 01