Procedural Programming midterm review
Given the input: Samantha 168.5 46 and the variable declaration: (2, 8) double dec = 2.7; int num = 45; string str = "**"; What is the output, if any? Use the same input for each part. a. cin >> str >> dec >> num; cout << str << " " << dec << " " << num << endl; b. cin >> str >> num >> dec; cout << str << " " << dec << " " << num << endl; c. cin >> dec >> str >> num; cout << str << " " << dec << " " << num << endl;
a. Samantha 168.5 46 b. Samantha 0.5 168 c. ** 2.7 45 Input failure: Trying to read S into dec, which is a double variable. The values of dec, num, and str are unchanged.
A program reads the data from a file called inputFile.dat and, after doing some calculations, writes the results to a file called outFile.dat. Answer the following questions: a. After the program executes, what are the contents of the file inputFile.dat? b. After the program executes, what are the contents of the file outFile.dat if this file was empty before the program executed? c. After the program executes, what are the contents of the file outFile.dat if this file contained 100 numbers before the pro- gram executed? d. What would happen if the file outFile.dat did not exist before the program executed?
a. Same as before. b. The file contains the output produced by the program. c. The file contains the output produced by the program. The old contents are erased. d. The program would prepare the file and store the output in the file
What actions must be taken before a variable can be used in a program?
An identifier must be declared before it can be used.
Which number system is used by a computer?
Base 2 or Binary
How does the output in Exercise 10 change if the statement in Line 4 is replaced by the following statement? done = (num == static_cast<int>(2 * gpa) + static_cast<int>(9.3)); //Line 4
The value of done is: 0
To use a parameterized stream manipulator in a C++ program, you must include the header file _________. a. fstream b. iostream c. iomanip d. string
c. iomanip
Which of the following expressions evaluates to true? a. "Hello" > "Hi" b. "Hello" = "hello" c. "Air" < "An" d. "Bill" >= "Billy"
c."Air" < "An"
The specification of the assert function is found in the header file _________.
cassert
A memory location whose content is not allowed to change during program execution is known as a
const
Which of the following is not an input stream function? a. ignore b. putback c. get d. endl
d. endl
In ________ evaluation, the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. a. conditional b. associative c. decision maker d. short-circuit
d. short-circuit
To force floating-point output to show the decimal point and trailing zeros, use the ___________ manipulator. a. scientific b. decimal c. point d. showpoint
d. showpoint
The __________ loop is a post-test loop
do...while
What does the following expression evaluate to? !('A' < 'B')
false
What does the following expression evaluate to? (24 >= 35) && ('A' < 'B')
false
To use the functions peek and putback in a program, which header file(s) must be included in the program?
iostream
Putting one control structure inside another is called __________.
nesting
The output stream variables can use the manipulator _________ to fill the unused columns with a character other than a space.
setfill
Suppose that class Standing is a char variable, gpa and dues are double variables. Write a switch expression that assigns the dues as following: If classStanding is 'f', the dues are $150.00; if classStanding is 's', (if gpa is at least 3.75, the dues are $75.00; otherwise dues are 120.00); if classStanding is 'j', (if gpa is at least 3.75, the dues are $50.00; otherwise dues are $100.00); if classStanding is 'n', (if gpa is at least 3.75, the dues are $25.00; otherwise dues are $75.00). (Note that the code 'f' stands for first year students, the code 's' stands for second year students, the code 'j' stands for juniors, and the code 'n' stands for seniors.)
switch (classStanding) { case 'f': dues = 150.00; break; case 's': if (gpa >= 3.75) dues = 75.00; else dues = 120.00; break; case 'j': if (gpa >= 3.75) dues = 50.00; else dues = 100.00; break; case 'n': if (gpa >= 3.75) dues = 25.00; else dues = 75.00; break; default: cout << "Invalid class standing code." << endl; }
The logical expression !(p && !q) equivalent to (!p || q). (Hint: See if they have matching truth tables.)
true
The use of the keywords true and false are considered best to use for logical expressions, over the equivalent values of 1 and 0.
true
What does the following expression evaluate to? !!(6 <= 7)
true
What does the following expression evaluate to? (14 >= 5) && ('A' < 'B')
true
What does the following expression evaluate to? (24 >= 35) || ('A' < 'B')
true
When C++ evaluates a logical expression, any nonzero value is treated as _____________.
true
Suppose x, y, and z are int variables and w and t are double variables. What value is assigned to each of these variables after the last state- ment executes? x = 8; y = x + 3; z = x * y + 2 * x; x = z - y % 4; w = 2.5 * z - x; t = w / 2 + 13 / 4 - y % 5;
x = 101 y = 11 z = 104 w = 159.00 t = 81.50
Suppose that x, y, z, and w are int variables. What is stored in x, y, z, and w after the following statements execute? x = 9; y = x - 4; z= (y + 7) % 6; w = (x * z) / y - 3; z = w + (x - y + 2) % x;
x= 9, y = 5, z = 3, w = -3
In C++, all preprocessor commands begin with _____.
#
In a C++ program, preprocessor directives begin with which symbol?
#
The following program contains errors. Correct them so that the pro- gram will run and output w = 25. #include <iostream> using namespace std; const int SECRET = 5 main ( ) { int x, y, w, z; z = 13; if (z < 15); x = 12; y = 8, w = x + y + SECRET; else x = 12; y = 8, w = x + y + SECRET; cout << "w = " << w << endl; }
#include <iostream> using namespace std; const int SECRET = 5; int main() { int x, y, w, z; z = 9; if (z > 10) { x = 12; y = 5; w = x +y + SECRET; } else { x = 12; y = 4; w = x +y + SECRET; } cout << "w = " << w << endl; return 0; }
The following program is supposed to read the length and width of a rectangle from a file named input.txt and write the area and perim- eter of the numbers to a file named output.txt. However, it fails to do so. Rewrite the program so that it accomplishes what it is intended to do. (Also, include statements to close the files.) #include <iostream> using namespace std; int main() { double length, width; ofstream outfile; infile.open("input.txt"); infile >> length >> width; outfile << "Area = " << length * width << ", Perimeter = " << 2 * (length + width) << endl; return 0; }
#include <iostream> #include <fstream> using namespace std; int main() { int num1, num2; ifstream infile; ofstream outfile; infile.open("input.dat"); outfile.open("output.dat"); infile >> num1 >> num2; outfile << "Sum = " << num1 + num2 << endl; infile.close(); outfile.close(); return 0; }
Rewrite the following program so that it is properly formatted. #include <iostream> #include <string> using namespace std; const double X = 13.45;const int Y=18; const char STAR= '*'; int main() {string employeeID;string department; int num; double salary; cout<<"Enter employee ID: "; cin>> employeeID; cout<<endl; cout<<"Enter department: "; cin >>department; cout<<endl; cout<<"Enter a positive integer less than 80:"; cin>>num;cout<<endl; salary=num*X; cout<<"ID: "<< employeeID <<endl; cout<<"Department "<<department<<endl;cout <<"Star: "<< STAR<<endl; cout<<"Wages: $"<<salary<<endl; cout<<"X = "<<X<<endl; cout<<"X+Y = " << X+Y << endl; return 0; }
#include <iostream> #include <string> using namespace std; const double X = 13.45; const int Y = 18; const char STAR = '*'; int main() { string employeeID; string department; int num; double salary; cout << "Enter employee ID: "; cin >> employeeID; cout << endl; cout << "Enter department: "; cin >> department; cout << endl; cout << "Enter a positive integer less than 80: "; cin >> num; cout << endl; salary = num * X; cout << "ID: " << employeeID << endl; cout << "Department " << department << endl; cout << "Star: " << STAR << endl; cout << "Wages: $" << salary << endl; cout << "X = " << X << endl; cout << "X + Y = " << X + Y << endl; return 0;
The following program has syntax errors. Correct them. On each suc- cessive line, assume that any preceding error has been corrected. const char = STAR = '*' const int PRIME = 71; int main { int count, sum; double x; count = 1; sum = count + PRIME; x = 25.67 newNum = count * ONE + 2; sum + count = sum; (x + sum)++; x = x + sum * COUNT; sum += 3--; cout << " count = " << count << ", sum = " << sum << ", PRIME = " << Prime << endl; }
#include <iostream> using namespace std; const char STAR = '*'; const int PRIME = 71; int main() { int count, sum; double x; int newNum; //declare newNum count = 1; sum = count + PRIME; x = 25.67; // x = 25.67; newNum = count * 1 + 2; //newNum = count * ONE + 2; sum++; //(x + sum)++; sum = sum + count; //sum + count = sum; x = x + sum * count; // x = x + sum * COUNT; sum += 3; //sum += 3--; cout << " count = " << count << ", sum = " << sum << ", PRIME = " << PRIME << endl; return 0; }
Describe the steps required by the problem-solving process.
(1) Analyze and outline the problem and its solution requirements, and design an algorithm to solve the problem. (2) Implement the algorithm in a programming language, such as C++, and verify that the algorithm works. (3) Maintain the program by using and modi- fying it if the problem domain changes
What if 5 is replaced with a 0 in the for loop control expression?
0
The extraction operator takes _____ operands.
2
The equality operator made of up ______ equal sign(s) and the assignment operator is mode up of _________ equal sign(s)
2 1
What is the output of the following program? #include <iostream> using namespace std; int main() { int first = 16; int second = 8; if ((first / second == 2) || (second / first == 3)) { second = 10; first = 20; } else if ((first % second == 2 || second % first == 1)) { second = 15; first = 5; } else { first = -10; second = -20; } cout << first << " " << second << endl; return 0; }
20 10
The final value of s is ________ . int s = 0; int iter; for (iter = 0; iter < 5; ++iter) { s = 2 * s + 1; cout << s << " "; } cout << endl;
31
The output of the following code segment is _________ ? int count = 5 while (count++ <= 5) cout << count << " "; cout << "." << endl;
6
In C++, __________ is the stream insertion operator.
<<
What is linking?
In linking, an object program is combined with other programs in the library, used in the program, to create the executable code.
What is programming?
Programming is a process of problem solving
__________ programming consists of classes of objects, each specifying the data and operations associated with an object type.
Object-oriented
Correct the following code so that it prints the correct message. if (score <= 60) cout << "Pass" << endl; else; cout << "Fail" << endl;
Omit the semicolon after else. The correct statement is: if (score >= 60) cout << "Pass" << endl; else cout << "Fail" << endl;
__________ programming divides a problem into subproblems which are then solved by specifying a series of computational steps to be carried out.
Procedural
You are given a list of students' names and their test scores. Design an algorithm that does the following: a. Calculates the average test scores. b. Determines and prints the names of all the students whose test scores are below the average test score. c. Determines the highest test score. d. Prints the names of all the students whose test scores are the same as the highest test score. (Each of the parts a, b, c, and d must be solved as a subproblem. The main algorithm combines the solutions of the subproblems.)
Suppose averageTestScore denotes the average test score, highestScore denotes the highest test score, testScore denotes a test score, sum denotes the sum of all the test scores, count denotes the number of students in class, and studentName denotes the name of a student. a.First you design an algorithm to find the average test score. To find the average test score, first you need to count the number of students in the class and add the test score of each student. You then divide the sum by count to find the average test score. The algorithm to find the average test score is as follows: i. Set sum and count to 0. ii. Repeat the following for each student in class. 1. Get testScore 2. Increment count and update the value of sum by adding the current test score to sum. iii. Use the following formula to find the average test score. if (count is 0) averageTestScore = 0; otherwise averageTestScore = sum / count; b. The following algorithm determines and prints the names of all the students whose test score is below the average test score. Repeat the following for each student in class: i. Get studentName and testScore ii. if (testScore is less than averageTestScore) print studentName c. The following algorithm determines the highest test score. i. Get first student's test score and call it highestTestScore. ii. Repeat the following for each of the remaining students in the class 1. Get testScore 2. if (testScore is greater than highestTestScore) highestTestScore = testScore; d. To print the names of all the students whose test score is the same as the highest test score, compare the test score of each student with the highest test score and if they are equal print the name. The following algorithm accomplishes this. e. Repeat the following for each student in the class: i.Get studentName and testScore ii. if (testScore is equal to highestTestScore) print studentName You can use the solutions of the subproblems obtained in parts a to d to design the main algorithm as follows: 1. Use the algorithm in part a to find the average test score. 2. Use the algorithm in part b to print the names of all the students whose score is below the average test score. 3. Use the algorithm in part c to find the highest test score. 4. Use the algorithm in part d to print the names of all the students whose test score is the same as the highest test score.
Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling is free; otherwise, the shipping and handling is $10 per item. Design an algorithm that prompts Jason to enter the number of items ordered and the price of each item. The algorithm then out- puts the total billing amount. Your algorithm must use a loop (repeti- tion structure) to get the price of each item. (For simplicity, you may assume that Jason orders no more than five items at a time.)
Suppose that billingAmount denotes the total billing amount, numOfItemsOrdered denotes the number of items ordered, shippingAndHandlingFee denotes the shipping and handling fee, and price denotes the price of an item. The following algorithm computes and outputs the billing amount. a. Enter the number of items bought. b. Get numOfItemsOrdered c. billingAmount = 0.0; d. shippingAndHandlingFee = 0.0; e. Repeat the following for each item bought. I. Enter the price of the item ii. Get price iii. billingAmount = billingAmount + price; f. if billingAmount < 200 shippingAndHandlingFee = 10 * numOfItemsOrdered; g. billingAmount = billingAmount + shippingAndHandlingFee h. Print billingAmount
The volume of a sphere is (4.0/3.0)pr3 and the surface area is 4.0pr2, where r is the radius of the sphere. Given the radius, design an algo- rithm that computes the volume and surface area of the sphere. Also using the C11 statements provided for Example 1-1, write the C11 statement corresponding to each statement in the algorithm. (You may assume that p 5 3.141592.)
Suppose that radius denotes radius of the sphere, volume denotes volume of the sphere, and surfaceArea denotes the surface area of the sphere. The following algorithm computes the volume and surface area of the sphere. Algorithm 1. Get the radius. 2. Calculate the volume. 3. Calculate the surface area. C++ Instruction (Code) 1. cin >> radius; 2. volume = (4.0 / 3.0) * 3.1416 * radius * radius * radius; 3. surfaceArea = 4.0 * 3.1416 * radius * radius;
Design an algorithm to find the real roots of a quadratic equation of the form ax2 1 bx 1 c 5 0, where a, b, and c are real numbers, and a is nonzero
Suppose x1 and x2 are the real roots of the quadratic equation. a. Get a b. Get b c. Get c d. if (b * b - 4 * a * c < 0) Print "The equation has no real roots." Otherwise { temp = b * b - 4 * a * c; x1 = (-b + temp) / (2 * a); x2 = (-b - temp) / (2 * a); }
What is the purpose of the manipulator setprecision? Which header file must be included to use the function setprecision?
The manipulator scientific is used to output floating-point num- bers in scientific format. To use this function the program must include the header file iomanip
What is the purpose of the manipulator setw? Which header file must be included to use the function setw?
The manipulator setw is used to output the value of an expression in a specific number of columns. To use this function the program must include the header file iomanip
If the number of items bought is less than 5, then the shipping charges are $7.00 for each item bought; if the number of items bought is at least 5, but less than 10, then the shipping charges are $3.00 for each item bought; if the number of items bought is at least 10, there are no shipping charges. Correct the following code so that it computes the correct shipping charges. if (numOfItemsBought > 10) shippingCharges = 0.0; else if (5 <= numOfItemsBought || numOfItemsBought <= 10); shippingCharges = 3.00 * numOfItemsBought; else if (0 < numOfItemsBought || numOfItemsBought < 5) shippingCharges = 7.00 * numOfItemsBought;
The correct code is: if (numOfItemsBought > 10). shippingCharges = 0.0; else if (5 <= numOfItemsBought && numOfItemsBought <= 10) shippingCharges = 3.00 * numOfItemsBought; else if (0 < numOfItemsBought && numOfItemsBought < 5) shippingCharges = 7.00 * numOfItemsBought;
Suppose that name is variable of type string. What is the effect of the following statement? getline(cin, name);
The function getline reads until it reaches the end of the current line. The newline character is also read but not stored in the string variable.
What does function pow do? Which header file must be included to use the function pow? (
The function pow calculates xy in a program. That is, pow(x, y) = xy. To use this function the program must include the header file cmath
Are the identifiers quizNo1 and quizno1 the same?
The identifiers quizNo1 and quizno1 are not the same. C++ is case sensitive. The fifth letter of quizNo1 is uppercase N while the fifth character of quizno1 is lowercase n. So these identifiers are different
Given the radius, in inches, and price of a pizza, design an algorithm to find the price of the pizza per square inch.
To find the price per square inch, first we need to find the area of the pizza. Then we divide the price of the pizza by the area of the pizza. Let radius denote the radius and area denote the area of the circle, and price denote the price of pizza. Also, let pricePerSquareInch denote the price per square inch. a. Get radius b. area = p * radius * radius c. Get price d. pricePerSquareInch = price / area
Design an algorithm to find the weighted average of four test scores. The four test scores and their respective weights are given in the following format: testScore weightTestScore ...
To find the weighted average of the four test scores, first you need to know each test score and its weight. Next, you multiply each test score with its weight, and then add these numbers to get the average. Therefore, 1. Get testScore1, weightTestScore1 2. Get testScore2, weightTestScore2 3. Get testScore3, weightTestScore3 4. Get testScore4, weightTestScore4 5. weightedAverage = testScore1 * weightTestScore1 + testScore2 * weightTestScore2 + testScore3 * weightTestScore3 + testScore4 * weightTestScore4;
Suppose a, b, and c are int variables and a = 7 and b = 2. What value is assigned to each variable after each statement executes? If a variable is undefined at a particular statement, report UND (undefined). a = ++b + 5; c = a + b-- + 1; b = (a++) - (--c); a ---- ---- ---- b ---- ---- ---- c ---- ---- ----
a = (b++) + 3; c = 2 * a + (++b); b = 2 * (++c) - (a++); a 8 8 9 b 3 2 -3 c und 12 11
Which of the following are correct C++ statements? a. cout << "Programming with C++!" << endl; b. cout << " Programming " << " with " << " C++" << endl; c. cout << " Programming " << " with C++!" << '\n'; d. cout << "Programming with C++! ' << endl;
a and c are correct.
Which of the following arithmetic operators can you use only with the integral data type? a. * b. % c. / d. +
b. %
Rewrite the following expressions using the conditional operator. (Assume that all variables are declared properly.) a. if (x == y) z = x + y; else z = (x + y) / 2; b. if (hours >= 40.0) wages = 40 * 7.50 + 1.5 * 7.5 * (hours - 40); else wages = hours * 7.50; c. if. (loanAmount >= 200000) closingCosts = 10000; else closingCosts = 8000;
a. (x == y) ? z = x + y : (x + y) / 2; b. (hours >= 40.0) ? wages = 40 * 7.50 + 1.5 * 7.5 * (hours - 40) : wages = hours * 7.50; c. (loanAmount >= 200000) ?closingCosts = 10000 : closingCosts = 8000;
What is the output of the following statements? a. if ('+' < '-') cout << "+-"; cout << "-+" << endl; b. if (12 / 2 == 4 + 1) cout << "6 "; cout << "12 / 2 != 4 + 1" << endl; c. if ('*' >= '/') cout << "/"; cout << "*"; cout << endl; d. if ("C++" <= "++C") cout << "C++" << endl; cout << "C++" << endl; e. if ( "low" <= "high") cout << "low" << endl; cout << "high" << endl;
a. +--+ b. 12 / 2 != 4 + 1 c. *d. C++ C++ e. low high
Suppose that beta is an int variable. Consider the following C++ code. cin >> beta; switch (beta % 10) { case 0: case 1: beta = beta - 2; break; case 2: case 8: beta += beta; break; case 4: case 7: beta = static_cast<int> (pow(beta, 3.0)); case 6: case 9: beta = static_cast<int> (sqrt(beta * 1.0)); break; default: beta = -5; } If the input is 1, beta is _______ after execution. If the input is 23, beta is ______ after execution. If the input is 89, beta is ______ after execution. If the input is 4, beta is _______ after execution. If the input is 8, beta is _______after execution. If the input is 36, beta is ______ after execution
a. -1 b. -5 c. 9 d. 8 e. 16 f. 6
The following loop iterates _____ times. int count = 1; while (count < 1) ++count; a. 0 b. 1 c. 2 d. infinite
a. 0
Suppose that you have the following conditional expression. (Assume that all the variables are properly declared.) (0 < backyard && backyard <= 5000) ? fertilizingCharges = 40.00 : fertilizingCharges = 40.00 + (backyard - 5000) * 0.01; a. What is the value of fertilizingCharges if the value of backyard is 3000? b. What is the value of fertilizingCharges if the value of backyard is 5000? c. What is the value of fertilizingCharges if the value of backyard is 6500?
a. 40.00 b. 40.00 c. 55.00
If int x = 10;, int y = 7;, double z = 4.5;, and double w = 2.5;, evaluate each of the following statements, if possible. If it is not possible, state the reason. a. (x + y) % x b. x % y + w c. (z - y) / w d. (y + z) % x e. (x % y) * z f. x % y % 2 g. (x + y) % z h. (x % y + z) / w
a. 7 b. 5.50 c. −1.00 d. Not possible. Both the operands of the operator % must be inte- gers. y + z is of type double. Both operands, y + z and x, of % must be integers. e. 13.50 f. 1 g. Not possible. Both the operands of the operator % must be inte- gers. Because the second operand, z, is a floating-point value, the expression is invalid. h. 3.00
Write each of the following as a C++ expression. a. 9.0 divided by 5 times C plus 32. b. The integer value of the character +. c. Round the decimal number x to the nearest integer. d. Assign the string C++ Programming is exciting to the variable str. e. Assign the value of 12 times feet plus inches to totalInches. f. Increment the value of the int variable i by 1. g. v = 4/3(3.1416r^3). h. s = 2(3.1416r^2) + 2(3.1416r)h. I. a +(b-c)/d(ef - gh) j. (-b + (b^2- 4ac)) / 2a
a. 9.0 / 5 * C + 32 b. static_cast<int>('+') c. static_cast<int>('+') d. static_cast<int>(x + 0.5) str = "C++ Programming is exciting" e. totalInches = 12 * feet + inches f. i++, ++i, or i = i + 1; g. v= 4 / 3 * (3.1416 * r * r * r); h. s= 2 * (3.1416 * r * r) + 2 * (3.1416 * r) * h; I. a+ (b - c) / d * (e * f - g * h) j. (-b + (b * b - 4 * a * c)) / (2 * a)
What is the output of the following statements? a. if ('K' > '%' || '@' <= '?') cout << "?%"; cout << "!!"; cout << endl; b. if ('8' < 'B' && 15 > -13) cout << "a b c d" << endl; cout << "##" << endl; c. if ("Fly" >= "Flying" && "Programming" >= "Coding") cout << "Fly Programming" << endl; cout << "Flying Coding" << endl;
a. ?%!! b. a b c d ## c. Flying Coding
Suppose that score is an int variable. Consider the following if statements. if (score >= 90); cout << "Discount = 10%" << endl; a. What is the output if the value of score is 95? Justify your answer. b. What is the output if the value of score is 85? Justify your answer.
a. bonus = 0.0; The output is: Discount = 10%. The semicolon at the end of the if statement terminates the if statement. So the cout statement is not part of the if statement. The cout statement will execute regardless of whether the expression in the if state- ment evaluates to true or false. b. The output is: Discount = 10%. The semicolon at the end of the if statement terminates the if statement. So the cout statement is not part of the if statement. The cout statement will execute regardless of whether the expression in the if statement evaluates to true or false.
In C++, the symbol ==, which consists of two equal signs, is called the _______ operator. a. equality b. assignment c. comparison d. relational e. logical
a. equality
Mark the following statements as true or false. a. The order in which statements execute in a program is called the flow of control. b. In C++, =, is the equality operator. c. In a one-way selection, if a semicolon is placed after the expression in an if statement, the expression in the if statement is always true. d. Every if statement must have a corresponding else. e. The expression in the if statement: if (score = 30) grade = 'A'; always evaluates to true. f. The expression: (2, 5) (ch >= 'A' && ch <= 'Z') evaluates to false if either ch < 'A' or ch >= 'Z'. g. Suppose the input is 5. The output of the code: (3) cin >> num; if (num > 5) cout << num; num = 0; else cout << "Num is zero" << endl; is: Num is zero h. The result of a logical expression cannot be assigned to an int variable. i. The expression !(x > 0) is true only if x is a negative number. j. In C++, both ! and != are logical operators. k. The expression in a switch statement should evaluate to a value of the simple data type.
a. false; b. false; c. false; d. false; e. true; f. false; g. true; h. false; i. false; j. false; k. false;
Mark the following statements as true or false. a. An identifier must start with a letter and can be any sequence of characters. b. In C++, there is no difference between a reserved word and a predefined identifier. c. A C++ identifier cannot start with a digit. d. The collating sequence of a character is its preset number in the character data set. e. Only one of the operands of the modulus operator needs to be of type int. f. If a = 4; and b = 3;, then after the statement a = b; the value of b is erased. g. If the input is 7 and x is a variable of type int, then the statement cin >> x; assigns the value 7 to x. h. In an output statement, the newline character may be a part of the string. I. In C++, all variables must be initialized when they are declared. j. In a mixed expression, all the operands are converted to floating- point numbers. k. Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6. l. Suppose a = 5. After the statement ++a; executes, the value of a is still 5 because the value of the expression is not saved in another variable.
a. false; b. false; c. true; d. true; e. false; f. false; g. true; h. true; i. false; j. false; k. true; l. false
Suppose that x, y, and z are int variables and x = 13, y = 45, and z = 20. Determine whether the following expressions evaluate to true or false. a. !(x - y <= 10) b. x + y > 10 || z < 15 c. (x != y) && (x <= z) d. (y - x >= z) || (2 * z <= y - x)
a. false; b. true; c. true; d. true;
The following loop iterates _____ times. int count = 1; while (count < 1) --count; a. infinite b. 2 c. 1 d. 0
a. infinite
Write C++ statements to do the following. a. Declare int variables num1 and num2. b. Prompt the user to input two integers. c. Input the first number in num1 and the second number in num2. d. Output num1, num2, and 2 times num1 minus num2. Your output must identify each number and the expression
a. int num1; int num2; b. cout << "Enter two numbers separated by spaces." << endl; c. cin >> num1 >> num2; d. cout << " num1 = " << num1 << ", num2 = " << num2 << ", 2 * num1 - num2 = " << 2 * num1 - num2 << endl;
Suppose int1 and int2 are int variables and dec1 and dec2 are double variables. Assume the following input data: (2) 56.50 67 48 62.72 What value (if any) is assigned to int1, int2, dec1, and dec2 after each of the following statements executes? (Use the same input for each statement.) a. cin >> dec1 >> int1 >> int2 >> dec2; b. cin >> int1 >> dec1 >> dec2 >> int1; c. cin >> dec1 >> dec2 >> int1 >> int2; d. cin >> int1 >> dec1 >> int2 >> dec2; e. cin >> int1 >> int2 >> dec1 >> dec2
a. int1 = 67, int2 = 48, dec1 = 56.5, dec2 = 62.72 b. int1 = 48, int2 = -1, dec1 = 0.5, dec2 = 67 c. a. int1 = 48, int2 = 62, dec1 = 56.5, dec2 = 67 d. int1 = 56, int2 = 67, dec1 = 0.5, dec2 = 48 e. Inout failure: int1 =56; trying to read the . (period) into int2
Suppose that height is a double variable and name is a string variable. What are the values of height and name after the following input statements execute: cin >> height; getline(cin, name); if the input is: a. 5.4 Christy Miller b. 5.4 Christy Mille
a. name = " Christy Miller", height = 5.4 b. name = " ", height = 5.4
Suppose that you have the following statements: ofstream outfile; int numOfJuiceBottlesSold = 35; double costOfaJuiceBottle = 0.75; double revenue; Use this information and write C11 statements to do the following: a. Open the file sales.dat using the variable outfile. b. Write the statement to format the output in the outfile to two decimal places in fixed form. c. Calculate and store the revenue generated by selling the juice bottles in the variable revenue. d. Write the values of the variables numOfJuiceBottlesSold and costOfaJuiceBottle, and revenue in the file sales.dat. e. Write a statement to close the file sales.dat
a. outfile.open("sales.dat "); b. outfile >> fixed >> showpoint >> setprecision(2); c. revenue = numOfJuiceBottlesSold * costOfaJuiceBottle; d. outfile >> numOfJuiceBottlesSold >> " " >> costOfaJuiceBottle >> " " >> revenue >> endl; e. outfile.close
Which of the following is not a reserved word in C++? a. int b. include c. double d. const e. cin f. bool
b, e
Mark the following statements as true or false. a. An output stream is a sequence of characters from a computer to an output device. b. To use cin and cout in a program, the program must include the header file iostream. c. Suppose pay is a variable of type double. The statement cin >> pay; requires the input of a decimal number. d. The statement cin >> length; and length >> cin; are equivalent. e. When the statement cin >> num1 >> num2; executes, then after inputting a number into the variable num1 the program skips all trailing whitespace characters. f. To use the predefined function sqrt in a program, the program must include the header file cmath. g. The statement cin.get(ch); inputs the next nonwhitespace character into the variable ch. h. When the input stream enters the fail state, the program terminates with an error message. I. To use the manipulators fixed and showpoint, the program does not require the inclusion of the header file iomanip. j. The statement cin >> right; sets the input of only the next vari- able right-justified. k. To input data from a file, the program must include the header file fstream.
a. true; b. true; c. false; d. false; e. false; f. true; g. false; h. false; i. true; j. false; k. true
Mark the following statements as true or false. a. The calculating device called the Pascaline could calculate sums up to eight figures long. b. All programs must be loaded into the CPU before they can be exe- cuted and all data must be loaded into main memory before it can be manipulated. c. Main memory is an ordered sequence of cells and each cell has a random location in main memory. d. The program that loads first when you turn on your computer is called the operating system. e. Analog signals represent information with a sequence of 0s and 1s. f. The machine language is a sequence of 0s and 1s. g. A binary code is a sequence of 0s and 1s. h. A sequence of eight bits is called a byte. I. One GB is 220 MB. j. In ASCII, A is the 65th character. k. The number system used by a computer is base 2. l. An assembler translates the assembly language instructions into machine language. m. A compiler translates the source program into an object program. n. In a C11 program, statements that begin with the symbol # are called preprocessor directives. o. An object program is the machine language version of a high-level language program. p. All logical errors, such as division by 0, are reported by the compiler. q. In object-oriented design (OOD), a program is a collection of interacting objects. r. An object consists of data and operations on that data. s. ISO stands for International Organization for Standardization.
a. true; b. false; c. false; d. true; e. false; f. true; g. true; h. true; i. false; j. false; k. true; l. true; m. true; n. true; o. true; p. false; q. true; r. true; s. true
Mark the following statements as true or false. a. A loop is a control structure that causes certain statements to execute over and over. b. In a counter-controlled while loop, it is not necessary to initialize the loop control variable. c. It is possible that the body of a while loop may not execute at all. d. In an infinite while loop, the while expression (the decision maker) is initially false, but after the first iteration it is always true. e. The while loop: j = 0; while (j <= 10) j++; terminates if j > 10. f. A sentinel-controlled while loop is an event-controlled while loop whose termination depends on a special value. g. To read data from a file of an unspecified length, an EOF-controlled loop is a good choice. h. When a while loop terminates, the control first goes back to the statement just before the while statement, and then the control goes to the statement immediately following the while loop. I. A do...while loop is called a pretest loop. j. Executing a break statement in the body of a loop immediately terminates the loop.
a. true; b. false; c. true; d. false; e. true; f. true; g. true; h. false; i. false; j. true;
Write equivalent compound statements if possible. a. x = x + 5; b. x = 2 * x * y; c. totalPay = totalPay + currentPay; d. z = z * x + 2 * z; e. y = y / (x + 5);
a. x += 5; b. x *= 2 * y c. totalPay += currentPay; d. z *= (x + 2); e. y /= x + 5;
Suppose that x, y, z, and w are int variables and x = 25, y = 3, z = 12, and w = 4. What is the output of the following statements? a. cout << "x == z: " << (x == z) << endl; b. cout << "y != z - 9: " << (y != z - 9) << endl; c. cout<<"x-y==z+10:"<<(x-y==z+10)<<endl; d. cout << "!(z < w): " << !(z < w) << endl; e. cout << "w - y < x - 2 * z: " << (w - y < x - 2 * z) << endl;
a. x == z: 0 b. y != z - 9: 0 c. x - y == z + 10: 1 d. !(z < w): 1 e. w - y < x - 2 * z: 0
Suppose a and b are int variables, c is a double variable, and a = 25, b = 20, and c = 5.0. What is the output of the following state- ments? a. cout << a * 2 * b << endl; b. cout << a + b / 2.0 + 1.5 * c << endl; c. cout << a / static_cast<double>(b) << endl; d. cout << 62 % 28 + a / c << endl; e. cout << static_cast<int>(c) % 3 + 7 << endl; f. cout << 22.5 / 2 + 14.0 * 3.5 + 28 << endl; g. cout << 2 / (c - static_cast<int>(c + 1.2))<< endl;
a.1000 b.42.50 c. 1.25 d. 11.00 e. 9 f. 88.25 g. −2.00
Suppose that num is an int variable. Consider the following C11 code. cin >> num; i f (num >= 0) switch (num % 3) { case 0: num--; break; case 1: ++num; break; case 2: num = static_cast<int>(pow(num, 3.0)); break; } else num = -num; num = static_cast<int>(sort(num * 1.0)); { } cout << num << endl; a. What is the output if the input is 7? b. What is the output if the input is 23? c. What is the output if the input is 20? d. What is the output if the input is -9?
a.7 b.12167 c.8000 d.3
Suppose that alpha is an int variable. Consider the following C++ code. cin >> alpha; switch (alpha % 9) { case 0: case 3: alpha = alpha / 3; break; case 1: case 5: case 7: alpha = alpha / 2; break; case 2: case 4: ++alpha; break; case 6: alpha = (alpha / 9) * (alpha / 9); break; case 8: alpha = (alpha % 9) * (alpha % 9); break; default: alpha--; } cout << alpha << endl; } a. What is the output if the input is 16? b. What is the output if the input is 8? c. What is the output if the input is 1? d. What is the output if the input is 25?
a.8 b.64 c.1 d.12
If the possible range of values for a multiple selection statement cannot be reduced to a finite set of values, you must use the ___________ structure. a. if b. if ... else c. switch d. nested if e. loop
b. if ... else
The value of the expression in a switch statement must be a(n) a. string b. integral c. floating-point d. integral or floating-point
b. integral
The operator ! is ______ , so it only has one operand. a. relational b. unary c. binary d. ternary
b. unary
A ___________ statement causes an immediate exit from the switch structure.
break
When the _________ statement executes in a repetition structure, it immediately exits from the structure.
break
What is printed by the following program? Suppose the input is: (8, 10, 11) 35 10.5 27 B #include <iostream> using namespace std; const int NUM = 10; const double X = 20.5; int main() { int firstNum, secondNum; double z; char grade; firstNum = 62; cout << "firstNum = " << firstNum << endl; cout << "Enter three numbers: "; cin >> firstNum >> z >> secondNum; cout << endl; cout << "The numbers you entered are " << firstNum << ", " << z << ", and " << secondNum << endl; z = z - X + 2 * firstNum - secondNum; cout << "z = " << z << endl; cout << "Enter grade: "; cin >> grade; cout << endl; cout << "The letter that follows your grade is: " << static_cast<char>(static_cast<int>(grade) + 1) << endl; return 0; }
firstNum = 62 Enter three numbers: 35 10.5 27 The numbers you entered are 35, 10.5, and 27 z = 33 Enter grade: B The letter that follows your grade is: C
To force floating-point output to show exactly as many digits after the decimal point as specified by the precision, use the _________ manipulator.
fixed
Which header file needs to be included in a program that uses the data types ifstream and ofstream?
fstream
C++ uses short-circuit evaluation for logical expressions. Therefore, the value of x is after the following poorly-written code is executed. int x = 5; int y = 1; if(y < 1 && x = 2) { x++; } else if (y > 0 && x = 50) { --x; } a. 1 b. 2 c. 3 d. 4 e. 5 f. 6 g. 49 h. 50 i. 51
g. 49
To read a string containing blanks, you can use the function
getline
Suppose that sale and bonus are double variables. Write an if...else statement that assigns a value to bonus as follows: If sale is greater than $20,000, the value assigned to bonus is 0.10, that is 10%; If sale is greater than $10,000 and less than or equal to $20,000, the value assigned to bonus is 0.05, that is 5%; otherwise the value assigned to bonus is 0, that is 0%.
if (sale > 20000) bonus = 0.10 else if (sale > 10000 && sale <= 20000) bonus = 0.05; else bonus = 0.0;