COP 3014: Fall 2017 Midterm 1 Total Points: 115 (15 point extra credit) Thursday 10/12/2017 Name: FSUID: Section: Instructions • The exam has 8 pages (including this page). • You have 75 minutes to solve 5 problems. • The exam is closed book, closed notes. Cheat sheets are not allowed. • Partial credit will be given for Problem 1 if compelling reasons are given for your choice. • Please be as specific as possible when answering short answer questions. • You can use the backs of the sheets as scratch paper. • You have an opportunity to earn 15 extra credit points (2.25 overall percentage points). Grade Summary • Problem 1: / 30. • Problem 2: / 20. • Problem 3: / 30. • Problem 4: / 20. • Problem 5: / 15. • Total: / 100. 1
Problem 1: Multiple choice Questions. 2x15 = 30 points 1. The extraction operator is used to extract values FROM a stream. Which of the following is the extraction operator? (a) << (b) >> 2. Which of the following is a valid variable name in C++? (a) three things (b) black&blue (c) default (d) someValue 3. Which of the following is NOT a C++ primitive data type? (a) double (b) function (c) float (d) char 4. Which of the following statement is FALSE? (a) Local variables are destroyed when a function returns. (b) A variable declared in the initial assignment of a for loop is available outside the loop. (c) Global variables are alive from declaration to the end of the program. (d) Reference variables retain their changes across function calls. 5. A variable declared inside a block is normally available only within that block. (a) True (b) False 6. Which of the following is NOT an available loop in C++? (a) for (b) while (c) do-while (d) redo 7. Which of the following is a valid case label in a switch statement? (a) 12 (b) 2.95 (c) "value" (d) print() 8. The continue statement causes execution to move on to the next iteration of the loop. (a) True (b) False 2
9. What does this expression evaluate to? a = 10, b = 6, c = 17, d = 3. All integers. a * a - c / b + d - 5 (a) 11 (b) 96 (c) 95.1666666667 (d) 11.8333333333 10. Which of the following is NOT a C++ operator? (a) ! (b) ++ (c) $ (d) && 11. Which of the following operators has the highest precedence (evaluated first)? (a) + (addition) (b) * (multiplication) (c) - (subtraction) (d) = (assignment) 12. Which of the following is NOT an attribute of the for loop? (a) You can have multiple assignments and increments, but only one condition. (b) The initial assignment is executed only once at the start of the loop. (c) The loop body is executed at least once. (d) You can nest for loops inside for loops. 13. Which of the following functions is not included in the cmath library? (a) cos() (b) pow() (c) sqrt() (d) setprecision() 14. Which of the following is NOT a valid C++ comment? (a) //This is a comment (b) /* This is a comment */ (c) < !– This is a comment – > (d) None of the above. 15. Which of the following is true for a default parameter? (a) It can be used only in overloaded functions. (b) It can be only of integer type. (c) It can only be at the end of the parameter list. (d) It can be only at the beginning of the parameter list. 3
Problem 2: Read through the code and indicate what the output would be. 10 x 2 = 20 points You can assume this code snippet is a part of a larger program that will compile. Don’t worry about undeclared variables, etc. You can assume there is code above these lines that will allow this to run. 1. int x = 219, y =30, z =23; int k = ++x / y + z--; cout<<"K is "<<k<<endl; cout<<"X is "<<x<<endl; cout<<"Z is "<<z<<endl; double area = 3.14 * 11*11; if( area < 200) cout<<"This circle will protect us from the sea bear"; else if(area > 1000) cout<<"No cubed cheese. Slices are fine."; else cout<<"You wore a sombrero in a goofy fashion. Aaaaahhh!!!"; 2. sum = 0, j = 32; bool done = false; for(int i=0; i<100; i++) { if( i % 19 == 0) sum += i; while( j > 25 && ! done) { j = j - 5; if ( j > 30 ) { done = true; break; } } } cout<<"The sum is "<< sum <<endl << "j is " << j << endl; 4
Problem 3: The scary, code writing problem. 10 + 20 = 30 points 1. Print cubes of numbers - 10 points Write a C++ program to accept a bunch of numbers from the user. Accept the numbers in a loop and stop when the user enters 0. Print the cube of the numbers as they come in. You can use the math library. You don’t have to account for floating point precision. Sample Run: Enter the numbers(0 to stop):12 The cube is 1728 -8 The cube is -512 3.93 The cube is 60.70 -22.5 The cube is -11390.63 0 5
2. Calculate the average of the factors of a number - 20 points Write a C++ function the accepts an integer parameter “num” and returns a double value. The function should calculate the average of the factors of “num”. You can assume that “num” will be a positive integer greater than 1. You don’t have to account for floating point precision. Your function should not have any cin or cout statements. The sample runs assume this function is being called multiple times in main(). Sample Run: Enter a number: 36 The average is 10.11 Enter a number: 19 The average is 10 Enter a number: 150 The average is 31 6
Problem 4: Debug the following program and fix the syntax errors. 20 points // Program to print the Fibonacci series up to a given number /* The Fibonacci series is a series of numbers where a number is the sum of the previous 2 numbers. */ / For example: If the upper limit is 50, the Fibonacci series is: //0, 1, 1, 2, 3, 5, 8, 13, 21, 34 */ #include <iostream>; using namesapce std; int main() { cout << "Enter the upper limit: "; int limit; cin << limit; // read in upper limit int a = 0, b = 1, c; cout << a, << b , ; while (a =< limit) { c = a + b; // sum up previous 2 numbers cout c << ", ""; // print the new number // update the 2 numbers a = b; b = c, } retrun 0; 7
Problem 5: Short answer questions. 5 x 3 = 15 points 1. What is the conditional operator? Give an example of its use. 2. What is typecasting? How would one cast an int into a char? 3. What is function overloading? Explain with an example. 8
Recommend
More recommend