# A Quick Look at C++

👁 88
statistics

A simple and basic C++ code example. Explore your logical understanding of programming wither you are a beginner or it is your first C++ encounter.

Questions #: 18
Time: 15 minutes
Pass Score: 80.0%
Style
Mode

1  //**************************************************************2  // Given the length and width of a rectangle, this C++ program3  // computes and outputs the perimeter and area of the rectangle.4  //**************************************************************5 6  #include <iostream> 7  using namespace std; 89  int main()10 {11   double length; 12   double width; 13   double area; 14   double perimeter;15  16   cout << "Program to compute and output the perimeter and " << "area of a rectangle." << endl;1719   length = 6.0;20   width = 4.0;21   perimeter = 2 * (length + width); 22   area = length * width;23   cout << "Length = " << length << endl; 24   cout << "Width = " << width << endl;25   cout << "Perimeter = " << perimeter << endl; 26   cout << "Area = " << area << endl;27   return 0;28 }

POINTS (1)

#### Sample Run: (When you compile and execute this program, the following five lines are displayed on the screen)

Program to compute and output the perimeter and area of a rectangle.Length = 6Width = 4Perimeter = 20Area = 24
POINTS (1)

#### The output of the program is generated by executing the following statements

cout << "Program to compute and output the perimeter and " <<  "area of a rectangle." << endl;cout << "Length = " << length << endl;cout << "Width = " << width << endl;cout << "Perimeter = " << perimeter << endl;cout << "Area = " << area << endl;
POINTS (1)

#### Explain how this happens

cout << "Program to compute and output the perimeter and " <<  "area of a rectangle."  << endl;

This is an example of a C++ (1) statement. It causes the computer to evaluate the (2) after the pair of symbols << and display the (3) on the screen

drag and drop the selected option to the right place or type it instead
output
result
expression
POINTS (1)

#### Arithmetic expression or a string?

length + width

is an example of

POINTS (1)

POINTS (1)

POINTS (1)

#### cout << "Length = " << length << endl;

What does the second length evaluate to?

(write a whole number without decimal points)

Missing
POINTS (1)

POINTS (1)

#### return 0;

returns zero to the program itself which continues running

POINTS (1)

#### Main function

A C++ program can execute without a main function

POINTS (1)

POINTS (1)

#### #include <iostream>

#include <iostream>

allows us to use the (predefined object) cout to generate output and the (manipulator) endl.

POINTS (1)

#### using namespace std;

using namespace std;

This statement is useless.  You can remove it and the program still compiles.

POINTS (1)

#### int main()

int main()

This is the (1) of the function main. The next line consists of a (2) . This marks the beginning of the (body) of the function main. The (3) (at the last line of the program) matches this (4) and marks the end of the body of the function main.

drag and drop the selected option to the right place or type it instead
right brace
left brace
POINTS (1)

POINTS (1)

#### Various parts of a C++ program

Match certain program sections

• (1) lines from 1 to 4
• (2) lines from 11 to 14
• (3) lines from 19 to 22
• (4) lines from 23 to 26
drag and drop the selected option to the right place or type it instead
Assignment statements
Variable declarations
Output statements
POINTS (1)

Missing
POINTS (1)