Chapter 2 Flashcards
What marks the beginning of a comment in C++?
//
What is the purpose of comments in C++ programs?
To explain what’s going on in the code
What is a preprocessor directive in C++?
A line that begins with # and is executed by the preprocessor
What does the #include <iostream> directive do?</iostream>
Includes the contents of the iostream header file
What is the role of the iostream header file?
Allows a C++ program to display output and read input
What does the statement ‘using namespace std;’ do?
Gives access to the names in the std namespace
What is the significance of ‘int main()’ in a C++ program?
It marks the beginning of the main function, which is the starting point of the program
What does ‘int’ represent in ‘int main()’?
Indicates that the function returns an integer value
Why is C++ considered a case-sensitive language?
It treats uppercase and lowercase letters as different characters
What is the function of the left brace ‘{‘ in C++?
Indicates the beginning of a function’s body
What is a string literal in C++?
A group of characters enclosed in “double quotation marks”
What does the statement cout << "Programming is great fun!";
do?
Displays the message on the screen
What symbol marks the end of a complete statement in C++?
;
What does ‘return 0;’ signify in a C++ program?
Indicates successful execution of the program
What does a closing brace ‘}’ denote in a C++ program?
Marks the end of a function or block
The contents of the < iostream > file are included in the program at the point the #include statement appears, which is called a ?
preprocessor directive
True or False: Every C++ program must have a function called main.
True
What is the output of the sample program?
// A simple C++ program
#include <iostream>
using namespace std;</iostream>
int main(){
cout «_space;“Programming is great fun!”;
return 0;
}
Programming is great fun!
What is the purpose of the cout object in C++?
To display information on the computer’s screen
cout stands for console output.
What is the stream insertion operator in C++?
It sends a stream of characters to cout.
«
It sends a stream of characters to cout.
What does the statement cout «_space;“Programming is great fun!”; do?
It displays the message ‘Programming is great fun!’ on the screen.
Can the stream insertion operator be used to send multiple items to cout?
Yes.
True or False: The cout object starts a new line automatically after each output.
False.
How can you instruct cout to start a new line?
Using the stream manipulator endl or the escape sequence \n.
What does the endl manipulator do?
Advances the output position to the beginning of the next line.
What is an escape sequence?
A series of characters starting with a backslash that control output display.
What is the newline escape sequence in C++?
\n
What happens if you use a forward slash instead of a backslash in an escape sequence?
It will not work and will display the forward slash characters on the screen.
Fill in the blank: A raw string literal begins with ___ and ends with ___.
R”( and )”
What is the advantage of using a raw string literal?
Escape sequences are not processed, and backslashes are treated as literal characters.
What does the statement cout «_space;R”(One\nTwo\nThree)”; display?
One\nTwo\nThree
What is the output of the following code? cout «_space;“One\nTwo\nThree”;
One
Two
Three
Why is it important to avoid spaces between the backslash and the character in an escape sequence?
It can lead to incorrect interpretation of the escape sequence.
What will happen if you forget to put \n inside quotation marks?
It will result in a compilation error.
True or False: Each escape sequence is stored in memory as multiple characters.
False.
What is the output of this code? cout «_space;“The following items were top sellers” «_space;endl «_space;“during the month of June:” «_space;endl;
The following items were top sellers
during the month of June:
How can you control the display of strings with backslashes, like file paths?
By using raw string literals.
Write a program that will display your name on the first line, your street address on the second line, your city, state, and ZIP code on the third line, and your telephone number on the fourth line. Place a comment with today’s date at the top of the program. Test your program by compiling and running it.
include <iostream></iostream>
// today’s date
using namespace std;
int main() {
cout «_space;“Christian DaSilva\n”;
cout «_space;“74 West Meadow Road\n”;
cout «_space;“Lowell, MA, 01854” «_space;endl;
cout «_space;“978-458-8768”;
}
What cout stand for?
console output
what is a stream object?
a stream object works with streams of data
What does a variable represent?
Variables represent storage locations in the computer’s memory.
What is a literal?
Literals are constant values that are assigned to variables. They are a piece of data that is written directly into a program’s code. Aka constants.
Variable definition
tells the compiler the variables name and the type of data it will hold
2.5 Examine the following program:
// This program uses variables and literals.
#include <iostream>
using namespace std;
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;
}
List all the variables and literals that appear in the program.</iostream>
Variables: little, big
Literals: 2, 2000, “The little number is “, “The big number is “, 0
2.6 What will the following program display on the screen?
#include <iostream> using namespace std; int main() { int number; number = 712; cout << "The value is " << "number" << endl; return 0; }
The value is number
Identifier
A programmer-defined name that represents some element of a program. Variable names are example of identifiers.
What is a self-documenting program?
provides understanding of what the program is doing just by reading the code. Such as Naming a variable based on the variables use. Ex: int playerHeath vs int x
Legal Identifiers
- first character must be one of a-z, A-Z, or _(underscore)
- after the first character you may use a-z, A-Z, 0-9, or _(underscore)
- uppercase and lowercase characters are distinct. playerhealth is not the same as playerHealth
What is an Integer Data Type?
Determines the kind of information that may be stored in variables.
In a broad sense what are the two data types in C++?
Number and Character
What are the two types of numeric data?
Integer and Floating Point
What are the primary considerations for selecting a data type?
- The largest and smallest numbers that may be stored in the variable
- How much memory the variable uses
- Where the variable stores signed or unsigned numbers
- The number of decimal places of precision the variable has
What can unsigned data types store?
They can only store nonnegative values.
You are not allowed to use commas to separate digits for readability. What can you use as a digit separator?
Single Quotation
integer literal
A numeric literal that is treated as an int. A result of the numeric literal fitting within the range of int.
What do you add to an integer value to make it a long or a long long?
L or LL Ex: 32(integer) 32L(long integer) 32LL(long long integer)
2.7 Which of the following are illegal variable names, and why?
X
99bottles
july97
theSalesFigureForFiscalYear98
r&d
grade_report
99bottles(starts with a number)
r&d(contains ampersand)
2.8 Is the variable name Sales the same as sales? Why or why not?
No, because variable names are case sensitive
2.9 Refer to the data types listed in Table 2-6 for these questions.
A) If a variable needs to hold numbers in the range 32 to 6,000, what data type would be best?
unsigned short int
B) If a variable needs to hold numbers in the range 240,000 to 140,000, what
data type would be best?
unsigned int
C) Which of the following literals uses more memory? 20 or 20L
they use the same
A) unsigned short int
B) unsigned int
C) they use the same
2.10 On any computer, which data type uses more memory, an integer or an unsigned integer?
they use the same
char data type
used to store individual characters
What is used to enclose character literals?
enclosed in ‘single quotation marks’
How are characters stored in C++?
characters are internally represented by numbers(ASCII codes)
What is a null terminator or null character?
It marks the end of a string with ASCII code 0. Stored as one character = \0
Strings are fill_in of characters that occupy fill_in of memory.
consecutive sequences, consecutive bytes
How are string literals stored?
String literals are always stored in memory with a null terminator at the end. This marks the end of the string.
How are Escape Sequences stored?
Escape Sequences such as ‘\n’ are stored internally as a single character.
How much memory do Characters occupy?
Normally a single byte
2.15 What is wrong with the following program statement?
char letter = “Z”;
char values need to be enclosed within single quotation marks
2.16 What header file must you include in order to use string objects?
<string>
</string>
2.14 Write a program that has the following character variables: first, middle, and
last. Store your initials in these variables then display them on the screen.
include <iostream> // include the contents of the header file iostream to display output on the screen</iostream>
using namespace std; // use names within the std namespace. I still don’t know which names
int main() { // beginning of the main function
// create defined variables with values
char first = ‘c’;
char middle = ‘j’;
char last = ‘d’;
// display the variables cout << first << middle << last; return 0; // return 0 to the computer } // end program
2.17 Write a program that stores your name, address, and phone number in three
separate string objects. Display the contents of the string objects on the screen.
include <iostream> // inlcude the contents of the iostream header file. allows input and output display to the screen</iostream>
#include <string> // allows use of strings
using namespace std; // allows use of the names within the std namespace</string>
int main() { // beginning of main function. required in all C++ programs
// define variables string name, address, phone_number; // assign values to variables name = "Christian"; address = "74 West Meadow Road, Lowell, MA"; phone_number = "978-458-6828"; // display contents of the string objects cout << name << endl; // display name to the screen on first line cout << address << endl; // display address to the screen on second line cout << phone_number << endl; // display phone_number to the screen on the third line return 0; // send 0 to the computer } // end program
2.12 Which of the following is a character literal?
‘B’
“B”
‘B’ char literal
“B” string literal
Define a bool
a data type that can be true or false. true = 1 and false = 0
truncated
when part of a value is discarded
2.18 Yes or No: Is there an unsigned floating-point data type? If so, what is it?
No
2.19 How would 6.31 x 10^17 in scientific notation be represented in E notation?
6.31E17
2.20 Write a program that defines an integer variable named age and a float variable named weight. Store your age and weight, as literals, in the variables. The program should display these values on the screen in a manner similar to the following:
My age is 26 and my weight is 180 pounds.
What are the three data types for floating-point?
float
double
long double
Floating-point literals are normally stored as which data type?
double
How do you store a literal as a float?
append with the letter F. Ex: 10F
How do you force store a literal as a long double?
append with the letter L. Ex: 10L
Floating-Point Data Types
used to define variables that can hold real numbers. allows for fractional values.
What is the difference between a variable assignment vs. initialization?
An assignment operation assigns, or copies, a value into a variable.
When a value is assigned to a variable as part of the variable’s definition, it is called an initialization.
lvalue
value on the left side of an assignment. it identifies a place in memory whose contents may be changed
rvalue
operand on the right side of an operator. rvalue is any expression that has a value.
what is an auto key word?
determines the variables data type from the initialization value
Variable Scope
the part of the program that has access to the variable. a variable cannot be used in a program before it has been defined.
2.21 Is the following assignment statement valid or invalid? If it is invalid, why?
72 = amount;
Invalid, amount cannot be assigned to the value 72. 72 is a rvalue and must be to the right of the operand. amount can be either a rvalue or lvalue. In this operation it must be an lvalue
2.22 How would you consolidate the following definitions into one statement?
int x = 7;
int y = 16;
int z = 28;
int x = 7, y = 16, z = 28;
2.23 What is wrong with the following program? How would you correct it?
#include <iostream>
using namespace std;
int main()
{
number = 62.7;
double number;
cout << number << endl;
return 0;
}</iostream>
either define number as a double before assign the value 62.7 or initialize number
2.24 Is the following an example of integer division or floating-point division? What
value will be stored in portion?
portion = 70 / 3;
integer division, value = 23
Integer Division
when both operands of a division statement are operands. the result will be an integer.
Named Constant
like a variable but its content is read-only and cannot be changed while the program is running. initialization value must be present.
Ex: const VAR_NAME = 1.23;
2.25 Write statements using the const qualifier to create named constants for the following literal values:
Literal Value Description
2.71828 Euler’s number (known in mathematics as e)
5.256E5 Number of minutes in a year
32.2 The gravitational acceleration constant (in ft/s2)
9.8 The gravitational acceleration constant (in m/s2)
1609 Number of meters in a mile
const EULERS_NUMBER = 2.71828;
const MINUTES_IN_ONE_YEAR = 5.256E5;
const GRAVITY_ACCELERATION_CONST_FTPERSEC2= 32.2;
const GRAVITY = 9.8;
const METER_IN_ONE_MILE = 1609;
cin Object
can be used to read data typed at the keyboard
prompt
the question displayed to the user prior to requesting a cin read input. letting the user
know they must type a value at the keyboard
stream extraction operator
> > it gets the character for the stream object on
its left and stores them in a variable whose
name appears on its right
keyboard buffer
an area of memory where the value the user types
at the keyboard is first stored.
3.2 True or false: cin requires the user to press the Enter key when finished entering
data.
True
3.3 Assume value is an integer variable. If the user enters 3.14 in response to the
following programming statement, what will be stored in value?
cin»_space; value;
A) 3.14
B) 3
C) 0
D) Nothing. An error message is displayed.
B) 3
3.4 A program has the following variable definitions.
long miles;
int feet;
float inches;
Write one cin statement that reads a value into each of these variables.
cin «_space;miles «_space;feet «_space;inches;
3.5 The following program will run, but the user will have difficulty understanding
what to do. How would you improve the program?
// This program multiplies two numbers and displays the result.
#include <iostream>
using namespace std;
int main()
{
double first, second, product;
cin >> first >> second;
product = first * second;
cout << product;
return 0;
}</iostream>
Display a prompt to the user asking for the two values being multipied
3.6 Complete the following program skeleton so it asks for the user’s weight (in
pounds) and displays the equivalent weight in kilograms.
#include <iostream>
using namespace std;
int main()
{
double pounds, kilograms;
// Write code here that prompts the user
// to enter his or her weight and reads
// the input into the pounds variable.
// The following line does the conversion.
kilograms = pounds / 2.2;
// Write code here that displays the user's weight
// in kilograms.
return 0;
}</iostream>