www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 68 (continued)]

Self-Review Exercises

2.1

Fill in the blanks in each of the following.

  1. Every C++ program begins execution at the function _________.

  2. The _________ begins the body of every function and the _________ ends the body of every function.

  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 are used.

  4. All variables must be given a type when they are 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 declarations 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 the value entered in integer variable age.


  4. [Page 69]
  5. If the variable number is not equal to 7, print "The variable number is not equal to 7".

  6. Print the message "This is a C++ program" on one line.

  7. Print the message "This is a C++ program" on two lines. End the first line with C++.

  8. Print the message "This is a C++ program" with each word on a separate line.

  9. Print the message "This is a C++ program" with each word separated from the next by a tab.

2.4

Write a statement (or comment) to accomplish each of the following (assume that using declarations have been used):

  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 will need to write the necessary using declarations.]

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";
    


Previous Page
Next Page