1.x — Chapter 1 summary and quiz

📘 Chapter 1⏱ about 2 min read✍️ Beginner friendly
Read one box at a time. No rush.

Quick review#

  • A statement is one step. Most end with ;.
  • Every program starts at main.
  • std::cout << prints. std::cin >> reads.
  • // starts a comment. The computer ignores it.
  • A variable is a named box that holds a value of one type.
  • Initialize your variables, preferably with { }.
  • Using a variable with no value is undefined behavior.
  • int / int throws away the decimal part.
Quick check

Pick an answer. You find out right away if it is right.

Quick check #1

Which line correctly prints Hi and ends the line?

Quick check #2

What is the value of x after int x {};?

Quick check #3

Which of these is not a valid variable name?

Quiz time
Question #1

What is wrong with this program?

C++
#include <iostream> int main(){    std::cout << "Hello\n"    return 0;}

The first std::cout line is missing a semicolon ; at the end.

Question #2

What does this print?

C++
int x { 10 };x = x + 5;x = x * 2;std::cout << x;

30. First x is 10. Then 10 + 5 = 15. Then 15 × 2 = 30.

Question #3

What does std::cout << 9 / 2; print, and why?

It prints 4. Both numbers are int, so the decimal part (.5) is thrown away. Use 9.0 / 2 to get 4.5.

Question #4

Write a program that asks the user for two whole numbers, then prints their sum and their product.

C++▶ Run
#include <iostream> int main(){    std::cout << "Enter a number: ";    int a {};    std::cin >> a;     std::cout << "Enter another number: ";    int b {};    std::cin >> b;     std::cout << a << " + " << b << " = " << a + b << '\n';    std::cout << a << " * " << b << " = " << a * b << '\n';    return 0;}
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.