Chapter 2

24 July 2022
4.7 (114 reviews)
115 test answers

Unlock all answers in this set

Unlock answers (111)
question
A comment starts with what characters?
answer
//
question
The text of a comment is checked by the compiler for accuracy. must appear in the first line of the program. is printed out when the program runs. can be anything the programmer wants to write.
answer
can be anything the programmer wants to write.
question
A preprocessor directive starts with what character or characters?
answer
#
question
Preprocessor directives are carried out: just before a program is loaded into the processor. just before a program is executed by the central processing unit (CPU). just before a program is processed by the compiler. just before the program's output is processed.
answer
just before a program is processed by the compiler. its the set up for your source code. They also dont require ; at the end
question
Function
answer
Can be though of as a group of one or more programming statements that collectively has a name.
question
{} Braces
answer
All the statements that make up a function are enclosed in a set of braces.
question
Literals
answer
"Constant" values that are assigned to variables. can be string or integer
question
Which special of the following special characters does NOT need to be paired with another special character in C++? { ; ( "
answer
;
question
Every C++ program must contain a ____ function.
answer
main
question
Which of the following names in a program is equivalent to the name int? Int INT All of the above None of the above
answer
None of the above //Correct - Only int is int: case matters.
question
The names defined in iostream are associated with which namespace?
answer
std
question
Rearrange the following code so that it forms a correct program that prints out the letter Q: int main() } // A SCRAMBLED program return 0; #include cout << "Q"; { using namespace std;
answer
#include using namespace std; int main() { cout<<"Q"; return 0; }
question
The character escape sequence to force the cursor to go to the next line is:
answer
n
question
The character escape sequence to force the cursor to advance forward to the next tab setting is:
answer
t
question
The character escape sequence to represent a single quote is:
answer
'
question
The character escape sequence to represent a double quote is:
answer
"
question
How many lines are printed out by this statement: cout << "abcndeftghinjkl" << endl << endl << "mnonpqrn";
answer
6
question
Write a complete program that prints Hello World to the screen.
answer
#include using namespace std; main() { cout <<"Hello World"; return 0; }
question
Suppose your name was George Gershwin. Write a complete program that would print your last name, followed by a comma, followed by your first name. Do not print anything else (that includes blanks).
answer
#include using namespace std; int main() { cout << "Gershwin,George " <
question
Write a statement that prints the following to standard output : i= Just write one statement that generates the message above: do not generate any extraneous spaces. Do not declare variables , do not write a main() function, do not write a whole program .
answer
cout << "i=";
question
Given an integer variable count, write a statement that writes the value of count to standard output .
answer
cout << count;
question
The word in the brackets of an include directive specifies A. a namespace. B. a file containing code that is copied into the program at that point. C. a file containing definitions of input/output code. D. the name of the program.
answer
A file containing code that is copied into the program at that point.
question
Which of the following will not be recognized if iostream is not included in the program? main std namespace return cout
answer
cout
question
Write the include directive needed to allow use of the various I/O operators such as cout and cin .
answer
#include
question
Write the include directive that allows use of the function headers in the file myFuncs.h.
answer
#include
question
Write a statement that declares an int variable named count.
answer
int count;
question
Declare an integer variable named degreesCelsius .
answer
int degreesCelsius;
question
Which of the following is NOT a legal identifier? outrageouslyAndShockinglyLongRunon _42 _ lovePotionNumber9 7thheaven
answer
7thheaven
question
An identifier that cannot be used as a variable name is a
answer
reserved word
question
Which of the following IS a legal identifier? 5_And_10 Five_ Constant ____________ LovePotion#9 "Hello World"
answer
____________ Rules: First character , must be a letter or underscore character. After first, you may use letters, numbers, or underscore. Upper and lower case symbolize 2 diff things.
question
Which of the following is NOT a C++ keyword? using int main namespace return
answer
main
question
Identifier
answer
A programmer-defined name that rep some element of a program.
question
Which is the best identifier for a variable to represent the amount of money your boss pays you each month? notEnough wages paymentAmount monthlyPay money
answer
monthlyPay
question
Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not? temperature feverTest hasFever fever
answer
hasFever
question
Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not? divisible isPrime mightBePrime number
answer
mightBePrime
question
Declare a variable populationChange , suitable for holding numbers like -593142 and 8930522.
answer
int populationChange;
question
Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.
answer
int profitStartOfQuarter; int cashFlowEndOfYear;
question
Write a declaration of a variable named numberOfWidgets that can be used to hold numbers like 57 and 981 and -4.
answer
int numberOfWidgets;
question
Write a declaration for a variable hits that can hold the number of times a baseball player has hit the ball in a baseball game.
answer
int hits;
question
Write a declaration of a variable named count that can be used to hold numbers like 90000 and -1 and -406.
answer
int count;
question
Write a declaration of a variable named number_of_children that can be used to hold the number of children in a family.
answer
int number_of_children;
question
Write a declaration for two integer variables , age and weight.
answer
int age, weight;
question
Declare an unsigned integer variable named degreesKelvin .
answer
unsigned int degreesKelvin;
question
Declare a long integer variable named grossNationalProduct.
answer
long int grossNationalProduct;
question
Write a hexadecimal integer literal representing the value fifteen.
answer
0xf
question
Write a literal representing the long integer value twelve million.
answer
12000000L
question
Write a long integer literal that has the value thirty-seven
answer
37L
question
Qualifying an integer declaration with "unsigned" essentially increases the maximum value of the variable by a factor of
answer
2
question
Write a character literal representing the (upper case) letter A .
answer
'A'
question
Write a literal representing the character whose ASCII value is 65.
answer
'A'
question
Write a character literal representing the digit 1 .
answer
'1'
question
Write a literal representing the largest character value . Suppose we are using unsigned one-byte characters .
answer
255
question
Declare a character variable named c.
answer
char c;
question
Integer variables
answer
Can only hold whole numbers. -Float point holds decimals
question
QUESTION 1: How many bytes are needed to store: 'n' ? QUESTION 2: How many bytes are needed to store: "n" ? QUESTION 3: How many bytes are needed to store: 'n' ? QUESTION 4: How many bytes are needed to store: "n", ? QUESTION 5: How many bytes are needed to store: "n" ? QUESTION 6: How many bytes are needed to store: "" ?
answer
1. 1 2. 2 3. 1 4. 2 5. 3 6. 1
question
What's the difference in UNICODE value between 'E' and 'A'? (consult a table of UNICODE values): QUESTION 2: What's the difference in UNICODE value between 'e' and 'a'? (consult a table of UNICODE values): QUESTION 3: What's the difference in UNICODE value between '3' and '0'? (consult a table of UNICODE values): QUESTION 4: What's the difference in UNICODE value between '6' and '0'? (consult a table of UNICODE values):
answer
1. 4 2. 4 3. 3 4. 6
question
Write the necessary preprocessor directive to enable the use of the C++ string class .
answer
#include
question
Declare a string variable named mailingAddress.
answer
string mailingAddress;
question
Declare a string variable named empty and initialize it to the empty string .
answer
string empty="";
question
Assume that message is a string variable . Write a statement to display its value on standard output .
answer
cout << message;
question
Assume that word is a string variable . Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of word on standard output .
answer
cout << "Today's Word-Of-The-Day is: "+word;
question
Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine").
answer
3.14159
question
Write a floating point literal corresponding to the value zero.
answer
.0
question
Write a statement that declares a double variable named dosage.
answer
double dosage;
question
Write a declaration for a variable temperature that can hold the current outdoor temperature, measured to the half degree (like 98.6 or 31.5).
answer
float temperature;
question
Declare a variable x , suitable for storing values like 3.14159 and 6.02E23.
answer
float x;
question
The char data type
answer
Is used to store individual characters(1 letter) and needs to be put in ' '. These are characters are known as character literals. Its actually an integer data type that typically uses 1 byte of memory.
question
Null terminator/character
answer
Its 0. It adds a extra byte to a string to inform it that it is concluded.(so in terms of space its always +1)
question
Strings
answer
Are consecutive sequences of characters that occupy consecutive bytes of memory.
question
String literals
answer
Are always stored in memory with a null terminator at the end. Which marks the end of a string. Enclosed in " "
question
Character literals
answer
are enclosed in ' '
question
C++ string class
answer
#include then define the string variable Allows you to store more then one character in a variable unlike char.
question
Given a floating-point variable fraction, write a statement that writes the value of fraction to standard output . Do not write anything else to standard output -- just the value of fraction.
answer
cout << fraction;
question
Two variables , num and cost have been declared and given values : num is an integer and cost is a double . Write a single statement that outputs num and cost to standard output . Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character . Do not output any thing else.
answer
cout <
question
Given an integer variable i and a floating-point variable f, write a statement that writes both of their values to standard output in the following format: i=value -of-i f=value -of-f Thus, if i has the value 25 and f has the value 12.34, the output would be: i=25 f=12.34 But if i has the value 187 and f has the value 24.06, the output would be: i=187 f=24.06
answer
cout << "i=" << i << " " << "f=" << f;
question
Write a literal representing the false value in C++.
answer
false
question
Write a literal representing the true value .
answer
true
question
Boolean expressions
answer
Expressions that have a true or false value. True = 1 False = 0
question
Declare a variable isACustomer, suitable for representing a true or false value .
answer
bool isACustomer;
question
Declare a variable hasPassedTest , and initialize it to true .
answer
bool hasPassedTest=true;
question
Write an expression whose value is the number of bytes that an int variable occupies on whatever machine the expression is executed on.
answer
sizeof(int);
question
Write an expression whose value is the number of bytes that an unsigned long variable occupies on whatever machine the expression is executed on.
answer
sizeof(long);
question
The exercise instructions here are LONG -- please read them all carefully. If you see an internal scrollbar to the right of these instructions, be sure to scroll down to read everything. Declare and initialize the following variables : A variable monthOfYear , initialized to the value 11 A variable companyRevenue , initialized to the value 5666777 A variable firstClassTicketPrice , initialized to the value 6000 A variable totalPopulation , initialized to the value 1222333
answer
int monthOfYear=11; long companyRevenue=5666777; int firstClassTicketPrice=6000; long totalPopulation=1222333;
question
Initialization
answer
When a value is assigned to a variable as a part of a variables definition.
question
Operands
answer
THe data that operators work with. Ex: unitSold = 12 Then unitSold and 12 are operands.
question
Declare an integer variable cardsInHand and initialize it to 13.
answer
int cardsInHand=13;
question
Declare a variable temperature and initialize it to 98.6.
answer
float temperature=98.6;
question
Declare a numerical variable precise and initialize it to the value 1.09388641.
answer
double precise=1.09388641;
question
Declare a string variable named str and initialize it to Hello .
answer
string str="Hello";
question
Write a statement that declares an int variable presidentialTerm and initializes it to 4.
answer
int presidentialTerm=4;
question
Write a statement to set the value of num to 4 (num is a variable that has already been declared ).
answer
num=4;
question
Given two integer variables num and highest, write a statement that gives highest the same value that num has.
answer
highest=num;
question
Given two double variables , bestValue and secondBestValue, write some code that swaps their values . Declare any additional variables as necessary.
answer
double tempValue; tempValue=bestValue; bestValue=secondBestValue; secondBestValue=tempValue;
question
Given two int variables , i and j, which have been declared and initialized , and two other int variables , itemp and jtemp, which have been declared , write some code that swaps the values in i and j by copying their values to itemp and jtemp, respectively, and then copying itemp and jtemp to j and i, respectively.
answer
itemp=i; jtemp=j; i=jtemp; j=itemp;
question
Given three already declared int variables , i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j .
answer
temp=i; i=j; j=temp;
question
Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values . Declare any additional variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.
answer
int temp; temp=firstPlaceWinner; firstPlaceWinner=secondPlaceWinner; secondPlaceWinner=temp;
question
Consider Program 2-19 below. Rearrange the lines of code to fix the scope error, yielding a correct C++ program that prints the number 100.
answer
#include using namespace std; int main() { int value = 100; cout <
question
Assume that an int variable x that has already been declared and initialized . Write an expression whose value is 1 more than x .
answer
x+1
question
Write an expression that computes the sum of the two variables verbalScore and mathScore (already declared and assigned values ).
answer
verbalScore + mathScore
question
Given the variables fullAdmissionPrice and discountAmount (already declared and assigned values ), write an expression corresponding to the price of a discount admission. (The variable discountAmount holds the actual amount discounted, not a percentage.)
answer
fullAdmissionPrice - discountAmount
question
Given the variable pricePerCase , write an expression corresponding to the price of a dozen cases.
answer
pricePerCase * 12
question
Given the variables costOfBusRental and maxBusRiders , write an expression corresponding to the cost per rider (assuming the bus is full).
answer
costOfBusRental/maxBusRiders
question
Write an expression that computes the remainder of the variable principal when divided by the variable divisor . (Assume both are type int .)
answer
principal % divisor
question
Write a statement to set the value of ans equal to the value of num plus 5. (These variables have already been declared and num has already been initialized .)
answer
ans = num + 5;
question
Write a statement to subtract tax from gross_pay and assign the result to net_pay . (The variables have already been declared and gross_pay and tax have already been initialized .)
answer
net_pay=gross_pay-tax;
question
Write a statement to multiply diameter by 3.14159 and assign the result to circumference. (The variables have already been declared and diameter has already been initialized .)
answer
circumference=diameter*3.14159;
question
Write a statement to assign to kilos the value of pounds divided by 2.2. (The variables have already been declared and pounds has already been initialized .)
answer
kilos = pounds/2.2;
question
Assume that an int variable x has already been declared ,. Write an expression whose value is the last (rightmost) digit of x .
answer
x % 10
question
A variable's scope
answer
Every variable has a scope. The scope of a variable is the part of the program where the variable may be used.
question
Which of the following lines contains a valid comment? int twoPi = 3.14159; /* holds the value of two times pi */ int twoPi = 2*3.14159; /* holds the value of two times pi //* int twoPi = 2*3.14159; / / *holds the value of 6 //* double twoPi = 2*3.14159; /* // holds the value of two time pi */ [comment] //
answer
int twoPi = 3.14159; /* holds the value of two times pi */
question
Which of the following lines does NOT consist of (a) valid, though boastful, comment(s)? // /* This is a */ First Rate Program //**// This is a //**// First Rate Program //**// //* This is a //*// First Rate Program //*// /* This is a //*// First Rate Program //*//
answer
/* This is a //*// First Rate Program //*//
question
Which comment below is the most helpful? int volume; // declare an int int volume; // declare volume int volume; // declare volume to be an int variable int volume; // size of trunk in cubic feet
answer
int volume; // size of trunk in cubic feet
question
Declare an int constant , MonthsInYear , whose value is 12 .
answer
const int MonthsInYear = 12;
question
Dividing two integers
answer
Will only result in an integer(whole #) and integer division occurs. AKA you lose the decimal place NOT rounding.
question
Returning float points
answer
Even if double is declared you may still lose the decimal. Ex: double Num Num = 5/2 Code prints out 2 NOT 2.5 However Num = 5.0/2