Chapter 9 Review-C++ True/False Indicate whether the sentence or statement is true or false. 1. It is legal to use the assignment operator (=) with character arrays. 2. The array arr[30] contains 30 elements. 3. Templates have prototypes. 4. In C++, "Baby" is considered greater than "Bunny". 5. A function cannot return a value of the type array. 6. Given the declaration string str[8] and assuming that all of the elements in str contain strings that are at least 5 characters long str[3][2] refers to the third character in the second element of str. 7. The declaration int nums[] = 2,3,4,5}; declares nums to be an array of integers, size 4. 8. In C++, when using < and > with string objects, you are comparing each entire string alphabetically. 9. In C++, tolower()takes a parameter of a string. 10. The following is a correct function definition: void myfun(const int arr[5][2]) <function body> } 11. Arrays, by default, are passed by reference. 12. To display a two dimensional array, you must use a nested loop. 13. When you specify the size of a two dimensional array, the first number represents the number of rows and the second number represents the number of columns. 14. In the following declaration, the array gamma has 11 components int gamma[5][6];. 15. Templates allow you to write functions that will work for more than one data type. Multiple Choice Identify the letter of the choice that best completes the statement or answers the question. 16. Which of the following statements declares alpha to be an array of 25 components of the type int? a. int alpha[25]; c. int alpha[2][5]; b. int array alpha[25]; d. int array alpha[25][25]; 17. Assume you have the following declaration double salesdata[1000];. Which of the following ranges is valid for the indices of the array salesdata? a. 0 through 999 c. 1 through 1001 b. 0 through 1000 d. 1 through 1000 18. Consider the declaration below. Which of the following codes correctly outputs all the elements of list?
a. for(j = 1; j < 10; j++) cout<<list[j]<<" "; cout<<endl; b. for(j = 0; j <= 9; j++) cout<<list[j]<<" "; cout<<endl; c. for(j = 1; j < 11; j++) cout<<list[j]<<" "; cout<<endl; d. for(j = 1; j <= 10; j++) cout<<list[j]<<" "; cout<<endl; 19. What is the output of the C++ code below? a. 0 1 2 3 4 c. 0 5 10 15 20 b. 0 5 10 15 d. 5 10 15 20 20. Which of the following is a correct way to begin a template function? a. template <type whatever> c. template <typename double> b. template <type int> d. template <typename whatever> 21. Given the declaration below, 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]<<" "; 22. What is the value of alpha[4] after the code below executes? a. 5 c. 11 b. 8 d. 14 23. What is the value of alpha[3] after the code below executes? a. 10 c. 15 b. 13 d. 17 24. What is stored in alpha after the code below executes?
a. alpha = 0, 2, 4, 6, 8} b. alpha = 0, 2, 9, 6, 8} c. alpha = 0, 3, 4, 7, 8} d. alpha = 3, 2, 9, 6, 8} 25. What is the output of the C++ code below? a. 2 4 6 8 10 c. 8 6 4 2 0 b. 4 3 2 1 0 d. 10 8 6 4 2 26. What is the value of alpha[3][2] after the code below executes? int alpha[5][5]= 1, 2, 3, 4, 5}, 6, 7, 8, 9, 10}, 11, 12, 13, 14, 15}, 16, 17, 18, 19, 20}, 21, 22, 23, 24, 25} }; a. 8 c. 14 b. 13 d. 18 27. Consider the following statement: int alpha[25][10];. Which of the following statements about alpha is true? a. Rows of alpha are numbered 0...24 and columns are numbered 0...9. b. Rows of alpha are numbered 0...24 and columns are numbered 1...10. c. Rows of alpha are numbered 1...24 and columns are numbered 0...9. d. Rows of alpha are numbered 1...25 and columns are numbered 1...10.
28. Given the declaration below, which of the following correctly finds the sum of the elements of the fifth row of sale? a. sum = 0; for(j = 0; j < 7; j++) sum = sum + sale[5][j]; b. sum = 0; for(j = 0; j < 7; j++) sum = sum + sale[4][j]; c. sum = 0; for(j = 0; j < 10; j++) sum = sum + sale[5][j]; d. sum = 0; for(j = 0; j < 10; j++) sum = sum + sale[4][j]; 29. After the statements below execute, what are the contents of matrix? a. 0 2 4 1 3 5 2 4 6 3 5 7 b. 0 1 2 1 2 3 2 3 4 3 4 5 c. 0 2 4 2 4 6 4 6 8 6 8 10 d. 0 1 2 2 3 4 4 5 6 6 7 8 30. In C++, which of the following strings would be considered the "greatest?" a. "yellow" c. "Blue" b. blue d. Yellow
Short Answer For the following problems, you should assume that iostream is the only header file that has been included!!!! 31. Write the function definition. Call the function wordcounter. It has two parameters: an array of strings and a character. The function should return an int. The return value represents the number of words in the array that start with the character. You can assume the array has a size of 100. 32. Write a template function called fillarray that takes an array of any data type, size 10. The function will also take an argument that is a single specific value to store in the array. The function will fill the array passed in with the value specified. The function will not return anything. The array should be changed by this function! Example : fillarray(a, 5); will assign 5 to every element of array a, assuming that a is an array of ints.
Chapter 9 Review Answer Section TRUE/FALSE 1. ANS: F 2. ANS: T 3. ANS: F 4. ANS: F 5. ANS: T 6. ANS: F 7. ANS: T 8. ANS: T 9. ANS: F 10. ANS: T 11. ANS: T 12. ANS: T 13. ANS: T 14. ANS: F 15. ANS: T MULTIPLE CHOICE 16. ANS: A 17. ANS: A 18. ANS: B 19. ANS: C 20. ANS: D 21. ANS: C 22. ANS: D 23. ANS: B 24. ANS: D 25. ANS: D 26. ANS: D 27. ANS: A 28. ANS: B 29. ANS: D 30. ANS: A
SHORT ANSWER 31. ANS: int wordcounter(const string s[], char c) int count=0; for(int x=0; x<100; x++) if(s[x][0]==c) count++; } return count; } 32. ANS: template <typename any> int fillarray(any arr[], any item) for(int x=0; x<10; x++) arr[x]=item; }