Name Number True or False (15 Points) 1. (15 pts) Circle T for true and F for false: T F a) Value Returning Functions cannot use reference parameters. T F b) Arguments corresponding to value parameters can be variables. T F c) ptrvar = *var; assigns the memory address of var to ptrvar T F d) The memory address of the first element of an array is called the Base Address. T F e) The elements of a C++ array must be homogeneous T F f) Given the declaration int beta[20]; the statement cout << beta; can be used to display all 20 elements of the array named beta on the standard output device. T F g) If a program has the declarations enum WeatherType SUNNY, CLOUDY, FOGGY, WINDY; int frequency[4]; then the statement cout << frequency[cloudy]; is syntactically valid. T F h) The C++ statement typedef int Boolean; creates a new name of Boolean for the existing DataType int. T F i) A hierarchical structure has at least one component that is itself a structure. T F j) The members of a C++ structure are always homogeneous. T F k) The names of members in a structure must be unique for that structure T F l) The expression name.first is used to access the first member of the structure variable name. T F m) C++ checks for out-of-bounds array indexes at compile time. T F n) Using an array index that is out of bounds results in a compile error. T F o) The reserved word const before an array parameter allows a function to change values of the array. Page 1 of 11
Extra Credit #1 (2 pts) The code from the day when exam2 was returned (10/27) is: (must have received your exam back in class that day) A) Classes B) I show up C)Missed Again D) Pointers Extra Credit #2 (2 pts) The code given out in class on 11/8 is (must have signed the sheet) A) Classes B) I show up C)Missed Again D) Pointers Multiple choice (17 points) Questions 2 18 For these problems circle all correct answers. For example if answers A, C and E are all valid then circle A, C and E. 2. How many function values does a value-returning function have? A) 4 B) 2 C) As many as necessary D) 1 E) 3 F) None of these 3. A function that does not return a function value is known as what kind of function? A) Value returning function B) Reference Parameter C) Empty D) Void function E) Expression less F) None of these 4. Circle all operations that ARE ALLOWABLE aggregate operations on structures. A) Input/Output (cin, cout statements) B) Assignment C) Arithmetic D) Return as a functions return value E) Comparison F) Argument pass by value to a function G) Argument pass by reference to a function 5. Circle all operations that ARE ALLOWABLE aggregate operations on arrays. A) Input/Output (cin, cout statements) B) Assignment C) Arithmetic D) Return as a functions return value E) Comparison F) Argument in a function call G) All of the above (a through f) Page 2 of 11
6. Value parameters (passing by value) are used if a parameters data flow is A) One-way, into the function B) One-way, out of the function C) Two-way, into and out of the function D) None of these 7. Reference parameters (passing by reference) are used if a parameters data flow is: A) two-way, into and out of the function B) one-way, into the function C) one-way, out of the function D) None of these 8. What are the reference parameters in the following function heading? void DoSomething(string& date, int num, float average, float& sum, string name) A) date B) num C) average D) sum E) name F) None of them 9. What are the value parameters in the following function heading? void DoSomething(string date, int num, float average, float sum, string& name) A) date B) num C) average D) sum E) name F) None of them 10. The following code fragment invokes (calls) a void function named InitToZero: int alpha[20]; InitToZero(alpha); // returns alpha with every element of the array set to 0 Which of the following is a valid function heading for InitToZero? A) void InitToZero( const int beta ) B) void InitToZero( int beta[20] ) C) void InitToZero( int [20] ) D) None of These 11. For the function InitToZero described above in Problem 10, which of the following would be an appropriate function prototype? A) void InitToZero( const int [20] ); B) void InitToZero( const int ); C) void InitToZero( int [ ] ); D) None of These Page 3 of 11
12. Which of the following correctly compares the memory addresses contained in the pointer variables intptr1 and intptr2 A) If(intPtr1 == *intptr2) B) if (intptr1 == intptr2) C) if (*intptr1 == *intptr2) 13. Suppose that typedef int MatrixType[MAXROWS][MAXCOLS]; has been used to create a two-dimensional array matrix data type called MatrixType. Which of the following statements correctly declares a variable named C of the data type MatrixType? A) MatrixType[MAXROWS][MAXCOLS] C; B) MatrixType C; C) int MatrixType; D) None of These 14. Given the global type declaration MatrixType as described in problem 13, which of the following would be a suitable function prototype for a function named PrintMatrix? A) void PrintMatrix( MatrixType[ ][ ] ); B) void PrintMatrix( MatrixType M ); C) void PrintMatrix( MatrixType M[MAXROWS][ ]); D) None of These 15. The void function named GetNums has two parameters A pass-by-value parameter named x of type float A pass-by-value parameter named num of type int. Which of the following choices are correct function prototypes for the description of the parameters for the function GetNums? A) void GetNums( float&, int& ); B) void GetNums( float& x, int& num ); C) void GetNums( float, int num ); D) void GetNums(float, int); E) none of the above 16. Which of the following statements correctly declares a one-dimensional array variable named somearray with exactly 9 elements of type int? A) int somearray[8]; B) int somearray[9]; C) int somearray[10]; D) typedef int somearray[9]; E) B and D F) None of these Page 4 of 11
17. The function PrintMatrix will work only for 2x2 matrices. Assuming that the matrix dimensions are represented by global integer constants MAXROWS and MAXCOLS which specify the maximum number of rows and columns, respectively, in the matrix M, which of the following code fragments would correctly print out the contents of the matrix M row-by-row? If matrix M were as follows: M 1 2 3 4 Sample output for matrix M: 1 2 3 4 A) int r,c; B) int r,c; for(c=0; c<maxcols; c++) for(r=0; r<=maxrows; r++) for(r=0; r<maxrows; r++) for(c=0; c<=maxcols; c++) cout << M[r][c] ; cout << M[r][c] ; cout << endl; cout << endl; C) int r,c; D) int r,c; for(c=1; c<maxcols; c++) for(r=0; r<maxrows; r++) for(r=1; r<maxrows; r++) for(c=0; c<maxcols; c++) cout << M[r][c] ; cout << M[r][c] ; cout << endl; cout << endl; E) None of the above produces the desired output 18. Assuming that the one-dimensional array variable somearray has already been declared correctly, suppose that one wishes to initialize each array element to its index value ( the array element holds the value of the index necessary to access that element ) Which of the following program fragments correctly initializes the array somearray? A) int k = 0; B) int k = 1; C) int k = 0; while (k <= 9) while (k <= 9) while (k <= 9) somearray[k]=k; somearray[k]=k; k=somearray[k]; k++; ++k; ++k; D) int k = 0; E) int k = 0; F) None of these while (k < 9) while (k < 9) k = somearray[k]; somearray[k] = k; k++; k++; Page 5 of 11
Short Answer (68 points) Questions 19 26 19. (4 pts) There are four functions shown in the code segment below. Assume all variables and function prototypes have been correctly declared before this segment of code and that the code compiles. Compute(inFile, MyCalc()); Average(SumUp(num), num); A) Which function(s) is(are) most likely value-returning function(s)? B) Which function(s) is(are) most likely void function(s)? 20. (9 pts) Consider the program below. What is the output written to the terminal when this code is executed? #include <iostream> using namespace std; int main() int x = 1; // Assume variable x is stored at memory address 1000 int y = 2; // Assume variable y is stored at memory address 2000 int *a = &x; // Assume variable a is stored at memory address 3000 int* b = &y; // Assume variable b is stored at memory address 4000 cout << a << endl; cout << &a << endl; cout << *a << endl; cout << b << endl; cout << &b << endl; cout << *b << endl; return 0; Page 6 of 11
21. (5 pts) Consider the following structure declarations when answering the questions below. struct Date int day; int month; int year; ; struct Purchase string name; Date buydate; ; a) Write a statement that declares the identifier payday as a variable of DataType Date. b) Write a output statement that will output the value of month of the variable payday. c) Write a statement that declares the identifier item as a variable of DataType Purchase. d) Write a statement that assigns a value of Car to the name member of item. e) Write an output statement that will output the value of year of the buydate member of item. 22. (6 pts) Write a structure declaration for a structure named Zoo with the following members: an integer variable representing the number of animals a string variable representing the name of the zoo a string variable representing the city where the zoo is located a floating-point variable indicating the cost of admission Page 7 of 11
23. (10 pts) For the questions A through G, consider the following program. Note just the lines of importance have been displayed here. There is at least one answer for each question, and some may have more than one answer. Provide ALL the answers to a question. As an example, if the question is what line contains a return statement? The answer is line 12, 14, 16 and line 18. #include <iostream> // Line 1 using namespace std; // Line 2 int function_1(int&, float, int); // Line 3 void function_2(float&, int, float&); // Line 4 void function_3(); // Line 5 int main() // Line 6 int sum1, num, enter; // Line 7 float temp, avg; // Line 8 function_3(); // Line 9 function_2(avg,num,temp); // Line 10 function_2(avg,function_1(sum1,avg,num),temp); // Line 11 return 0; // Line 12 int function_1(int& sum, float number, int enter) // Line 13 return enter; // Line 14 void function_2(float& average, int count, float& number) // Line 15 return; // Line 16 void function_3() // Line 17 return; // Line 18 A) Which of the lines indicated are function prototypes? B) Which of the lines indicated include function calls? C) Which lines are a function heading? D) What are the arguments used in the function call for function_1? E) Which parameter(s) of function_2 are value parameters? Page 8 of 11
24. (10 pts) What is the output for the following program? Pay attention to the index values used with the arrays. #include <iostream> using namespace std; void init_array (int[][3]); void print_array (int [][3]); int main() int my_array[4][3]; init_array(my_array); print_array(my_array); return 0; void init_array (int my_array[][3]) int row, col; for (row = 0; row < 4 ; row++) for (col = 0; col < 3; col++) my_array[row][2 - col] = row + col; void print_array (int my_array[][3]) int row, col; for (row = 3; row >= 0; row--) for (col = 2; col >= 0; col--) cout << my_array[row][col] << ; cout << endl; Page 9 of 11
25. (12 pts) Finish the program below by adding a void function as specified below. Add only a function prototype, function call statement and function definition to the following program. Do not add any variable declarations. The name of the void function is InitStruct. The function has one parameter of the struct DataType Time. The function is to initialize the structure parameter with a time of 12:00:00 AM.(form is HH:MM:SS) The information stored in the parameter must be available in main() after the function call. #include <iostream> using namespace std; struct Time // format of the time is HH:MM:SS designator // where HH is for hours, MM for minutes and SS for seconds int hour; int minute; int second; string designator; // for AM or PM ; // Place the function prototype below this line int main() Time appt; // appt is a structure variable of data type Time // Place the function call below this line return 0; // Place the function definition below this line Page 10 of 11
26. (12 pts) Add a function prototype, function call statement and function definition to the following program. The name of the void function that is to be added is FillArray. This function is to read in values from standard input (cin) to fill all of the array elements. The void function has the following two parameters: 1) a floating-point one-dimensional array, 2) an integer value representing the number of array elements to read in. In the function definition use a for loop to read in a value for each element/component of the array. Use cout to prompt for a value and cin for reading the value. Assume that all values are entered correctly. #include <iostream> using namespace std; const int NUM = 100; // dimension size: number of values to read // Place the function prototype below this line int main() float numbers[num]; // one-dim. Array for holding floats entered // Place the function call statement below this line return 0; // Place the function definition below this line Page 11 of 11