Chapter 2: Checkpoints
When the following main function runs, what will display on the screen? int main() { int number; number = 712; cout << "The value is " << "number" << endl; return 0; }
The value is number
What is wrong with the following program statement? char letter = "Z";
char uses single quotes, not double quotes (string literal).
Write a program that will display your name, address, zip code, and number on separate lines:
cout << "name \n" << "address \n" << "zip code \n" << "number" << endl;
The following cout statement contains errors. cout << "red /n" << "blue \ n " << "yellow" \n << "green"; Correct it so that it will display a list of colors, with one item per line.
cout << "red \n" << "blue \n" << "yellow \n" << "green";
Write a program that displays: My age is 26 (int) and my weight is 168.5 (double) pounds.
#include <iostream> using std::cout; using std::endl; int main() { int age = 27; double weight = 240; cout << "My age is " << age << " and my weight is " << weight << " pounds." << endl; return 0; }
What output will the following lines of code display on the screen? cout << "The works of Wolfgang\ninclude the following"; cout << "\nThe Turkish March" << endl; cout << "and Symphony No. 40 "; cout << "in G minor." << endl;
"The works of Wolfgang include the following The Turkish March and Symphony No. 40 in G minor.
Write a program that will display your name on the screen.
#include <iostream> using std::cout; int main() { cout << "David Laird" << endl; return 0; }
What integer data types can only hold non-negative values?
unsigned: short, int, long
List all the variables and literals that appear below. int main() { int little; int big; little = 2; big = 2000; cout << "The little number is " << little << endl; cout << "The big number is " << big << endl; return 0; }
variables: little, big literals: 2, 2000, "The little number is", "The big number is", 0
Write a program that stores your name, address, and phone number in three separate string objects. Then display their contents on the screen.
#include <iostream> #include <string> using std::cout; using std::endl; using std::string; int main() { string name, string address, string phone; name = "Dave"; address = "E Lehigh"; phone = "3oh3"; cout << name << endl; cout << address << endl; cout << phone << endl; return 0; }
The following C++ program will not compile because the lines have been mixed up. int main() } //A crazy mixed up program #include <iostream> return 0; cout << "In 1492 Columbus sailed the ocean blue."; { using namespace std; Properly arrange the lines to display: In 1492 Columbus sailed the ocean blue.
#include <iostream> using namespace std; //A crazy mixed up program int main() { cout << "In 1492 Columbus sailed the ocean"; cout << "blue."; return 0; }
Which of the following is a char literal? 'B' "B"
'B'
Assuming the char data type uses 1 byte of memory, how many bytes do each of the following literals use: 'Q' "Q" "Sales" '\n'
'Q' = 1 byte "Q" = 2 bytes "Sales" = 6 bytes '\n' = 1 byte
What will the following code display? int number; number = 3.625; cout << number;
3
How would the following number in scientific notation be represented in E notation? 6.31 x 10^17
6.31E17
What are the ASCII codes for the following char (Refer to Appendix A.): C F W
67, 70, 87
Which of the following are illegal C++ variable names, and why? x 99bottles july97 theSalesFigureForFiscalYear98 r&d grade_report
99bottles - variables can only begin with letters or underscores (_). r&d - variables can only contain numbers, letters, or underscores (_).
What header file must you include in order to use string objects?
<string>
What will be assigned to x in each of the following statements? A) x = 8 + 3; B) x = 8 -3; C) x = 8 * 3; D) x = 8 & 3;
A) 11 B) 5 C) 24 D) 2
Refer to the data types listed in Table 2-6 provide the best data types for the following ranges: A) 32 to 6,000 B) -40,000 to +40,000 C) 20 and 20 L are both integer literals. Does one use more memory than the other, and if so which one, or do they both use the same number of bytes?
A) short or unsigned short B) int C) They both use the same amount of memory.
Is the following assignment statement valid or invalid? If it is invalid, why? 72 = amount
Invalid. The value on the left of the = operator must be an lvalue, such as a variable name.
Is the variable name Sales the same as sales? Why or why not?
No, C++ considers Sales and sales as two different variable because variables are case sensitive
When the about main funtion runs, what will display on the screen?
The little number is 2 The big number is 2000
What is wrong with the following program? How would you correct it? #include <iostream> using namespace std; int main() { critter = 62.7; double critter; cout << critter << endl; return 0; }
The variable critter is assigned a value before it is declared. You can correct the program by moving the statement critter = 62.7 after the statement double critter.
Which of the following are legal C++ assignment statements? a. a = 7; b. 7 = a; c. 7 = 7;
a. a = 7
How would you combine the following variable definition and assignment statement into a single statement? int apples; apples = 20;
int apples = 20;
How would you combine the following variable definitions into a single statement? int xCoord = 2; int yCoord = -4; int zCoord = 6;
int xCoord = 2, yCoord = -4, zCoord = 6;
Is the following an example of integer division or floating-point division? What value will be displayed? cout << 16 / 3;
integer division; 5