1.
Discuss the purpose of each of the following:
A. cin
B. cout
C. iostream
D. getline
2. Fill in the blanks in each of the following:
A. _____ are used to document a program and improve its readability.
B. The object used to print information on the screen is ______
C. A C++ statement that makes a decision is _____.
D. Most calculations are normally performed by _____ statements.
E. The _____ object inputs values from the keyboard.
F. What arithmetic operations are on the same level of precedence as multiplication?
G. When parentheses are nested, which set of parentheses is evaluated first in an
arithmetic expression? _____
H. A location in the computer’s memory that may contain different values throughout the
execution of a program is called a(n) _____
I. Naming an array, stating its type and specifying the number of elements in the array is
called __________ the array.
J. When accessing an array element, by convention, the first subscript in a two-
dimensional array identifies an element’s _____ and the second subscript identifies an
element’s _____.
K. An m-by-n array contains _____ rows, _____ columns and _____ elements.
3. State whether each of the following is true or false. If false, explain why.
A. Comments cause the computer to print the text after the // on the screen when the
program is executed.
B. Displaying the escape sequence \n causes the cursor to position to the beginning of
the next line on the screen.
C. All variables must be declared before they’re used.
D. All variables must be given a type when they’re declared.
E. C++ considers the variables number and NuMbEr to be identical.
F. Declarations can appear almost anywhere in the body of a C++ function.
G. The remainder operator (%) can be used only with integer operands.
H. The arithmetic operators *, /, %, + and – all have the same level of precedence.
I. All operators are evaluated from left to right.
J. The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales,
his_account_total, a, b, c, z, z2.
K. The statement cout << "a = 5;"; is an assignment statement.
L. A valid arithmetic expression with no parentheses is evaluated from left to right.
M. The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.
N. To refer to a particular array element, we specify the array’s name and element’s
value.
O. An array definition reserves space for an array.
P. To reserve 100 locations for integer array p, you write p[100];
Q. The only way to initialize all the elements of a 15-element array to zero is to use a
loop that assigns zero to every element.
4. Write a single C++ statement to accomplish each of the following:
A. Declare the variables c, thisIsAVariable, q76354 and number to be of type int
(in one statement) and initialize each to 0.
B. 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.
C. Read an integer from the user at the keyboard and store it in int variable age.
D. If the variable number is not equal to 7, print "The variable number is not equal to 7".
E. Print "This is a C++ program" on one line.
F. Print "This is a C++ program" on two lines. End the first line with C++.
G. Print "This is a C++ program" with each word on a separate line.
H. Print "Enter two numbers".
I. Assign the product of variables b and c to variable a.
J. State that a program performs a payroll calculation (i.e., use text that helps to
document a program).
K. Input three integer values from the keyboard into integer variables a, b and c.
5. Identify and correct the errors in each of the following statements
a) b)
if (c < 7); { if (c => 7) {
cout << "c is less than 7\n"; cout << "c is equal to or greater than
} 7\n";
}
6. What, if anything, prints when each of the following statements executes? If nothing prints,
answer “nothing.” Assume x = 2 and y = 3.
A. cout << x;
B. cout << x + x;
C. cout << "x=";
D. cout << "x = " << x;
E. cout << x + y << " = " << y + x;
F. z = x + y;
G. cin >> x >> y;
H. // cout << "x + y = " << x + y;
I. cout << "\n";
7. Write a program that asks the user to enter two integers, obtains the numbers from the
user and prints their sum, product, difference, and quotient.
8. (Comparing Integers) Write a program that asks the user to enter two integers, obtains
the numbers from the user, then prints the larger number followed by the words "is
larger." If the numbers are equal, print the message "These numbers are equal."
9. (Arithmetic, Smallest and Largest) Write a program that inputs three integers from the
keyboard and prints the sum, average, product, smallest and largest of these numbers.
10. (Diameter, Circumference and Area of a Circle) Write a program that reads a circle’s
radius as an integer and prints the circle’s diameter, circumference and area. Use the
constant value 3.14159 for π. Do all calculations in output statements.
11. What does the following code print? cout << "*\n**\n***\n****\n*****\n";
12. Write a program that inputs a five-digit integer, separates the integer into its digits and prints
them with three spaces between each. For example, if the user types in 42339, the program
should print: 4 2 3 3 9
13. (What Does This Program Do?) What does the following program print?
14. (What Does This Program Do?) What does the following program print?
15. Write a program that inputs the size of a square’s side, then prints a hollow square of that size
using asterisks and blanks. Your program should work for squares of all side sizes between 1
and 20. For example, if your program reads a size of 5, it should print:
16. (Calculating a Circle’s Diameter, Circumference and Area) Write a program that inputs
a circle’s radius (as a double value) and computes and prints the diameter, the
circumference and the area. Use the value 3.14159 for π.
17. Write a program that reads three nonzero double values and determines and prints whether
they could represent the sides of a triangle.
18. (Sides of a Right Triangle) Write a program that reads three nonzero integers and determines
and prints whether they’re the sides of a right triangle.
19. Compare and contrast the while and for iteration statements.
20. Discuss a situation in which it would be more appropriate to use a do…while statement
than a while statement. Explain why.
21. Compare and contrast the break and continue statements.
22. (What’s Wrong with This Code?) Find the error(s), if any, in each of the following:
a) case 0:
For (int x{100}, x >= 1, ++x) { cout << "Even integer\n";
cout << x << ' '; case 1:
} cout << "Odd integer\n";
}
b)
The following code should print whether c)
the integer value is odd or even: The following code should output the
switch (value % 2) { odd integers from 19 to 1:
for (int x{19}; x >= 1; x += 2) { int counter{2};
cout << x << ' '; do {
} cout << counter << ' ';
counter += 2;
d) } While (counter < 100);
The following code should output the
even integers from 2 to 100:
23. (Calculating the Product of Odd Integers) Write an application that calculates the product of
the odd integers from 1 to 15.
24. (Triangle-Printing Program) Write an application that displays the following patterns
separately, one below the other. Use loops to generate the patterns.
25. Show the value of x after each of the following statements is performed:
A. x = fabs(7.5); E. x = fabs(-6.4);
B. x = floor(7.5); F. x = ceil(-6.4);
C. x = fabs(0.0); G. x = ceil(-fabs(-8 + floor(-5.5)));
D. x = ceil(0.0); H. x = floor(x + 0.5);
26. (Parking Charges) A parking garage charges a $20.00 minimum fee to park for up to
three hours. The garage charges an additional $5.00 per hour for each hour or part
thereof over three hours. The maximum charge for any given 24-hour period is $50.00.
Assume that no car parks for longer than 24 hours at a time. Write a program that
calculates and prints the parking charges for each of three customers who parked their
cars in this garage yesterday. You should enter the hours parked for each customer.
Your program should print the results in a neat tabular format and calculate and print
the total of yesterday’s receipts. The program should use the function calculateCharges
to determine the charge for each customer. Your outputs should appear in the following
format:
27. What does the following program do?
28. Write C++ statements to accomplish each of the following:
A. Display the value of element 6 of the character array alphabet.
B. Input a value into element 4 of one-dimensional floating-point array grades.
C. Initialize each of the 5 elements of one-dimensional integer array values to 8.
D. Total and display the elements of array temperatures of 100 double elements.
E. Determine and display the smallest and largest values in array values containing 99
double elements.
29. Pointers:
A. Declare the variable doublePtr to be a pointer to an object of type double and
initialize the pointer to nullptr.
B. Assign the address of the variable number1 to pointer variable doublePtr.
C. Display the value of the object pointed to by doublePtr.
D. Assign the value of the object pointed to by doublePtr to variable number2.
E. Display the value of number2.
F. Display the address of number1.
G. Display the address stored in doublePtr. Is the address the same as the address of
variable number1?
30. Write a program that separately inputs a first and last name and concatenates the two with a
space between them into a new string. Show two techniques for accomplishing this.
31. Write a program that inputs a string and prints it backward. Convert all uppercase characters
to lowercase and all lowercase characters to uppercase.
32. Class:
A. Describe Class as a Blueprint
B. What’s a default constructor?
C. Discuss how inheritance saves time during program development and helps prevent
errors.
D. Create a class Rectangle with attributes m_length and m_width, each of which
defaults to 1. Provide member functions that calculate the perimeter and the area of
the rectangle. Also, provide set and get functions for the m_length and m_width
attributes. The set functions should verify that m_length and m_width are each larger
than 0.0 and less than 20.0.
33. Fine more yourself based on your handout.