Skip to main content

Exercises 2.12 Exercises

At the end of each chapter, we include exercises you can do with the things youโ€™ve learned. You canโ€™t learn to program only by reading about it; you have to practice.

1.

Take a look at the following program. How many lines of output will be produced?
int main() {
  cout << "The current time is: " << endl;
  cout << 7;
  cout << ":" << 50;
  cout << endl;
  cout << "I'm going to be late for my 8am!";
}
  • 5
  • There are 5 cout statements, but that doesnโ€™t mean there are 5 lines of output!
  • 4
  • Remember that endl is what makes a new line!
  • 3
  • Even though there are 5 cout, there are only 3 lines of output in the terminal.
  • 2
  • There are 2 endl statements. But what happens when you have more output after the endl?
  • 0 (There is an error!)
  • No errors here.

2.

Construct a block of code that prints: โ€œLions &โ€ one the first line, โ€œTigers & Bears!โ€ on the second line, and โ€œOh my!โ€ on the FOURTH line.

3.

4.

Complete the code so that it prints Hello! on one line and then I love programming! on the next.
You have attempted of activities on this page.