2.x — Chapter 2 summary and quiz

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

Quick review#

  • A type decides what a value is and how much memory it uses.
  • int for whole numbers, double for decimals, bool for true/false, char for one character, std::string for text.
  • 'A' is a char. "A" is a string.
  • Numbers can overflow if they get too big for their type.
  • Decimals are not exact. Don't compare them with ==.
  • const and constexpr make values that cannot change.
  • static_cast<type>(value) changes a type on purpose.
Quick check

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

Quick check #1

Which type is best for storing a price like 4.99?

Quick check #2

What's the difference between 'A' and "A"?

Quick check #3

What happens here? const int lives { 3 }; lives = 4;

Quiz time
Question #1

Which type would you use for each?

  1. The number of students in a class
  2. The price of a book
  3. Whether a door is locked
  4. A person's name
  5. A grade from A to F
  1. int
  2. double
  3. bool
  4. std::string
  5. char
Question #2

What is wrong here?

C++
char answer { "y" };

"y" uses double quotes, so it is text (a string), not a char. Use single quotes: char answer { 'y' };

Question #3

This prints 2. How do you make it print 2.5?

C++
int total { 5 };int count { 2 };std::cout << total / count;

Convert one of them to double first:

C++
std::cout << static_cast<double>(total) / count;
Question #4

Write a program that asks for a temperature in Celsius (it may have decimals) and prints it in Fahrenheit. The formula is F = C × 9 / 5 + 32.

C++▶ Run
#include <iostream> int main(){    std::cout << "Temperature in Celsius: ";    double celsius {};    std::cin >> celsius;     double fahrenheit { celsius * 9.0 / 5.0 + 32.0 };    std::cout << celsius << " C is " << fahrenheit << " F\n";    return 0;}

Note the .0 on the numbers so the math uses decimals.

Finished reading? Tick it off. Your progress is saved in this browser.

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