Cpre185exam1 Sample 2008
Cpre185exam1 Sample 2008
Cpre185exam1 Sample 2008
Directions:
Answer all questions to the best of your ability. You may use your textbook but the use of laptops and other portable computing devices is prohibited. Please ensure any cell-phone or other alarmed devices are turned off for the remainder of the exam.
Multiple Choice:
Circle the letter that best answers the following statements (3 pts each). 1.) A compiler performs the operation of: a. opening and executing application programs. b. translating source code into executable machine language. c. compiles data sets for statistical purposes. d. examines machine hardware for proper configuration. e. all of the above. 2.) If the variable num is of type double and temp is a variable of type int, how could you correctly complete this function call? a. b. c. d. e.
scanf("%lf%d", _________); num, temp &num, &temp temp, num &temp, &num
3.) Which of the following are valid variable names? i. _this ii. That_ iv. 7samarai v. main a. i, ii, iv, v b. i, iv c. i, ii, v d. i, iv, v e. All are valid.
5.) Which one of the following lines names a constant needed in a program that computes the density of a steel washer? a. scanf("%lf", &radius); b. pi = 3.14159; c. #define PI 3.14159 d. #include <pi> e. a. and c. 6.) What type is returned by the following function:
double kapow(int x, int y) { while (y > 0) { x = x * x; y--; } return 1.0 * x; }
a. b. c. d. e.
int double x
7.) Assume that the variable control is of type int. if (control >3) { printf( inside ); if (control-4) { printf( out ); } } else if (control < 3) { printf( outside ); if ((control *2) < 3) { printf( in ); } } What is displayed by the above code segment when control is 4? a. inside b. insideout c. outside d. out e. outsidein f. Code will display nothing. What is displayed by the above code segment when control is 1? a. inside b. insideout c. outsidein d. out e. outside f. Code will display nothing. 8. Which of the following are equal to binary 01100111 ? (You may select more than one) 67 (hexadecimal) 103 (base 10) -1 (base 10) 147 (base 8) 01100000 + 00000111 (all in binary) AC (Hexadecimal) 18 (base 95)
a) b) c) d) e) f) g)
Short Answer:
Answer the question in the space provided. 1.) What does the following code segment display? Be sure to format your output appropriately (10 pts)
#include "stdafx.h" double poly(double a, double b); int main(int argc, char* argv[]) { double a=0.0; a = poly(-2,-3); printf("f(2,3) = %.2f\n", poly(2,3)); printf("f(-2,3) = %.2f\n", poly(-2,3)); printf("f(-2,-3) = %.5f\n", a); printf("%.3f\n", poly(a,2)); printf("%.4f\n", poly(a,-a)); getchar(); return 0; } double poly(double a, double b) { int result = 0; if (((a < 0) && (b > 0)) || ((a > 0) && (b < 0))) result = -(a*a + b*b); else result = (a*a + b*b); } return (result);
2.) Write a C function named least that returns as its value the least of its two type int input parameters num1 and num2 (20 pts). If they are equal, return either value.
3.) What does the following code segment display (20 pts)? You may assume that i,j, and k are defined as int. Write the output as it would appear.
int i = 1; while (i<3) { int j =1; while (j<4) { printf(%d j++; } printf(\n); i++; }
, i+j);
4.) Complete the program below so that it computes the price of a piece of glass. Pieces of glass are usually priced based on the type of glass and the area of the piece, but there is a minimum charge of $5.00. For clear glass (glass type 1), the charge is $5.00 per square meter; for frosted glass (type 2), the price is $7.00 per square meter. For example, the cost of a 0.25-square-meter piece of clear glass is $5.00 since 0.25 * $5.00 is $1.25, an amount less than the minimum charge. The price of a 2.4-squaremeter piece of frosted glass is $16.80 (2.4 * $7.00). You do not need to do error checking in the program (25 pts). You may want to work on the back and then put your final answer in the space below.
#include stdafx.h #define #define #define #define #define CLEAR SQMETER_CLEAR FROSTED SQMETER_FROSTED MINIMUM 1 5.00 2 7.00 5.00
int _tmain(int argc, _TCHAR* argv[]) { double price, area; int type; printf("Enter glass type: %d (clear) or %d (frosted)> ", CLEAR, FROSTED); scanf("%d", &type); printf("Enter area in square meters> "): scanf("%lf", &area); /* ENTER YOUR CODE BELOW */
5.) Did you remember to write your name on the first page (1 pt)?