Home > Articles > Programming > C/C++

  • Print
  • + Share This
  • 💬 Discuss

This chapter is from the book

Self-Review Exercises

2.1

Fill in the blanks in each of the following.

  1. Every C++ program begins execution at the function _______.
  2. A(n) ________ begins the body of every function and a(n) ________ ends the body.
  3. Every C++ statement ends with a(n) ________.
  4. The escape sequence \n represents the _______ character, which causes the cursor to position to the beginning of the next line on the screen.
  5. The _______ statement is used to make decisions.

2.2

State whether each of the following is true or false. If false, explain why. Assume the statement using std::cout; is used.

  1. Comments cause the computer to print the text after the // on the screen when the program is executed.
  2. The escape sequence \n, when output with cout and the stream insertion operator, causes the cursor to position to the beginning of the next line on the screen.
  3. All variables must be declared before they're used.
  4. All variables must be given a type when they're declared.
  5. C++ considers the variables number and NuMbEr to be identical.
  6. Declarations can appear almost anywhere in the body of a C++ function.
  7. The modulus operator (%) can be used only with integer operands.
  8. The arithmetic operators *, /, %, + and - all have the same level of precedence.
  9. A C++ program that prints three lines of output must contain three statements using cout and the stream insertion operator.

2.3

Write a single C++ statement to accomplish each of the following (assume that using directives have not been used):

  1. Declare the variables c, thisIsAVariable, q76354 and number to be of type int.
  2. Prompt the user to enter an integer. End your prompting message with a colon (:) followed by a space and leave the cursor positioned after the space.
  3. Read an integer from the user at the keyboard and store it in integer variable age.
  4. If the variable number is not equal to 7, print "The variable number is not equal to 7".
  5. Print the message "This is a C++ program" on one line.
  6. Print the message "This is a C++ program" on two lines. End the first line with C++.
  7. Print the message "This is a C++ program" with each word on a separate line.
  8. Print the message "This is a C++ program". Separate each word from the next by a tab.

2.4

Write a statement (or comment) to accomplish each of the following (assume that using directives have been used for cin, cout and endl):

  1. State that a program calculates the product of three integers.
  2. Declare the variables x, y, z and result to be of type int (in separate statements).
  3. Prompt the user to enter three integers.
  4. Read three integers from the keyboard and store them in the variables x, y and z.
  5. Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.
  6. Print "The product is " followed by the value of the variable result.
  7. Return a value from main indicating that the program terminated successfully.

2.5

Using the statements you wrote in Exercise 2.4, write a complete program that calculates and displays the product of three integers. Add comments to the code where appropriate. [Note: You'll need to write the necessary using directives.]

2.6

Identify and correct the errors in each of the following statements (assume that the statement using std::cout; is used):

  1. if ( c < 7 );
        cout << "c is less than 7\n";
    
  2. if ( c => 7 )
        cout << "c is equal to or greater than 7\n";
    
  • + Share This
  • 🔖 Save To Your Account
C++ How to Program, 8th Edition

This chapter is from the book

C++ How to Program, 8th Edition

Discussions

comments powered by Disqus