CS 101, Spring 2012 Mar 20th Exam 2 Name: Question 1. [5 points] Determine which of the following function calls are valid for a function with the prototype: void drawrect(int width, int height); Assume variables a, b, length, width, height, and x all have type int. I drawrect(int a, int b); II drawrect(length, length*2); III void drawrect(width, height); IV drawrect(10, sqrt(x)); a. I only b. II only c. III only d. II and IV e. I, II, and IV Question 2. [5 points] Given the following symbolic constant definition #define SIZE 10 what is the proper way to declare an array named cost that can hold at most 11 double values: a. int cost[size]; b. double cost[size+1]; c. void cost[]; d. double cost(size); e. None of the above.
Question 3. [5 points] What output is printed by the following program (which begins on the left and continues on the right)? #include <stdio.h> int halfvalue(int num); int main(void) { int result = 0; int x = 5; halfvalue(x); printf("%i",result); return 0; int halfvalue(int num) { int result; result = num/2; return result; a. 0 b. 2 c. 2.5 d. 5 e. Does not compile. Question 4. [5 points] What must be present in a function prototype? a. function name, variable assignments, symbolic constants b. return type, function name, parameter list c. argument list, array access, return type d. int main(), return statement, array initialization e. function call, argument expressions, variable assignment Question 5. [5 points] Identify the error(s) in the following function. For each error, briefly explain how to fix it. void xray(int x, int y) { float x; x = pow(x,y) return x;
Question 6. [15 points] Write definitions for the functions described below. (The definition specifies the body (code) of the function.) Each description specifies the formula you should use to compute the function s result. Each function takes a single parameter (the given value). Hints: the M PI constant is a close approximation of π, and the sqrt function computes the square root of a double value. (a) Given the area a of a circle, compute its radius r. Use the formula r = a/π (b) Given the radius r of a circle, compute its circumference c. Use the formula c = 2πr (c) Given the circumference c of a circle, compute its diameter d. Use the formula d = c/π Question 7. [5 points] Using the function or functions you wrote for Question 6, write statements that prompt the user to enter the area of a circle and then print the diameter of the circle with that area. The output should use two points of precision (two places after the decimal point.)
For Questions 8 15, circle True or False. Question 8. [2 points] True False A function can only return one value. Question 9. [2 points] True False Every function must return a value. Question 10. [2 points] True False If you attempt to enter a value into an array that is out of bounds the array will grow accordingly. Question 11. [2 points] True False To determine the number of elements in an array called X you can use X.size() Question 12. [2 points] True False The elements of a one-dimensional array are variables. Question 13. [2 points] True False The following is valid: char Alphabet[26]; Question 14. [2 points] True False When accessing an array, the first index is always 1. Question 15. [2 points] True False double values. A single one-dimensional array can contain both int and
Questions 16 18 are programming questions.