CSCE 2004 Midterm Exam Spring 2017 Student Name: Student UAID: Instructions: This is a 50 minute exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers are not allowed. Reminder: This class is governed by the code of ethics of the College of Engineering and Department of Computer Science and Computer Engineering. Any instances of cheating will result in a grade of zero on the exam and referral to the ethics board. Page 1
Consider the following C++ variables: float Num1 = 10; int Num2 = 5; float Num3 = 2; Expressions [1 point] What is the value of: (1+2+3) / Num1 [1 point] What is the data type of this expression? [1 point] What is the value of: (Num2 + 2 * 5-4) [1 point] What is the data type of this expression? [1 point] What is the value of: (Num3 * 2) + 5 / 2 [1 point] What is the data type of this expression? [1 point] What is the value of: Num1 * Num2 + Num3 [1 point] What is the data type of this expression? Consider the following C++ code: float Num1 = (1 + 2 + 3); int Num2 = 5 / 2.0; [1 point] What is value stored in variable Num1? [1 point] What is value stored in variable Num2? Page 2
If Statements Consider the problem of choosing a place to rent based on your monthly budget and the number of roommates you are planning to live with. If you have over $2,000, rent a house to share with 3 roommates. If you have over $1,500, rent a 2 bedroom apartment with 1 roommate. If you have over $1,000, rent a 1 bedroom apartment by yourself. If you have under $1,000, rent your parent s basement. The following C++ code implements this decision process: float Money = 0.0; int Roommates = 0; cin >> Money >> Roommates; if ( condition 1 ) cout << "Rent a house and throw big parties\n"; else if ((Money > 1000) && (Roommates == 0)) cout << "Rent a one bedroom apartment\n"; else if ( condition 2 ) cout << "Rent a two bedroom apartment\n"; else if (Money < 1000) cout << "Rent your parent's basement\n"; else cout << "Sorry, no advice\n"; [5 points] What should condition 1 be? a) (Money > 2000) b) (Money > 2000) && (Roommates!= 0) c) (Roommates >= 0) && (Roommates <= 3) && (Money > 2000) d) (Money > 2000) && (0 <= Roommates <= 3) e) None of the above. [5 points] What should condition 2 be? a) (Money > 1500) (Roommates < 2) b) (0 <= Roommates <= 1) c) (Money * Roommates == 1500) d) (Roommates == 1) && (Money > 1500) e) None of the above. Page 3
Consider the following C++ code, which is intended to calculate the cost of an item after tax has been added. Notice that different tax rates are applied based on prices. float Tax1 = 0.05; float Tax2 = 0.10; float Price = 0.0; float Cost = 0.0; cin >> Price; if (Price > 5000.0) Cost = Price + Price * Tax2; else if (Price > 3000.0) Cost = Price + Price * Tax1; else Cost = Price; cout << "Cost:" << fixed << setprecision(2) << Cost << endl; [5 points] Assume that the user enters 10000 for the Price. What does this program print for the value of Cost? a) 10500.00 b) 10000.00 c) 0.00 d) 11000.00 e) None of the above. [5 points] Assume that the user enters 2000 for the Price. What does this program print for the value of Cost? a) 2200.00 b) 2000.00 c) 0.00 d) 2100.00 e) None of the above. Page 4
Loops [5 points] How many times will the following loop display Hi Mom!? for (int i = 10; i >= 0; i--) cout<<" Hi Mom!"<<endl; i--; a) 5 b) 6 c) 10 d) 11 e) None of the above. [5 points] What is the output of the following code segment? int n = 1; while (n <= 5 n > 0) cout << n << ' '; n++; a) 1 2 3 4 5 b) 1 1 1... and so on forever c) 1 2 3 and so on forever d) 1 2 3 4 e) 2 3 4 5 6 Page 5
[5 points] What will the following code display? int x = 0; for (int count = 4; count > 0; count--) x = x + count; cout << x << ; a) 4 3 2 1 b) 4 7 9 10 c) 4 d) 10 e) none of the above [5 points] Consider the following code. What is the final value of x? int x, y; x = 1; while (x < 40) for (y = 0; y <= 20; y = y+10) x = x + y; a) 1 b) 21 c) 40 d) 41 e) none of the above Page 6
Function Prototypes [10 points] For this question, your task is to write function prototypes (the first line of the function that gives the return type and the list of parameters) that match the comments below. You do NOT have to fill in the function bodies. // This function takes in a letter grade between A and F and // calculates the corresponding grade point value between 0.0 and 4.0. // The answer is returned by the function. GetGradePoint ( ); // This function takes in two integer numbers and calculates their difference. // The answer is returned by the function. CalculateDifference ( ); // This function prints a message and asks the user their shoe size. // The answer is stored in a floating point reference parameter. GetShoeSize ( ); // This function exchanges the values of two character reference parameters. // The function does not return any value. Exchange ( ); // This function takes in one floating point number and calculates the cube root. // The answer is stored in a floating point reference parameter. CubeRoot ( ); Page 7
Calling Functions [10 points] Assume we have the following variables and function prototypes. Which of the function calls below are correct (they compile without errors or warnings, and the return values and parameters are used properly) and which function calls are incorrect? Circle your answer. // Variable declarations int A, B; float X, Y, Z; char C, D; // Function prototypes void ReadChar(char & ch); int GetMin (int Num1, float Num2); float FindSum (float A, float B); cout << ReadChar (); ReadChar (C); cin >> ReadChar (); cout << GetMin (2, 3); A = GetMin (Num1, Num2); GetMin (A, B, C); B = GetMin(A+2, B-1); FindSum (X, Y, Z); Z = FindSum (X, Y); FindSum (X-Y, Y-Z); Page 8
Tracing Functions Consider the following three C++ functions. void silly (int &U, int &V) cout << U << " " << V << endl; if (U < V) int temp = U; U = V; V = temp; cout << U << " " << V << endl; int goofy (int X, int Y, int Z) cout << X << " " << Y << " " << Z << endl; silly (X, Y); silly (Y, Z); cout << X << " " << Y << " " << Z << endl; return Z; int main() // Part A int U = 6; int V = 8; silly (U, V); // Part B int Result = goofy (7, 1, 2); cout << "Result: " << Result << endl; return(0); Page 9
[5 points] Using the box method, trace the execution of Part A in the program above to show the sequence of function calls, the values of parameters, and return values. What will the Part A section of the code print? [5 points] Using the box method, trace the execution of Part B in the program above to show the sequence of function calls, the values of parameters, and return values. What will the Part B section of the code print? Page 10