Code Questions (Chapter 1-8)

3 September 2022
4.7 (114 reviews)
56 test answers

Unlock all answers in this set

Unlock answers (52)
question
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl;
answer
6
question
What will the following code display? int number = 6; number++; cout << number << endl;
answer
7
question
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;
answer
20
question
What will the following code display? int number = 6; cout << number++ << endl;
answer
6
question
What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl;
answer
5
question
Look at the following statement. while (x++ < 10) Which operator is used first?
answer
<
question
How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;
answer
21
question
What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << ' '; n++;
answer
1 1 1 ... and on forever
question
What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++;
answer
1 1 1 ... and on forever
question
What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl;
answer
3
question
What will the following code display? int number = 6; ++number; cout << number << endl;
answer
7
question
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }
answer
0 1 2 3 4
question
How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl;
answer
20
question
What will the following code display? int number = 6; cout << ++number << endl;
answer
7 (chapter 5 end)
question
What is the output of the following program? #include using namespace std; int getValue(int); int main() { int x = 2; cout << getValue(x) << endl; return 0; } int getValue(int num) { return num + 5; }
answer
7
question
Which line in the following program contains a call to the showDub function? 1 #include 2 using namespace std; 3 4 void showDub(int); 5 6 int main() 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
answer
10
question
What is the output of the following program? #include using namespace std; void doSomething(int&); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num) { num = 0; cout << num << endl; }
answer
2 0 0
question
Which line in the following program contains the prototype for the showDub function? 1 #include 2 using namespace std; 3 4 void showDub(int); 5 6 int main() 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
answer
4
question
Given the following function definition: void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code fragment that invokes calc? (All variables are of type int) x = 1; y = 2; z = 3; calc(x, y); cout << x << " " << y << " " << z << endl;
answer
163
question
What is the output of the following program? #include using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; }
answer
4 2
question
What is the output of the following program? #include using namespace std; void doSomething(int); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; cout << num << endl; }
answer
2 0 2
question
Look at the following function prototype. int myFunction(double, double, double); How many parameter variables does this function have?
answer
3
question
Which line in the following program contains the header for the showDub function? 1 #include 2 using namespace std; 3 4 void showDub(int); 5 6 int main() 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
answer
15 (end chapter 6)
question
Look at the following function prototype. int myFunction(double); What is the data type of the funtion's return value?
answer
int
question
What will the following code display? int numbers[] = { 99, 87, 66, 55, 101 }; for (int i = 1; i < 4; i++) cout << numbers[i] << endl;
answer
87 66 55
question
What will the following code do? const int SIZE = 5; double x[SIZE]; for(int i = 2; i <= SIZE; i++) { x[i] = 0.0; }
answer
Each element in the array, except the first and the last, is initialized to 0.0
question
What will the following code display? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday"; A) Monday Tuesday Wednesday B) Monday Tuesday Wednesday C) MondayTuesdayWednesday D) "Monday" "Tuesday" "Wednesday"
answer
C) MondayTuesdayWednesday
question
1 // This program displays my gross wages. 2 // I worked 40 hours and I make $20.00 per hour. 3 #include 4 using namespace std; 5 6 int main() 7 { 8 int hours; 9 double payRate, grossPay; 10 11 hours = 40; 12 payRate = 20.0; 13 grossPay = hours * payRate; 14 cout << "My gross pay is $" << grossPay << endl; 15 return 0; 16 } Which line(s) in this program cause output to be displayed on the screen? A) 13 and 14 B) 8 and 9 C) 14 e. 15 D) 13
answer
C) 14
question
What will the following code display? int number = 7; cout << "The number is " << "number" << endl; A) The number is 7 B) The number is number C) The number is7 D) The number is 0
answer
B) The number is number
question
What will the following code display? cout << "Four" << "score" << endl; cout << "and" << "seven" << endl; cout << "years" << "ago" << endl; A) Four score and seven years ago B) Four score and seven years ago C) Fourscoreandsevenyearsago D) Fourscore andseven yearsago
answer
D) Fourscore andseven yearsago
question
What will the following code display? cout << "Four " << "score "; cout << "and " << "seven/n"; cout << "years" << "ago" << endl; A) Four score and seven yearsago B) Four score and seven years ago C) Four score and seven/nyearsago D) Four score and seven yearsago
answer
C) Four score and seven/nyearsago
question
What will the following code display? cout << "Fourn" << "scoren"; cout << "and" << "nseven"; cout << "nyears" << " ago" << endl; A) Four score and seven years ago B) Four score and seven years ago C) Four score and seven years ago D) Four score and seven years ago
answer
A) Four score and seven years ago
question
What will the following code display? int x = 0, y = 1, z = 2; cout << x << y << z << endl; A) 0 1 2 B) 0 1 2 C) xyz D) 012
answer
D) 012
question
What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0; A) 2.0 B) 4.0 C) 1.5 D) 6.0
answer
B) 4.0
question
Which line in the following program will cause a compiler error? 1 #include 2 using namespace std; 3 4 int main() 5 { 6 const int MY_VAL; 7 MY_VAL = 77; 8 cout << MY_VAL << endl; 9 return 0; 10 } A) 6 B) 8 C) 9 D) 7
answer
A) 6
question
Which is true about the following statement? cout << setw(4) << num4 << " "; A) It allows four spaces for the value in the variable num4. B) It outputs "setw(4)" before the value in the variable num4. C) It should use setw(10) to output the value in the variable num10. D) It inputs up to four characters stored in the variable num4. E) None of these
answer
A) It allows four spaces for the value in the variable num4.
question
The statement cout << setprecision(5) << dollars << endl; will output $5.00 to the screen. A) True B) False
answer
B) False
question
What will the following segment of code output if 11 is entered at the keyboard? int number; cin >> number; if (number > 0) cout << "C++"; else cout << "Soccer"; cout << " is "; cout << "fun" << endl; A) C++ is fun B) Soccer is fun C) C++ D) C++fun E) Soccerfun
answer
A) C++ is fun
question
The following code correctly determines whether x contains a value in the range of 0 through 100 . if (x >= 0 && <= 100) A) True B) False
answer
B) False
question
What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2; A) 10 B) 120 C) 20 D) This code will not compile
answer
A) 20
question
What is the output of the following segment of code if 4 is input by the user when asked to enter a number? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl; A) 0 B) 3 C) 13 D) 28 E) None of these
answer
C) 13
question
What will the following program segment display? int funny = 7, serious = 15; funny = serious % 2; if (funny != 1) { funny = 0; serious = 0; } else if (funny == 2) { funny = 10; serious = 10; } else { funny = 1; serious = 1; } cout << funny << " " << serious << endl; A) 7 15 B) 0 0 C) 10 10 D) 1 1 E) None of these
answer
1 1
question
What will the following segment of code output? score = 40; if (score > 95) cout << "Congratulations!n"; cout << "That's a high score!n"; cout << "This is a test question!" << endl; A) This is a test question! B) Congratulations! That's a high score! This is a test question! C) That's a high score! This is a test question! D) Congratulations! That's a high score! E) None of these
answer
C) That's a high score! This is a test question!
question
Assuming x is 5, y is 6, and z is 8, which of the following is false? 1. x == 5; 2. 7 <= (x + 2); 3. z < = 4; 4. (1 + x) != y; 5. z >= 8; 6. x >= 0; 7. x <= (y * 2) A) 3, 4, 6, 7 are False B) Only 5 is False C) 3 and 4 are False D) All are False E) None of these are False
answer
C) 3 and 4 are false
question
Which value can be entered to cause the following code segment to display the message "That number is acceptable." int number; cin >> number; if (number > 10 && number < 100) cout << "That number is acceptable.n"; else cout << "That number is not acceptable.n"; A) 100 B) 10 C) 99 D) 0 E) All of these
answer
C) 99
question
What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2; A) 12 B) 10 C) 0 D) 1
answer
C) 0
question
Given that x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl; A) answer = 0 B) answer = 1 C) answer = 2 D) None of these
answer
B) answer = 1
question
After execution of the following code, what will be the value of input_value if the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15; A) 15 B) 10 C) 25 D) 0 E) 5
answer
A) 15
question
What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; } A) 98 B) 99 C) 0 D) 1
answer
D) 1
question
What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2; A) 12 B) 10 C) 0 D) 2
answer
A) 12
question
What will the following segment of code output? You can assume the user enters a grade of 90 from the keyboard. cout << "Enter a test score: "; cin >> test_score; if (test_score < 60); cout << "You failed the test!" << endl; if (test_score > 60) cout << "You passed the test!" << endl; else cout << "You need to study for the next test!"; A) You failed the test! B) You passed the test! C) You failed the test! You passed the test! D) You failed the test! You did poorly on the test! E) None of the above
answer
C) You failed the test! You passed the test!
question
Given the following code segment, what is output after "result = "? int x = 1, y = 1, z = 1; y = y + z; x = x + y; cout << "result = " << (x < y ? y : x) << endl; A) 0 B) 1 C) 2 D) 3 E) None of these
answer
D) 3
question
Which line in the following program will cause a compiler error? 1 #include 2 using namespace std; 3 4 int main() 5 { 6 int number = 5; 7 8 if (number >= 0 && <= 100) 9 cout << "passed.n"; 10 else 11 cout << "failed.n"; 12 return 0; 13 } A) 6 B) 8 C) 10 D) 9
answer
B) 8
question
What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl; A) Nothing will be displayed B) false C) x D) true
answer
B) False
question
What will the following program display? #include using namespace std; int main() { int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; } A) 0 1 1 0 B) 0 0 1 0 C) 1 1 0 1 D) 1 0 0 1 E) None of these
answer
A) 0 1 1 0
question
What is the value stored at x, given the statements: int x; x = 3 / static_cast(4.5 + 6.4); A) .3 B) 0 C) .275229 D) 3.3 E) None of these
answer
B) 0