Machado Computer Science Final Test Review
Which of the following will cause a logical error if you are attempting to compare x to 5? a. if (x = 5) b. if (x >= 5) c. if (x <= 5) d. if (x == 5)
a
Consider the following program: #include <iostream> using namespace std; int main () { int x, y, z; // three input values int a, b; // two computed values for output // Input three values cout << "Enter three numbers: "; cin >> x >> y >> z; // Calculate output values a = x + y * z; b = a + y / z; // Output the results cout << a << " " << b << endl; return 0; } For the same program as in the previous question, if the input values are 1, 3, and 4, in that order, the computed values output by the last cout statement will be [a] and [b].
a = 13 b = 13
What does <= mean? a. less than or equal to b. less than c. greater than or equal to d. greater than
a
Complete the program below. The program should open a file called numbers.txt that contains three integers, read those three values into variables x, y, and z, the output the sum. Use the name infile as the name of your file variable. #include <iostream> [ans1] using namespace std; int main () { int x, y, z; // Three variables for input int sum; // Sum of x, y, and z [ans2]; // For reading from the file // Open the file [ans3]; // Read three integers from the file into x, y, and z [ans4]; // Calculate and output the sum sum = x + y + z; cout << "The sum is " << sum << "." << endl; // Close the file [ans5]; return 0; }
1: #include <fstream> 2: ifstream infile 3: infile.open ("numbers.txt") 4: infile >> x >> y >> z 5: infile.close ( )
The program below has several syntax errors. Each line with an error is indicated by a comment. For each line with an error, enter the complete corrected line (you may leave out the comment portion of the line). #include <iostearm> // [error1] using namespace std; int main () { int a, b, c; int sum // [error2] cout << "Enter three numbers: "; cin << a << b << c; // [error3] sum = a + b + c; cout << The sum is << sum << endl; // [error4] return 0; // [error5]
1: #include <iostream> 2: int sum; 3: cin >> a >> b >> c; 4: cout << "The sum is " << sum << endl; 5: }
Evaluate each of the following arithmetic expressions (as they would be evaluated in C++). Suppose variables x, y, and z are declared as int and have values 8, 5, and 3 respectively. x + y * 3 [result1] (x + 2) - y - z [result2] (x + 2) - (y - z) [result3] y / 2 [result4] x / 2 * 3 [result5]
1: 23 2: 2 3: 8 4: 2 5: 12
Evaluate each of the following Boolean expressions (as they would be evaluated in C++). Suppose variables x, y, and z are declared as int and have values 8, 5, and 3 respectively. Please enter your response for each expression as either T for true or F for false (one letter response only, do not use the full word). x < y [ans01] x <= y + 3 [ans02] x == y + 2 [ans03] x != y + z [ans04] x > y || z < y [ans05] x > y && z < y [ans06] x > 0 && y > 0 && z > 0 [ans07] (x < y) || (y > x && z > y) [ans08] !(x > y) [ans09] x < y < z [ans10]
1: F 2: T 3: F 4: F 5: T 6: T 7: T 8: F 9: F 10: T
Consider the following program: #include <iostream> using namespace std; int main ( ) { int a, b, c; cout << "Enter 3 integers: "; cin >> a >> b >> c; if (a + b < 10) cout << "cat "; if (a + b <= 10) cout << "bird "; else cout << "dog "; if (a < b) cout << "giraffe "; else if (a < c) cout << "llama "; else cout << "zebra "; cout << endl; return 0; } Note that the program produces one line of output with animal species separated by spaces. What is the output of this program for the following inputs? If input is 3 5 2, output is [ans1] If input is 2 3 5, output is [ans2] If input is 5 3 2, output is [ans3] If input is 7 5 8, output is [ans4]
1: cat bird giraffe 2: cat bird giraffe 3: cat bird zebra 4: dog llama
Complete the Boolean expressions such that the output would be as described. (Note that "positive" and "negative" do not include zero, and "non-negative" includes zero and all positive numbers.) #include <iostream> using namespace std; int main ( ) { int x, y; cout << "Enter x and y: "; cin >> x >> y; if ([ans1]) cout << "Both x and y are positive." << endl; if ([ans2]) cout << "At least one of x and y is negative." << endl; if ([ans3]) cout << "The sum of x and y is non-negative." << endl; if ([ans4]) cout << "x is positive and y is negative." << endl; if ([ans5]) cout << "x and y are not equal to each other." << endl; return 0; }
1: x > 0 && y > 0 2: x < 0 || y < 0 3: x + y >= 0 4: x > 0 && y < 0 5: x != y
Consider the following program code: int main ( ) { int num; cout << "Enter a number: "; cin >> num; while (num < 10) { cout << num << endl; num = num + 3; } cout << "Done." << endl; return 0; } How many times does the body of the while loop execute... ... if the input value is 3? [answer_for_3] ... if the input value is 4? [answer_for_4] ... if the input value is 11? [answer_for_11]
3: 3 4: 2 11: 0
Consider the following program code: int main () { int num; cout << "Enter a number: "; cin >> num; do { cout << num << endl; num = num + 3; } while (num < 10); cout << "Done." << endl; return 0; } How many times does the body of the while loop execute... ... if the input value is 3? [answer_for_3] ... if the input value is 4? [answer_for_4] ... if the input value is 11? [answer_for_11]
3: 3 4: 2 11: 1
A comma is also called a statement terminator. True / False
False
Arrays can be passed as parameters to a function by value, but it is faster to pass them by reference. True / False
False
Given the declaration int list[20]; the statement list[12] = list[5] + list[7]; updates the content of the twelfth component of the array list. True / False
False
If an array index goes out of bounds, the program always terminates in an error. True / False
False
If input failure occurs in a C++ program, the program terminates immediately and displays an error message. True / False
False
If the formal parameter list of a function is empty, the parentheses after the function name are not needed. True / False
False
Suppose list is a one dimensional array of size 25, wherein each component is of type int. Further, suppose that sum is an int variable. The following for loop correctly finds the sum of the elements of list. sum = 0; for (int i = 0; i < 25; i++) sum = sum + list; True / False
False
The escape sequence \r moves the insertion point to the beginning of the next line. True / False
False
The execution of a return statement in a user-defined function terminates the program. True / False
False
The result of a logical expression cannot be assigned to an int variable, but it can be assigned to a bool variable. True / False
False
The statement int list[25]; declares list to be an array of 26 components, since the array index starts at 0. True / False
False
The devices that the computer uses to display results are called ____ devices. a. output b. exit c. input d. entry
a
Suppose a program is processing a file that contains a list of high temperatures from various cities in the United States. In the program, the following int variables are declared: lowest, highest, and count, which will be used to find the lowest temperature in the file, the highest temperature in the file, and a count of the number of temperatures in the file. Assume that these variables are being modified inside an end-of-file controlled loop. What is an appropriate initial value for each of these variables? Initial value for lowest: Initial value for highest: Initial value for count: A. 0 B. 1 C. -200 D. 200
Lowest: d Highest: c Count: a
The program shown below is intended to add the numbers from 1 through 100 (i.e. 1 + 2 + 3 + ... + 100) and output the resulting sum. However, this program as it is written does not work. What does the program do when it is run? Briefly describe what you might do to correct the problem. #include <iostream> using namespace std; int main () { int number = 1; int sum = 0; while (number <= 100) sum = sum + number; cout << "The sum of integers 1 through 100 is " << sum << endl; return 0; }
The loop control is not present, so the program would not loop and go back to the beginning of the while loop statement. It supposed to go back to the top and keep executing until in reaches 100 and finishes adding all the numbers. Also, initializing "number" with " 1 " gives wrong numbers to be added. What i would do to fix the problem is is initialize "number" to "0", then at the end of the loop add the loop control "number++". OR As it is written, the program is in an infinite loop and produces no output. The loop control variable (number, in this case) needs to be updated within the loop. The loop could be modified to look like either of the following: while (number <= 100) { sum = sum + number; number++; } or it could be re-written as a for loop: for (number = 1; number <= 100; number++) sum = sum + number;
Consider the following program. #include <iostream> using namespace std; int main () { int score1, score2; // Two test scores, input double average; // Calculated average cout << "Enter two test scores: "; cin >> score1 >> score2; average = (score1 + score2) / 2.0; cout << "Your average is " << average << endl; if (average >= 90) cout << "Congratulations" << endl; cout << "You have an A." << endl; return 0; } The program compiles without errors. However, in testing the program you find that it seems to always tell the user they have an A. You expect that, if the user's average is 90 or above, the program should display the average, the "Congratulations!" message, and display the "A" message. If the user's average is below 90, it should only display the average. Why is the program always giving the "A" message? What change would you make to the program to get the expected behavior?
Since in the cout statements "Congratulations!" and "You have an A." aren't on the same line, the program is assuming that "You have an A." is the else statement. So whenever a person gets lower than a 90, the program probably assumes "You have an A" corresponds to any grade lower than 90, so it outputs that sentence. To change this, either put both sentences in one statement like [ cout << "Congratulations! You have an A." << endl; ] or put [ cout << "Congratulations!" << "You got an A." << endl; ]. OR The "A" message is not being controlled by the "if" statement; it is unconditional. Only the "Congratulations" message is controlled by the "if" statement. Both the "Congratulations" and "A" messages should be enclosed in a compound statement (using braces).
End of Test 3
Start of Chapter 8 Quiz
End of Test 1
Start of Test 2
End of Test 2
Start of Test 3
Consider the following program: #include <iostream> using namespace std; int main () { int score; // A test score, input cout << "Enter test score: "; cin >> score; if (score = 100) cout << "You have a perfect score!" << endl; else if (score >= 90) cout << "You have an A." << endl; else cout << "You do not have an A." << endl; return 0; } The program compiles without errors, but in testing the program you find that the program seems to always tells the user that they have a perfect score. Why is the program giving the "perfect score" message for apparently all inputs (such as 50, 75, 90, 100)? Is there any input for score that would result in something other than the "perfect score" message? If so, what is the input and the response? What correction would you make to the program to fix the problem?
The expression in the "if" statement is incorrect. The condition is an assignment statement, not a Boolean expression. An assignment statement results in the value assigned, which in this case is an integer which results in true for all values except zero. If the input to the program is 0, the program will output the message "You do not have an A." To fix the program, change the expression in the "if" statement from "score = 100" to "score == 100" (two equal signs, not one).
All components of an array are of the same data type. True / False
True
Assume all variables are properly declared. The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout << n << " "; } True / False
True
Suppose P and Q are logical expressions. The logical expression P && Q is true if both P and Q are true. True / False
True
A loop that continues to execute endlessly is called a(n) ____ loop. a. infinite b. unhinged c. end d. definite
a
A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time is called a(n) ____. a. algorithm b. analysis c. linker d. design
a
Assume you have the following declaration char nameList[100];. Which of the following ranges is valid for the index of the array nameList? a. 0 through 99 b. 0 through 100 c. 1 through 100 d. 1 through 101
a
Assume you have the following declaration double salesData[1000];. Which of the following ranges is valid for the index of the array salesData? a. 0 through 999 b. 0 through 1000 c. 1 through 1001 d. 1 through 1000
a
Functions that do not have a return type are called ____ functions. a. void b. empty c. zero d. null
a
Suppose that list is an array of 10 components of type int. Which of the following codes correctly outputs all the elements of list? a. for (int j = 0; j <= 9; j++) cout << list[j] << " "; cout << endl; b. for (int j = 1; j < 11; j++) cout << list[j] << " "; cout << endl; c. for (int j = 1; j <= 10; j++) cout << list[j] << " "; cout << endl; d. for (int j = 1; j < 10; j++) cout << list[j] << " "; cout << endl;
a
Suppose that sales is an array of 50 components of type double. Which of the following correctly initializes the array sales? a. for (int j = 0; j <= 49; j++) sales[j] = 0.0; b. for (int 1 = 1; j <= 49; j++) sales[j] = 0; c. for (int j = 1; j <= 50; j++) sales[j] = 0; d. for (int j = 0; j <= 50; j++) sales[j] = 0.0;
a
The ____ is the brain of the computer and the single most expensive piece of hardware in your personal computer. a. CPU b. ROM c. RAM d. MM
a
Consider the following program: #include <iostream> using namespace std; int main () { int x, y, z; // three input values int a, b; // two computed values for output // Input three values cout << "Enter three numbers: "; cin >> x >> y >> z; // Calculate output values a = x + y * z; b = a + y / z; // Output the results cout << a << " " << b << endl; return 0; } If the input values to this program are 4, 8, and 2, in that order, the computed values output by the last cout statement will be [a] and [b].
a = 20 b = 24
Consider the following program: #include <iostream> using namespace std; int main () { int x, y, z; // three input values int a, b; // two computed values for output // Input three values cout << "Enter three numbers: "; cin >> x >> y >> z; // Calculate output values a = x + y * z; b = a + y / z; // Output the results cout << a << " " << b << endl; return 0; } For the same program as in the previous question, if the input values are 1, 3, and 2, in that order, the computed values output by the last cout statement will be [a] and [b].
a = 7 b = 8
A variable or expression listed in a call to a function is called the ____. a. formal parameter b. actual parameter c. type of the function d. data type
b
Consider the following statement: double alpha[10][5];. The number of components of alpha is ____. a. 15 b. 50 c. 100 d. 150
b
Given the function prototype: double testAlpha(int u, char v, double t); which of the following statements is legal? a. cout << testAlpha('5.0', 'A', '2.0'); b. cout << testAlpha(5, 'A', 2); c. cout << testAlpha(5.0, "65", 2.0); d. cout << testAlpha( int 5, char 'A', int 2);
b
In C++, the null character is represented as ____. a. "\0" b. '\0' c. "0" d. '0'
b
____ loops are called posttest loops. a. for b. do...while c. while d. break
b
A collection of a fixed number of elements (called components) arranged in n dimensions (n>=1) is called a(n) ____. a. vector b. matrix c. n-dimensional array d. parallel array
c
A function prototype is ____. a. a comment line b. a declaration and a definition c. a declaration, but not a definition d. a definition, but not a declaration
c
An example of a floating point data type is ____. a. int b. char c. double d. short
c
Assume you have the following declaration int beta[50];. Which of the following is a valid element of beta? a. beta['2'] b. beta['50'] c. beta[0] d. beta[50]
c
Consider the following code. int limit; int reps = 0; cin >> limit; while (reps < limit) { cin >> entry; triple = entry * 3; cout << triple; reps++; } cout << endl; This code is an example of a(n) ____ while loop. a. flag-controlled b. sentinel-controlled c. counter-controlled d. EOF-controlled
c
Main memory is called ____. a. read and write memory b. read only memory c. random access memory d. random read only memory
c
Suppose that gamma is an array of 50 components of type int and j is an int variable. Which of the following for loops sets the index of gamma out of bounds? a. for (j = 0; j <= 49; j++) cout << gamma[j] << " "; b. for (j = 1; j < 50; j++) cout << gamma[j] << " "; c. for (j = 0; j <= 50; j++) cout << gamma[j] << " "; d. for (j = 0; j <= 48; j++) cout << gamma[j] << " ";
c
Suppose that x = 25.67, y = 356.876, and z = 7623.9674. What is the output of the following statements? cout << fixed << showpoint; cout << setprecision(2); cout << x << ' ' << y << ' ' << z << endl; a. 25.67 356.87 7623.96 b. 25.67 356.87 7623.97 c. 25.67 356.88 7623.97 d. 25.67 356.876 7623.967
c
What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 5; j++) cout << list[j] << " "; cout << endl; a. 0 1 2 3 4 b. 0 5 10 15 c. 0 5 10 15 20 d. 5 10 15 20
c
What is the value of alpha[2] after the following code executes? int alpha[5]; int j; for (j = 0; j < 5; j++) alpha[j] = 2 * j + 1; a. 1 b. 4 c. 5 d. 6
c
When one control statement is located within another, it is said to be ____. a. blocked b. closed c. nested d. compound
c
Which of the following is a reserved word in C++? a. "CHAR" b. "Char" c. "char" d. character
c
Suppose you are writing a program that involves calculating taxes owed, and the tax calculation involves a fixed tax rate of 0.12 (or 12%). You decide that, as part of your program, you would declare a constant for the tax rate. Show the C++ code for declaring this constant. (Give just the code that declares the constant; one line, not a complete program.)
const double tax rate = 0.12
Consider the following declaration: int alpha[5] = {3, 5, 7, 9, 11};. Which of the following is equivalent to this statement? a. int alpha[] = {3 5 7 9 11}; b. int alpha[5] = [3, 5, 7, 9, 11]; c. int alpha[] = (3, 5, 7, 9, 11); d. int alpha[] = {3, 5, 7, 9, 11};
d
Suppose that x and y are int variables, ch is a char variable, and the input is: 4 2 A 12 Choose the values of x, y, and ch after the following statement executes: cin >> x >> ch >> y; a. x = 4, ch = 2, y = 12 b. x = 4, ch = A, y = 12 c. x = 4, ch = ' ', y = 2 d. This statement results in input failure
d
The digit 0 or 1 is called a binary digit, or ____. a. hexcode b. bytecode c. Unicode d. bit
d
What is the output of the following C++ code? int alpha[5] = {2, 4, 6, 8, 10}; int j; for (j = 4; j >= 0; j--) cout << alpha[j] << " "; cout << endl; a. 2 4 6 8 10 b. 4 3 2 1 0 c. 8 6 4 2 0 d. 10 8 6 4 2
d
What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 1; j <= 5; j++) cout << list[j] << " "; cout << endl; a. 0 5 10 15 20 b. 5 10 15 20 0 c. 5 10 15 20 20 d. Code results in index out-of-bounds
d
What is the output of the following code? char lastInitial = 'S'; switch (lastInitial) { case 'A': cout << "section 1" <<endl; break; case 'B': cout << "section 2" <<endl; break; case 'C': cout << "section 3" <<endl; break; case 'D': cout << "section 4" <<endl; break; default: cout << "section 5" <<endl; } a. section 2 b. section 3 c. section 4 d. section 5
d
Which of the following is a relational operator? a. ! b. = c. && d. ==
d
____ is a valid char value. a. "-129" b. "A" c. 129 d. 'A'
d
The program below prompts the user for an integer. The program should then output the numbers from 1 through the number that was entered. For example, if the user enters 5, the program should output the numbers 1, 2, 3, 4, and 5, each on its own line. Complete the program by adding your code in place of the blank. int main () { int max; // Maximum number to be displayed by the program cout << "Enter maximum: "; cin >> max; cout << "The numbers from 1 through " << max << " are:" << endl; ________ // Your code to be inserted here return 0; }
int i = 1; // Loop control while ( i <= max) { cout << i << endl; i++; } OR for (int i = 1; i <= max; i++) cout << i << endl;
Write a function called twice that takes an int as a parameter and returns an int. The function should multiply its parameter value by two, then return the result. For example, if the parameter value is 5, the function should return 10. If the parameter value is 33, the function should return 66. The function might be called from the following code: int main () { int x, y; cout << "Enter a number: "; cin >> x; y = twice(x); cout << x << " doubled is " << y << endl; return 0; } Please give only the function code.
int twice ( int& x) { y = x * 2 return y; } OR int twice (int n) { return n * 2; }