Solutions to Midterm 6/9/2000

 

1. 3  8 – 12 / 5 = 8 – 2 = 6

2. 3 14.0 / 3.0 + 5.0 = 4.6667 + 5.0 = 9.6667

3. 2  3 % 7 – 4 = 3 – 4 = -1

4. 1  4.2 – 3 * 2 = 4.2 – 6 = -1.8

5. 1 AnInt is an int, so it cannot hold 2.6.  The value will be truncated to 2.

6. 5 The statement is not legal because aChar can only hold one character, not an entire string of characters.

7. b Case 0 is executed, so x is set to 5.  Then the break statement jumps out of the switch statement.

8. d Case 1 is executed, but since there is no break, cases 2 and 3 are executed also, setting x to 11.

9. a Since anInt is 11, and there is no case for 11, the default case is executed.  The default case sets x to 0.

10. 1 The statements are syntactically correct.

11. 1 The statements are syntactically correct.  Remember that in C++, 0 is false and nonzero is true.

12. 2 The first statement is syntactically incorrect, because ++ cannot be used as a binary operator.

13. 1 The statements are syntactically correct.  Since the condition is false, lucy will be set to 0.

14. 3 No output is produced due to the way to curly braces are arranged.

15. b Since x is an int, the floating point value 2.6 will be truncated to 2.

16. c The value 2 is passed into QuadFunc.  Temp therefore equals 9.5.  QuadFunc returns an int, which will be 9, which is what is printed.

17. f  Since Temp is defined as a float, it has the value 9.5.

18. 1 The second line of output will be the average of the 4th, 5th, and 6th numbers, which is 2.

19. 4 The last line of output contains the total of all 9 numbers, which is 46.

20. 1 It IS legal to place the prototype of one function inside the definition of another function.

21. b Variables declared inside main() are local to main() and cannot be accessed by other functions (unless of course they are passed as parameters).

22. 3 First the value 4 would be read into i1.  Since the extraction operator (>>) skips whitespace, the value ‘2’ will be read into ch1.

23. 10 Since i1 is an integer, it cannot have any of the listed values, since they all have type char.

24. 2  Since the function does not need to modify the array, it should be passed by constant reference.  However, since passing by reference will work, I will also accept 1.  If you put 1, come see me during my office hours to get a point back.

25. 3  Sum must be passed by reference in order for the function to do anything useful.

26. 3  Sum should be initialized to 0.

27. 1 

28. 2

29. 1

30. b  A is used as an actual parameter in the call MaxVal(A, B);

31. a They have file scope since they are outside because they are declared outside of any function.

32. c The local “Big” will be printed out.

33. a  The global “Big” is being assigned a value.

34. 6

35. 3

36. 1

37. e  When passed by constant refernce, the variable cannot be modified.

38. 3  Since there is no break after case 2, case 3 will execute and then break out.

39. 4  Case 4 will execute, and then the default case will execute.  However, the default case does not alter middle.

40. 5  The default case will execute, which does not alter middle.

 

Sample Solution to Programming Problem on Midterm Exam

 

 

void SwapEm(int x[], int y[], int size)

{

            int temp;

            for (int i = 0; i < size; i++)

            {

                        temp = x[i];

                        x[i] = y[i];

                        y[i] = temp;

                        cout << x[i] << endl;

            }

}

 

The point breakdown was as follows:

(1) point for correctly using the return type of the function

(1) point for giving the function a name (other than main)

(1) point for passing the first array in by reference

(1) point for passing the second array in by reference

(1) point for passing the size of the array in (or by correctly accessing it globally)

(1) point for having matching curly braces around your function

(1) point for declaring a temporary variable

(1) point for declaring a loop control variable

(1) point for using a looping construct (preferrably for but while is okay)

(1) point for initializing your loop control variable to zero

(1) point for having the correct loop termination condition

(1) point for incrementing the loop control variable in your loop

(1) point for having matching curly braces around your loop

(2) points for copying the value of x[i] into temp

(2) points for copying the value of y[i] into x[i]

(2) points for copying the value of temp into y[i]

(1) point for including the cout statement to print x[i]